activemq 话题模式(三)

生产者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.test.producermq;
 
import org.apache.activemq.ActiveMQConnectionFactory;
 
import javax.jms.*;
 
/**
 * @Title: MessageProducer
 * @ProjectName activemq
 * @date 2019/11/89:49
 */
public class MessageProducer2 {
    //定义ActivMQ的连接地址
    private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
    //定义发送消息的主题名称
    private static final String TOPIC_NAME = "MyTopicMessage";
 
    public static void main(String[] args) throws JMSException {
        //1创建连接工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2创建连接
        Connection connection = activeMQConnectionFactory.createConnection();
        //3打开连接
        connection.start();
        //4创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5创建队列目标
        Destination destination = session.createTopic(TOPIC_NAME);
        //6创建一个生产者
        javax.jms.MessageProducer producer = session.createProducer(destination);
        //创建模拟100个消息
        for (int i = 1; i <= 100; i++) {
            TextMessage message = session.createTextMessage("当前message是(主题模型):" + i);
            //发送消息
            producer.send(message);
            //在本地打印消息
            System.out.println("我现在发的消息是:" + message.getText());
        }
        //关闭连接
        connection.close();
    }
}

 消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.test.consumemq;
 
import org.apache.activemq.ActiveMQConnectionFactory;
 
import javax.jms.*;
 
/**
 * @Title: MessageConsumer
 * @ProjectName activemq
 * @date 2019/11/89:56
 */
public class MessageConsumer2 {
    //定义ActivMQ的连接地址
    private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
    //定义发送消息的队列名称
    private static final String TOPIC_NAME = "MyTopicMessage";
    public static void main(String[] args) throws JMSException {
        //1创建连接工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2创建连接
        Connection connection = activeMQConnectionFactory.createConnection();
        //3打开连接
        connection.start();
        //4创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5创建队列目标
        Destination destination = session.createTopic(TOPIC_NAME);
        //6创建消费者
        javax.jms.MessageConsumer consumer = session.createConsumer(destination);
        //创建消费的监听
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("2获取消息:" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

  如果消费者是在生产者产生消息之后来的,那么是不会对之前的消息进行消费的

       所有订阅者都会受到发布者的消息

 

 

 

posted @   qukaige  阅读(158)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示