【ActiveMQ】Part3 - 发布/订阅消息传递模型(Publish/Subscribe)

回顾

JMS入门学习
什么是消息队列
点对点消息传送模型

发布订阅模式

发布者发送到topic的消息,只有订阅了topic的订阅者才会收到消息。topic实现了发布和订阅,当你发布一个消息,所有订阅这个topic的服务都能得到这个消息,所以从1到N个订阅者都能得到这个消息的拷贝。
在这里插入图片描述

简单例子

session.createTopic("hello");

消费者

package com.example.topic;

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

@SuppressWarnings("all")
public class ConsumerDemo {
    public void reciveMessage() {
        ConnectionFactory factory = null;
        Connection connection = null;
        Destination destination = null;
        Session session = null;
        MessageConsumer consumer = null;
        Message message = null;

        try {
            factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://127.0.0.1:61616");
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic("hello");
            consumer = session.createConsumer(destination);
            while (true){
                // 设置接受消息的时间为1分钟
                message = consumer.receive(60000);
                if (null != message){
                    String str = ((TextMessage) message).getText();
                    System.out.println(str);
                }else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (consumer!=null){consumer.close();}
                if (session!=null){session.close();}
                if (connection!=null){connection.close();}
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ConsumerDemo demo = new ConsumerDemo();
        demo.reciveMessage();
    }
}

生产者

package com.example.topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

@SuppressWarnings("all")
public class ProducerDemo {
    public void sendMessage(String msg) {
        ActiveMQConnectionFactory factory = null;
        Connection connection = null;
        Destination destination = null;
        Session session = null;
        MessageProducer producer = null;
        Message message = null;

        try {
            factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            destination = session.createTopic("hello");
            producer = session.createProducer(destination);
            message = session.createTextMessage(msg);
            producer.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (producer!=null){producer.close();}
                if (session!=null){session.close();}
                if (connection!=null){connection.close();}
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ProducerDemo demo = new ProducerDemo();
        demo.sendMessage("测试");
    }
}

测试

先跑几个消费者实例
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
再跑一个生产者的实例
在这里插入图片描述
都接收到了消息…
(代码里设置的接收消息的时间为60秒)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值