ActiveMQ入門實例

該實例演示了ActiveMQ發送和異步接受文本類型消息的簡單功能。
要求:熟悉Java,瞭解JMS,瞭解ActiveMQ
環境:ActiveMQ5.1.0,JDK1.5或以上,Eclipse3.2或以上。

ActiveMQ5.1.0的下載地址:http://activemq.apache.org/activemq-510-release.html ,下載apache-activemq-5.1.0-bin.zip 版本,解壓即可。ActiveMQ也提供了從命令行啓動的功能,需要在環境變量中配置PATH。

主要源碼說明:
類com.honno.activemq.MQBroker負責啓動ActiveMQ的消息代理。
代碼片段:
BrokerService broker = new BrokerService();
        try {
            broker.addConnector("tcp://localhost:61616");
            broker.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
類com.honno.activemq.producer.Producer生產10個文本消息,併發送出去。
代碼片段:
try {
      // TODO Auto-generated method stub
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
        try {
           
            Connection connection = connectionFactory.createConnection();
            connection.start();

            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
           
            Destination destination = session.createQueue("TEST");
           
            MessageProducer producer = session.createProducer(destination);
           
            TextMessage message = session.createTextMessage();
   
            PrintlnUtil.println("Sending Messags");
           
            for (int i = 0; i < 10; i++) {
                message.setText("This is message " + (i + 1));
                producer.send(message);
            }

           
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


類com.honno.activemq.consumer.Consumer異步接收的方式消費消息。
代碼片段:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
        try {
           
            Connection connection = connectionFactory.createConnection();
            connection.start();

           
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
           
            Destination destination = session.createQueue("TEST");
           
            MessageConsumer consumer = session.createConsumer(destination);
            PrintlnUtil.println("Consumering Messages");
            consumer.setMessageListener(this);
           

        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void onMessage(Message m) {
        // TODO Auto-generated method stub
        if (m instanceof TextMessage) {
            TextMessage message = (TextMessage) m;
            try {
                System.out.println("Message :" + message.getText());
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

以上是幾個主要類的核心代碼片段。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章