ActiveMQ

来源:互联网 发布:手机淘宝网司法拍卖网 编辑:程序博客网 时间:2024/05/20 16:11

1.在apache官网下载ActiveMQ,然后解压,运行bin下面的activemq.bat

2.activeMQ的默认端口为61616,在浏览器中输入localhost:8161,出现如下图所示的界面


3.编写生产者代码

package com.activemq.producter;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by Administrator on 2017/1/19. */public class ActiveMQProducter {    //用户名    public static final String USER_NAME = ActiveMQConnection.DEFAULT_USER;    //密码    public static final String USER_PWD = ActiveMQConnection.DEFAULT_PASSWORD;    //url地址    public static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;    public static final int SEND_NUM = 10;    public static void main(String[] args) {        //连接工厂        ConnectionFactory connectionFactory;        //连接        Connection connection = null;        //session        Session session;        //消息生产者        MessageProducer messageProducer;        //消息目的地(消息队列)        Destination destination;        //创建连接工厂        connectionFactory = new ActiveMQConnectionFactory(USER_NAME,USER_PWD,BROKER_URL);        try {            //创建连接            connection = connectionFactory.createConnection();            //启动连接            connection.start();            //创建session            session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);            //创建消息发送目的地            destination = session.createQueue("test");            //创建消息发送者            messageProducer = session.createProducer(destination);            //发送消息            sendMsg(session,messageProducer);            //提交            session.commit();        } catch (JMSException e) {           e.printStackTrace();        } finally {            if(null != connection){                try {                    connection.close();                } catch (JMSException e1) {                    e1.printStackTrace();                }            }        }    }    /**     * 发送消息     * @param session     * @param messageProducer     */    private static void sendMsg(Session session, MessageProducer messageProducer) throws JMSException {        TextMessage textMessage;        for (int i = 0; i < SEND_NUM; i++) {            //创建消息文本            textMessage = session.createTextMessage("ActiveMQ 发送测试消息" + i);            System.out.println("ActiveMQ 发送测试消息" + i);            //发送消息            messageProducer.send(textMessage);        }    }}


4.编写消费者代码
package com.avtivemq.consumer;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by Administrator on 2017/1/19. */public class ActiveMQConsumer {    public static final String USER_NAME = ActiveMQConnection.DEFAULT_USER;    public static final String USER_PWD = ActiveMQConnection.DEFAULT_PASSWORD;    public static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;    public static void main(String[] args) {        ConnectionFactory connectionFactory;        Connection connection = null;        Session session;        MessageConsumer messageConsumer;        Destination destination;        connectionFactory = new ActiveMQConnectionFactory(USER_NAME,USER_PWD, BROKER_URL);        try {            connection = connectionFactory.createConnection();            connection.start();            session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);            destination = session.createQueue("test");            messageConsumer = session.createConsumer(destination);            TextMessage textMessage;            while(true){                textMessage = (TextMessage) messageConsumer.receive();                if(null != textMessage){                    System.out.println("消费消息=====>"+textMessage.getText());                }else{                    break;                }            }        } catch (JMSException e) {            e.printStackTrace();        } finally {            if(null != connection){                try {                    connection.close();                } catch (JMSException e) {                    e.printStackTrace();                }            }        }    }}


5.先启动生产者代码, 出现如下图所示

6.启动消费者代码,如下图所示

参考文献:http://blog.csdn.net/jiuqiyuliang/article/details/46701559该系列有4篇文章

                                             
0 0