ActiveMQ中队列模式和主题模式

博客介绍了ActiveMQ,它是基于JMS规范的消息中间件,阐述了JMS接口关系。重点对比了ActiveMQ的Queue模式和Topic模式,Queue模式按消费者请求数均摊信息,Topic模式让每个消费者接收所有信息,还指出Topic模式消费者无法订阅请求前生产者发送的消息,而Queue模式可以。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActiveMQ是什么:ActiveMQ是基于JMS规范实现的一种消息中间件。
JMS(JAVA MESSAGE SERVICE)是一种规范,API。
JMS接口间的关系如下:
工厂创建连接,连接创建会话,会话创建消息、生产者和消费者,生产者向目标对象发送信息,消费者从目标对象订阅信息。
在这里插入图片描述
下面是ActiveMQ中Queue模式和Topic模式的对比。

Queue模式

  • 生产者
public class AppQueueConsumer {
    private static final String url = "tcp://127.0.0.1:61617";
    private static final String queueName = "testQueue";

    public static void main(String[] args) throws JMSException {
        //创建工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        //创建连接
        Connection connection = connectionFactory.createConnection();
        // 启动连接
        connection.start();
        // 创建会话 false指是否在事务上处理,Session.AUTO_ACKNOWLEDGE为应答模式,这里是自动应答
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建一个目标
        Queue queue = session.createQueue(queueName);
        //创建消费者
        MessageConsumer consumer = session.createConsumer(queue);
        //创建监听器
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("接收消息:"+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

  • 消费者
public class AppQueueProducer {
    private  static  final String url="tcp://127.0.0.1:61617";
    private  static  final String queueName="testQueue";

    public static void main(String[] args) throws JMSException {
        //创建工厂
        ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(url);
        //创建连接
        Connection connection = connectionFactory.createConnection();
        // 启动连接
        connection.start();
        // 创建会话 false指是否在事务上处理,Session.AUTO_ACKNOWLEDGE为应答模式,这里是自动应答
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建一个目标
        Queue queue = session.createQueue(queueName);
        //创建生产者
        MessageProducer producer = session.createProducer(queue);

        for (int i = 0; i < 100; i++) {
        //创建消息
            TextMessage textMessage = session.createTextMessage("test:" + i);
            //发送消息
            producer.send(textMessage);
            System.out.println(" 发送消息:"+textMessage.getText());![在这里插入图片描述](https://img-blog.csdnimg.cn/20190519192111611.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzc3MDU0NQ==,size_16,color_FFFFFF,t_70)
        }
        //关闭连接
        connection.close();
    }
}
  • 运行效果:消费者我开了两个进程测试,如下:可以看到两个进程分摊了发送的信息
    在这里插入图片描述
    在这里插入图片描述
    Topic模式
    • 生产者
    public class AppTopicProducer {
     private static final String url = "tcp://127.0.0.1:61617";
     private static final String topicName = "testTopic";
    
     public static void main(String[] args) throws JMSException {
         //创建工厂
         ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(url);
         //创建连接
         Connection connection = connectionFactory.createConnection();
         // 启动连接
         connection.start();
         // 创建会话 false指是否在事务上处理,Session.AUTO_ACKNOWLEDGE为应答模式,这里是自动应答
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         //创建一个目标
         Topic topic = session.createTopic(topicName);
         //创建生产者
         MessageProducer producer = session.createProducer(topic);
    
         for (int i = 0; i < 100; i++) {
         //创建消息
             TextMessage textMessage = session.createTextMessage("test:" + i);
             //发送消息
             producer.send(textMessage);
             System.out.println(" 发送消息:"+textMessage.getText());
         }
         //关闭连接
         connection.close();
     }}
    
    • 消费者
public class AppTopicConsumer {
   private static final String url = "tcp://127.0.0.1:61617";
   private static final String topicName = "testTopic";

   public static void main(String[] args) throws JMSException {
       //创建工厂
       ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
       //创建连接
       Connection connection = connectionFactory.createConnection();
       // 启动连接
       connection.start();
       // 创建会话 false指是否在事务上处理,Session.AUTO_ACKNOWLEDGE为应答模式,这里是自动应答
       Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
       //创建一个目标
       Topic topic = session.createTopic(topicName);
       //创建消费者
       MessageConsumer consumer = session.createConsumer(topic);
       //创建监听器
       consumer.setMessageListener(new MessageListener() {
           @Override
           public void onMessage(Message message) {
               TextMessage textMessage = (TextMessage) message;
               try {
                   System.out.println("接收消息:"+textMessage.getText());
               } catch (JMSException e) {
                   e.printStackTrace();
               }
           }
       });
   }
}
  • 运行效果:消费者我开了两个进程测试,如下:可以看到两个进程均订阅所有发送的信息
    在这里插入图片描述
    在这里插入图片描述
总结:
  • Queue模式会把所有Producer提供的信息,根据Cousumer请求数进行均摊,Topic模式会返回给每个Consumer所有Producer提供的服务,你需要什么就用什么。
注意:
  • Topic模式中consumer无法订阅其请求前,生产者send的消息,Queue模式中可以。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值