ActiveMQ简易入门-创新互联
ActiveMQ官方下载地址:http://activemq.apache.org/download.html
10多年的咸丰网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整咸丰建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“咸丰网站设计”,“咸丰网站推广”以来,每个客户项目都认真落实执行。下载解压后,执行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可以监控队列消费情况
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻名称:ActiveMQ简易入门-创新互联
网页链接:http://pcwzsj.com/article/dsgghi.html