消息队列Active mq在Maven项目中的简单使用

来源:互联网 发布:光缆资源软件 编辑:程序博客网 时间:2024/05/22 06:21

1,ActiveMQ安装

下载地址:http://activemq.apache.org/download.html


我下载的是windows版本,5.15.0

下载完成解压缩就可以了,然后双击文件apache-activemq-5.14.3\bin\win64\activemq.bat即可

然后访问地址:http://127.0.0.1:8161/admin/ 用户名/密码:admin/admin

可以看到此时消息队列的内容为空:


2,编写程序,生产者和消费者

Maven项目中引入jar

<dependency>  <groupId>org.apache.activemq</groupId>  <artifactId>activemq-client</artifactId>  <version>5.15.0</version></dependency>

编写生产者:这里创建了名为“hello”的消息队列,并向消息队列发送10条消息。

import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by CY on 2017/8/31. */public class JMSProducer {    //默认连接用户名    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;    //默认连接密码    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;    //默认连接地址    private static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;    public static void main(String[] args) {        //连接工厂        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);        try {            //连接            Connection connection = connectionFactory.createConnection();            //启动连接            connection.start();            //创建session            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);            //消息目的地            Destination destination = session.createQueue("hello");            //消息生产者            MessageProducer producer = session.createProducer(destination);            //设置不持久化,此处学习,实际根据项目决定            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);            //发送消息            for (int i = 0; i < 10; i++) {                //创建一条文本消息                TextMessage message = session.createTextMessage("ActiveMQ啦啦啦:这是第 " + i + " 条消息");                //生产者发送消息                producer.send(message);            }            session.commit();            session.close();            connection.close();        } catch (JMSException e) {            e.printStackTrace();        }    }}
编写消费者:(这里指向名为“hello”的消息队列,并向消息队列一直取消息出来。

import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * 消息消费者 */public class JMSConsumer {    //默认连接用户名    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;    //默认连接密码    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;    //默认连接地址    private static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;    public static void main(String[] args) {        //连接工厂        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);        try {            //连接            Connection connection = connectionFactory.createConnection();            //启动连接            connection.start();            //创建session            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);            //消息目的地            Destination destination = session.createQueue("hello");            //消息消费者            MessageConsumer consumer = session.createConsumer(destination);            while (true) {                TextMessage message = (TextMessage) consumer.receive();                if (message != null) {                    System.out.println("接收到消息: " + message.getText());                } else {                    break;                }            }            session.close();            connection.close();        } catch (JMSException e) {            e.printStackTrace();        }    }}
运行代码进行测试:

首先运行生产者:(可以看到有10条消息进入队列)


然后运行消费者:(可以看到10条消息出队列)



现在我们进行第二项测试:

同时启动两个消费者,然后再启动生产者。我们会发现两个消费者都消费了5条信息,这说明一条信息不能被两个消费者同时消费。

现在我们想要两个消费者都能消费10条信息,只需修改一行代码即可:

Destination destination = session.createTopic("hello");
效果如下图所示:



参考:http://www.linuxidc.com/Linux/2017-03/142053.htm


原创粉丝点击