您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
ActiveMQ官方下载地址:http://activemq.apache.org/download.html
下载解压后,执行bin目录下activemq.bat启动mq服务
找到lib目录并引入相关jar包
Sender消息发送者类
package com.ycj.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; public class Sender { private static final String MQ_USER = "admin"; private static final String MQ_PWD = "admin"; private static final String MQ_URL = "tcp://localhost:61616"; private static final String MQ_QUEUE_DEFAULT = "testQueue"; public static void main(String[] args) throws JMSException { // 创建JMS连接工厂 ConnectionFactory factory = new ActiveMQConnectionFactory(MQ_USER, MQ_PWD, MQ_URL); // 通过工厂获取链接并启动 Connection connection = factory.createConnection(); connection.start(); // 创建session Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 创建destination 消息目的地 Destination destination = session.createQueue(MQ_QUEUE_DEFAULT); // 根据destination初始化一个生产者 MessageProducer producer = session.createProducer(destination); // 生产者消息不持久化 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 生成消息 并发送 Message msg1 = session.createTextMessage("I'm msg1"); Message msg2 = session.createTextMessage("I'm msg2"); producer.send(msg1); producer.send(msg2); session.commit(); connection.close(); System.out.println("producer send msg done!"); } }
Receiver消息接收者类
package com.ycj.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class Receiver { private static final String MQ_USER = "admin"; private static final String MQ_PWD = "admin"; private static final String MQ_URL = "tcp://localhost:61616"; private static final String MQ_QUEUE_DEFAULT = "testQueue"; public static void main(String[] args) throws JMSException { // 创建JMS连接工厂 ConnectionFactory factory = new ActiveMQConnectionFactory(MQ_USER, MQ_PWD, MQ_URL); // 通过工厂获取链接并启动 Connection connection = factory.createConnection(); connection.start(); // 创建session Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 创建destination 消息目的地 Destination destination = session.createQueue(MQ_QUEUE_DEFAULT); // 根据destination初始化一个消费者 MessageConsumer consumer = session.createConsumer(destination); while (true) { TextMessage msg = (TextMessage) consumer.receive(1000*1000); if (null != msg) { System.out.println("message has been received:" + msg.getText()); } } } }
依次运行Receiver和Sender,控制台输出:
Sender
Receiver
打开浏览器输入http://localhost:8161/admin/index.jsp,选择Queues可以监控队列消费情况
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。