<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.net.bysoft</groupId> <artifactId>activemqapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.14.1</version> </dependency> </dependencies> </project>
引用activemq.all.jar,最好你的類庫的版本與你的activemq版本一致。java
package cn.net.bysoft.activemqapp.test1; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * 消息的生產者,發送消息的一方 */ public class Producer { // 默認鏈接用戶名 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; // 默認鏈接密碼 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // 默認鏈接地址 private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL; // 發送的消息的次數 private static final int SENDNUM = 10; public static void main(String[] args) { // 鏈接工廠 ConnectionFactory connectionFactory; // 鏈接 Connection jms_connection = null; // 會話 接受或者發送消息的線程 Session jms_session; // 消息的目的地 Destination destination; // 消息生產者 MessageProducer messageProducer; // 實例化鏈接工廠 connectionFactory = new ActiveMQConnectionFactory(Producer.USERNAME, Producer.PASSWORD, Producer.BROKEURL); try { // 經過鏈接工廠獲取鏈接 jms_connection = connectionFactory.createConnection(); // 開始鏈接 jms_connection.start(); // 建立session jms_session = jms_connection.createSession(true, Session.AUTO_ACKNOWLEDGE); // 建立一個名稱爲HelloWorld的消息隊列 destination = jms_session.createQueue("HelloWorld"); // 建立消息生產者 messageProducer = jms_session.createProducer(destination); // 發送消息 sendMessage(jms_session, messageProducer); jms_session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (jms_connection != null) { try { jms_connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } /** * 發送消息 */ public static void sendMessage(Session session, MessageProducer messageProducer) throws Exception { for (int i = 0; i < Producer.SENDNUM; i++) { // 建立一條文本消息 TextMessage message = session.createTextMessage("ActiveMQ 發送消息" + i); System.out.println("發送消息:Activemq 發送消息" + i); // 經過消息生產者發出消息 messageProducer.send(message); } } }
該段程序主要實現了鏈接到ActiveMQ,並建立了名叫HelloWorld的消息隊列,並向該隊列發送了10條消息。當發送後,控制條會打印以下信息:apache
能夠登陸http://127.0.0.1:8161/admin/queues.jsp查看你的消息隊列:session
package cn.net.bysoft.activemqapp.test1; 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.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * 消息的消費者,接收(使用)消息的一方 */ public class Consumer { private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;// 默認鏈接用戶名 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;// 默認鏈接密碼 private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;// 默認鏈接地址 public static void main(String[] args) { ConnectionFactory connectionFactory;// 鏈接工廠 Connection connection = null;// 鏈接 Session session;// 會話 接受或者發送消息的線程 Destination destination;// 消息的目的地 MessageConsumer messageConsumer;// 消息的消費者 // 實例化鏈接工廠 connectionFactory = new ActiveMQConnectionFactory(Consumer.USERNAME, Consumer.PASSWORD, Consumer.BROKEURL); try { // 經過鏈接工廠獲取鏈接 connection = connectionFactory.createConnection(); // 啓動鏈接 connection.start(); // 建立session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 建立一個鏈接HelloWorld的消息隊列 destination = session.createQueue("HelloWorld"); // 建立消息消費者 messageConsumer = session.createConsumer(destination); while (true) { TextMessage textMessage = (TextMessage) messageConsumer.receive(100000); if (textMessage != null) { System.out.println("收到的消息:" + textMessage.getText()); } else { break; } } } catch (JMSException e) { e.printStackTrace(); } } }
上面的代碼爲消費者使用ActiveMQ消息隊列中的消息,運行後控制檯輸出:app
再一次登陸http://127.0.0.1:8161/admin/queues.jsp查看你的消息隊列:jsp
上面的例子就是點對點的消息模型發送同步消息。maven