1. 程式人生 > >activeMQ---入門

activeMQ---入門

首先到官網下載

http://activemq.apache.org/

開啟bin-選擇自己電腦對應的版本的bat,雙擊執行

此時訪問本機http://localhost:8161 需要登陸,使用者名稱和密碼都是:admin

 

此時控制檯啟動成功。建立專案,普通 java專案,引入activeMQ依賴

下面編寫生產者類
public class JMSProducer {

    private static final String USERNAME=ActiveMQConnectionFactory.DEFAULT_USER;
    private static final String PASSWORD=ActiveMQConnectionFactory.DEFAULT_PASSWORD;
    private static final String BROKEURL=ActiveMQConnectionFactory.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);
            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) {
                    e.printStackTrace();
                }
            }
        }
    }

    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);
        }
    }
}

再編寫消費者類

public class JMSConsumer3{
    private static final String USERNAME = ActiveMQConnectionFactory.DEFAULT_USER;
    private static final String PASSWORD = ActiveMQConnectionFactory.DEFAULT_PASSWORD;
    private static final String BORKERURL = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
    private static final int SENDNUM = 10;

    public static void main(String []args){
        ConnectionFactory connectionFactory;
        Connection connection = null;
        Session session ;
        Destination destination;
        MessageConsumer messageConsumer ;

        //例項化工廠
        connectionFactory = new ActiveMQConnectionFactory(JMSConsumer3.USERNAME,JMSConsumer3.PASSWORD,JMSConsumer3.BORKERURL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("SencondQueue");
            messageConsumer = session.createConsumer(destination);
            messageConsumer.setMessageListener(new Listener());//註冊監聽器
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
public class Listener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        try {
            System.out.println("收到的訊息:"+((TextMessage)message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

執行生產者產生訊息,執行消費者消費訊息,控制檯檢視訊息生產-消費情況。