activemq两种实现方式
第一种:点对点
#发布者
public class Producer { private static final String userName = ActiveMQXAConnectionFactory.DEFAULT_USER; private static final String password = ActiveMQXAConnectionFactory.DEFAULT_PASSWORD; private static final String brokerURL = "tcp://192.168.178.X:61616"; public static void main(String[] args) throws JMSException { //1.创建连接工厂类 ConnectionFactory factory = new ActiveMQXAConnectionFactory(userName, password, brokerURL); //2.创建连接 Connection connection = factory.createConnection(); //3.启动连接 connection.start(); //4.创建会话对象session(事务transacted为true,参数2不生效) //acknowledgeMode: Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE); //5.目的地 Queue queue = session.createQueue("mq-test-01"); //7.创建发送者 MessageProducer producer = session.createProducer(queue); for(int i=1;i<=10;i++) { //6.消息对象 TextMessage message = session.createTextMessage(); message.setText("消息"+i); //8.发送消息 producer.send(message); } //9.会话提交 // session.commit(); //10.关闭连接 connection.close(); } }
#消费者
public class Consumer1 { private static final String userName = ActiveMQXAConnectionFactory.DEFAULT_USER; private static final String password = ActiveMQXAConnectionFactory.DEFAULT_PASSWORD; private static final String brokerURL = "tcp://192.168.178.X:61616"; public static void main(String[] args) throws JMSException { //1.创建连接工厂类 ConnectionFactory factory = new ActiveMQXAConnectionFactory(userName, password, brokerURL); //2.创建连接 Connection connection = factory.createConnection(); //3.启动连接 connection.start(); //4.创建会话对象session(事务transacted为true,参数2不生效) //acknowledgeMode: Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE); //5.目的地 Queue queue = session.createQueue("mq-test-01"); //6.接收消息对象 MessageConsumer consumer = session.createConsumer(queue); //7.通过监听器接收消息 consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage textMessage = (TextMessage)message; //获取消息 try { String msg = textMessage.getText(); System.out.println(msg); } catch (JMSException e) { } } }); } }
第二种: 发布者/订阅者
启动顺序:先订阅、再发布
#订阅者
public class Subscriber1 { private static final String userName = ActiveMQXAConnectionFactory.DEFAULT_USER; private static final String password = ActiveMQXAConnectionFactory.DEFAULT_PASSWORD; private static final String brokerURL = "tcp://192.168.129.10:61616"; public static void main(String[] args) throws JMSException { //1.创建连接工厂类 ConnectionFactory factory = new ActiveMQXAConnectionFactory(userName, password, brokerURL); //2.创建连接 Connection connection = factory.createConnection(); //3.启动连接 connection.start(); //4.创建会话对象session(事务transacted为true,参数2不生效) //acknowledgeMode: Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE); //5.目的地 Topic topic = session.createTopic("mq-test-02"); //6.接收消息对象 MessageConsumer consumer = session.createConsumer(topic); //7.通过监听器接收消息 consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage textMessage = (TextMessage)message; //获取消息 try { String msg = textMessage.getText(); System.out.println(msg); } catch (JMSException e) { } } }); } }
#发布者
public class Publisher { private static final String userName = ActiveMQXAConnectionFactory.DEFAULT_USER; private static final String password = ActiveMQXAConnectionFactory.DEFAULT_PASSWORD; private static final String brokerURL = "tcp://192.168.129.10:61616"; public static void main(String[] args) throws JMSException { //1.创建连接工厂类 ConnectionFactory factory = new ActiveMQXAConnectionFactory(userName, password, brokerURL); //2.创建连接 Connection connection = factory.createConnection(); //3.启动连接 connection.start(); //4.创建会话对象session(事务transacted为true,参数2不生效) //acknowledgeMode: Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE); //5.目的地 Topic topic = session.createTopic("mq-test-02"); //7.创建发送者 MessageProducer producer = session.createProducer(topic); for(int i=1;i<=10;i++) { //6.消息对象 TextMessage message = session.createTextMessage(); message.setText("消息"+i); //8.发送消息 producer.send(message); //设置自动持久化 producer.setDeliveryMode(DeliveryMode.PERSISTENT); } //9.会话提交 // session.commit(); //10.关闭连接 connection.close(); } }
作 者:
Jony.K.Chen
出 处:http://www.cnblogs.com/lxcy/
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)