ActiveMQ官方下載地址:http://activemq.apache.org/download.html
創(chuàng)新互聯(lián)主營定西網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),定西h5小程序開發(fā)搭建,定西網(wǎng)站營銷推廣歡迎定西等地區(qū)企業(yè)咨詢
下載解壓后,執(zhí)行bin目錄下activemq.bat啟動(dòng)mq服務(wù)
找到lib目錄并引入相關(guān)jar包
Sender消息發(fā)送者類
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 { // 創(chuàng)建JMS連接工廠 ConnectionFactory factory = new ActiveMQConnectionFactory(MQ_USER, MQ_PWD, MQ_URL); // 通過工廠獲取鏈接并啟動(dòng) Connection connection = factory.createConnection(); connection.start(); // 創(chuàng)建session Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 創(chuàng)建destination 消息目的地 Destination destination = session.createQueue(MQ_QUEUE_DEFAULT); // 根據(jù)destination初始化一個(gè)生產(chǎn)者 MessageProducer producer = session.createProducer(destination); // 生產(chǎn)者消息不持久化 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 生成消息 并發(fā)送 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 { // 創(chuàng)建JMS連接工廠 ConnectionFactory factory = new ActiveMQConnectionFactory(MQ_USER, MQ_PWD, MQ_URL); // 通過工廠獲取鏈接并啟動(dòng) Connection connection = factory.createConnection(); connection.start(); // 創(chuàng)建session Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 創(chuàng)建destination 消息目的地 Destination destination = session.createQueue(MQ_QUEUE_DEFAULT); // 根據(jù)destination初始化一個(gè)消費(fèi)者 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()); } } } }
依次運(yùn)行Receiver和Sender,控制臺(tái)輸出:
Sender
Receiver
打開瀏覽器輸入http://localhost:8161/admin/index.jsp,選擇Queues可以監(jiān)控隊(duì)列消費(fèi)情況