博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java ActiveMQ 示例
阅读量:6212 次
发布时间:2019-06-21

本文共 2597 字,大约阅读时间需要 8 分钟。

所需引入Jar包:

  jms-1.1.jar

  activemq-all-5.15.0.jar

 

生产者

package com.mousewheel.demo;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.DeliveryMode;import javax.jms.Destination;import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; public class MQProducerDemo { public static void main(String[] args) throws JMSException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.31.33:61616"); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); Destination destination = session.createQueue("test-queue"); MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); MqBean bean = new MqBean(); bean.setAge(1); for (int i = 0; i < 1000; i++) { bean.setName("Jack " + i); producer.send(session.createObjectMessage(bean)); } producer.close(); System.out.println("Send finished"); } catch (Exception e) { // TODO: handle exception } finally { if (null != connection) { connection.close(); } } } }

 

消费者

package com.mousewheel.demo;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; public class MQConsumerDemo { public static void main(String[] args) throws JMSException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.31.33:61616"); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("test-queue"); MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { MqBean bean = (MqBean) ((ObjectMessage) message).getObject(); System.out.println(bean); if (null != message) { System.out.println("收到消息" + bean.getName()); } } catch (Exception e) { // TODO: handle exception  } } }); System.out.println("Begin listen"); } catch (Exception e) { // TODO: handle exception  } } }

 

转载于:https://www.cnblogs.com/mousewheel/p/7523555.html

你可能感兴趣的文章
常用软件整理
查看>>
磁盘超过2T无法用fdisk分区的问题
查看>>
scala特点和java的异同点
查看>>
VirtualBox中三维软件的libgl错误解决
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
#内存管理的艺术# 之 Nginx slab的实现 --- 第四篇“基于块的内存释放”
查看>>
linux下select函数详解及实例
查看>>
关于IE浏览器缓存的处理
查看>>
centos通过screen命令恢复xshell
查看>>
阿里联袂SMG,共同打造中式华尔街日报
查看>>
tr命令详解
查看>>
基于DRBD构建高可用主从MySQL服务器
查看>>
Python之匿名函数
查看>>
5-puppet软件包管理
查看>>
Linux下使用l7-filter过滤QQ、迅雷、电驴等
查看>>
总目录
查看>>
开源的感觉可用的 web 下拉组件
查看>>
NFS服务配置笔记
查看>>
Oracle ERP 技术探讨
查看>>