activemq发布者/订阅模式模式

来源:互联网 发布:淘宝白底图片制作教程 编辑:程序博客网 时间:2024/05/16 00:55

消息中间件(JMS)学习教程:http://blog.csdn.net/u014803081/article/details/73741052
centos下安装服务:http://blog.csdn.net/u014803081/article/details/73732375
activemq点对点模式:http://blog.csdn.net/u014803081/article/details/73741313
activemq发布者/订阅模式模式:http://blog.csdn.net/u014803081/article/details/73741413
activemq+springmvc1:http://blog.csdn.net/u014803081/article/details/73742071
activemq+springmvc2:http://blog.csdn.net/u014803081/article/details/73743300

发布者/订阅模式模式是消息生产者生产的一条消息会发送给每一个消费者(消费者一定要启动才能回去)。

首先maven依赖包

<dependency>    <groupId>org.apache.activemq</groupId>    <artifactId>activemq-all</artifactId>    <version>5.11.1</version></dependency>

生产者代码

import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * 消息生产者-消息发布者 * @author Administrator * */public class TopicProducer {    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(TopicProducer.USERNAME, TopicProducer.PASSWORD, TopicProducer.BROKEURL);        try {            connection=connectionFactory.createConnection(); // 通过连接工厂获取连接            connection.start(); // 启动连接            session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 创建Session            destination=session.createTopic("myTopic");            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< TopicProducer.SENDNUM; i++){            TextMessage message=session.createTextMessage("ActiveMQ 发送的消息"+i);            System.out.println("发送消息:"+"ActiveMQ 发布的消息"+i);            messageProducer.send(message);        }    }}

消费者和点对点一样,两种方式
这里直接说常用的第二中,监听的方式(这个模式,我们定义两个消费者):
消费者1:

import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * 消息消费者-消息订阅者一 * @author Administrator * */public class TopicConsumer {    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(TopicConsumer.USERNAME, TopicConsumer.PASSWORD, TopicConsumer.BROKEURL);        try {            connection=connectionFactory.createConnection();  // 通过连接工厂获取连接            connection.start(); // 启动连接            session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 创建Session            // destination=session.createQueue("FirstQueue1");  // 创建连接的消息队列            destination=session.createTopic("myTopic");            messageConsumer=session.createConsumer(destination); // 创建消息消费者            messageConsumer.setMessageListener(new TopicListener()); // 注册消息监听        } catch (JMSException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

消费者2

import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * 消息消费者-消息订阅者二 * @author Administrator * */public class TopicConsumer2 {    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(TopicConsumer2.USERNAME, TopicConsumer2.PASSWORD, TopicConsumer2.BROKEURL);        try {            connection=connectionFactory.createConnection();  // 通过连接工厂获取连接            connection.start(); // 启动连接            session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 创建Session            // destination=session.createQueue("FirstQueue1");  // 创建连接的消息队列            destination=session.createTopic("myTopic");            messageConsumer=session.createConsumer(destination); // 创建消息消费者            messageConsumer.setMessageListener(new TopicListener2()); // 注册消息监听        } catch (JMSException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

监听1

import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;/** * 消息监听-订阅者一 * @author Administrator * */public class TopicListener 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();        }    }}

监听2

import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;/** * 消息监听-订阅者二 * @author Administrator * */public class TopicListener2 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();        }    }}

先启动两个消费者(想要获取消息,必须先启动),topic
这里写图片描述

启动生产者
这里写图片描述

原创粉丝点击