ActiveMQ消息中間件Producer和Consumer


wKiom1XUiqHzUG7LAAMAStjJo7g288.jpg



生產者代碼:java

package com.java1234.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息生產者
 * @author Administrator
 *
 */
public class JMSProducer {

    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; // 默認的鏈接用戶名
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默認的鏈接密碼
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默認的鏈接地址
    private static final int SENDNUM=10; // 發送的消息數量
    
    public static void main(String[] args) {
        
        ConnectionFactory connectionFactory; // 鏈接工廠
        Connection connection = null; // 鏈接
        Session session; // 會話 接受或者發送消息的線程
        Destination destination; // 消息的目的地
        MessageProducer messageProducer; // 消息生產者
        
        // 實例化鏈接工廠
        connectionFactory=new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
        
        try {
            connection=connectionFactory.createConnection(); // 經過鏈接工廠獲取鏈接
            connection.start(); // 啓動鏈接
            session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 建立Session
            destination=session.createQueue("FirstQueue1"); // 建立消息隊列
            messageProducer=session.createProducer(destination); // 建立消息生產者
            sendMessage(session, messageProducer); // 發送消息
            session.commit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            if(connection!=null){
                try {
                    connection.close();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 發送消息
     * @param session
     * @param messageProducer
     * @throws Exception
     */
    public static void sendMessage(Session session,MessageProducer messageProducer)throws Exception{
        for(int i=0;i<JMSProducer.SENDNUM;i++){
            TextMessage message=session.createTextMessage("ActiveMQ 發送的消息"+i);
            System.out.println("發送消息:"+"ActiveMQ 發送的消息"+i);
            messageProducer.send(message);
        }
    }
}


消費者分兩種:apache

1.主動接受消息receive()session

package com.java1234.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息消費者
 * @author Administrator
 *
 */
public class JMSConsumer {

    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; // 默認的鏈接用戶名
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默認的鏈接密碼
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默認的鏈接地址
    
    public static void main(String[] args) {
        ConnectionFactory connectionFactory; // 鏈接工廠
        Connection connection = null; // 鏈接
        Session session; // 會話 接受或者發送消息的線程
        Destination destination; // 消息的目的地
        MessageConsumer messageConsumer; // 消息的消費者
        
        // 實例化鏈接工廠
        connectionFactory=new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
                
        try {
            connection=connectionFactory.createConnection();  // 經過鏈接工廠獲取鏈接
            connection.start(); // 啓動鏈接
            session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 建立Session
            destination=session.createQueue("FirstQueue1");  // 建立鏈接的消息隊列
            messageConsumer=session.createConsumer(destination); // 建立消息消費者
            while(true){
                TextMessage textMessage=(TextMessage)messageConsumer.receive(100000);
                if(textMessage!=null){
                    System.out.println("收到的消息:"+textMessage.getText());
                }else{
                    break;
                }
            }
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
}

2.使用listener被動監聽消息(非阻塞的)(觀察者模式)異步


Listener代碼ide

package com.java1234.activemq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * 消息監聽
 * @author Administrator
 *
 */
public class Listener implements MessageListener{

    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
        try {
            System.out.println("收到的消息:"+((TextMessage)message).getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}



package com.java1234.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息消費者
 * @author Administrator
 *
 */
public class JMSConsumer2 {

    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; // 默認的鏈接用戶名
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; // 默認的鏈接密碼
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; // 默認的鏈接地址
    
    public static void main(String[] args) {
        ConnectionFactory connectionFactory; // 鏈接工廠
        Connection connection = null; // 鏈接
        Session session; // 會話 接受或者發送消息的線程
        Destination destination; // 消息的目的地
        MessageConsumer messageConsumer; // 消息的消費者
        
        // 實例化鏈接工廠
        connectionFactory=new ActiveMQConnectionFactory(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.BROKEURL);
                
        try {
            connection=connectionFactory.createConnection();  // 經過鏈接工廠獲取鏈接
            connection.start(); // 啓動鏈接
            session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 建立Session
            destination=session.createQueue("FirstQueue1");  // 建立鏈接的消息隊列
            messageConsumer=session.createConsumer(destination); // 建立消息消費者
            messageConsumer.setMessageListener(new Listener()); // 註冊消息監聽
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
}



若是想主動的去接受消息,而不用異步消息監聽的話,把consumer.setMessageListener(this)改成Message message = consumer.receive(),手動去調用MessageConsumer的receive方法便可。this

參考文章:spa

http://blog.csdn.net/courage89/article/details/8809551.net

相關文章
相關標籤/搜索