一、生產者java
package com.demo.helloworld; 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 JMSProducer { // 默認鏈接用戶名 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 String QUENENAME = "Jack"; public static final int SENDNUM = 10; // 鏈接工廠 static ConnectionFactory connectionFactory; // 鏈接 static Connection connection = null; // 會話 接受或者發送消息的線程 static Session session; // 消息的目的地 static Destination destination; // 消息生產者 static MessageProducer messageProducer; // 建立鏈接 static { // 實例化鏈接工廠 connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL); try { // 經過鏈接工廠獲取鏈接 connection = connectionFactory.createConnection(); // 啓動鏈接 connection.start(); // 建立session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); // 建立一個名稱爲Jack的消息隊列 destination = session.createQueue(JMSProducer.QUENENAME); // 建立消息生產者 messageProducer = session.createProducer(destination); } catch (JMSException e) { System.out.println("建立鏈接異常"); } } // 發送消息 public static void sendMessage(String message) { try { TextMessage txtMsg = session.createTextMessage(message); messageProducer.send(txtMsg); session.commit(); } catch (JMSException e) { System.out.println("消息發送異常"); } } }
二、消費者apache
package com.demo.helloworld; 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 JMSConsumer { // 默認鏈接用戶名 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 String QUENENAME = "Jack"; static ConnectionFactory connectionFactory;// 鏈接工廠 static Connection connection = null;// 鏈接 static Session session;// 會話 接受或者發送消息的線程 static Destination destination;// 消息的目的地 static MessageConsumer messageConsumer;// 消息的消費者 static { // 實例化鏈接工廠 connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL); try { // 經過鏈接工廠獲取鏈接 connection = connectionFactory.createConnection(); // 啓動鏈接 connection.start(); // 建立session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 建立一個鏈接Jack的消息隊列 destination = session.createQueue(JMSConsumer.QUENENAME); // 建立消息消費者 messageConsumer = session.createConsumer(destination); } catch (JMSException e) { System.out.println("建立鏈接異常"); } } // 消費消息 public static void consume() { try { while (true) { TextMessage textMessage = (TextMessage) messageConsumer.receive(); if (textMessage != null) { System.out.println(textMessage.getText()); } else { break; } } } catch (JMSException e) { System.out.println("消息接收異常"); } } }
三、測試session
package com.demo.helloworld; import org.junit.Test; public class JMSTest { @Test public void testProducer() { JMSProducer.sendMessage("我發送消息了哦"); } @Test public void testConsumer() { JMSConsumer.consume(); } }