activemq 队列模式(二)
创建一个maven项目 (quick即可)
1 2 3 4 5 6 | <!--添加activemq的依赖--> < dependency > < groupId >org.apache.activemq</ groupId > < artifactId >activemq-all</ artifactId > < version >5.9.0</ version > </ dependency > |
创建生产者:
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.*; import javax.jms.*; /** * @Title: MessageProducer * @ProjectName activemq * @date 2019/11/89:49 */ public class MessageProducer { //定义ActivMQ的连接地址 private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616" ; //定义发送消息的队列名称 private static final String QUEUE_NAME = "MyMessage" ; 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.createQueue(QUEUE_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 MessageConsumer { //定义ActivMQ的连接地址 private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616" ; //定义发送消息的队列名称 private static final String QUEUE_NAME = "MyMessage" ; 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.createQueue(QUEUE_NAME); // 6.创建消费者 javax.jms.MessageConsumer consumer = session.createConsumer(destination); // 7.创建消费的监听 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(); } } }); } } |
运行之后可以看见:
消息已被消费
队列模式的消息,是只会被一个消费者所使用的,而不会被共享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义