去官方網站下載:http://activemq.apache.org/html
將apache-activemq-5.11.1-bin.zip解壓,因爲本系統是32位,因此進入apache-activemq-5.11.1\bin\win32目錄。java
不過時間,若是你沒有設置JAVA_HOME環境變量,會出錯,因此,最好是創建一個JAVA_HOME環境變量,其值設置爲C:\Program Files\Java\jdk1.8.0(該路徑爲jdk的安裝路徑)。apache
JMS(Java Message Service) 即Java消息服務。它提供標準的產生、發送、接收消息的接口簡化企業應用的開發。它支持兩種消息通訊模型:點到點(point-to-point) (P2P)模型和發佈/訂閱(Pub/Sub)模型。P2P模型規定了一個消息只能有一個接收者;Pub/Sub 模型容許一個消息能夠有多個接收者。
對於點到點模型,消息生產者產生一個消息後,把這個消息發送到一個Queue(隊列)中,而後消息接收者再從這個Queue中讀取,一旦這個消息被一個接收者讀取以後,它就在這個Queue中消失了,因此一個消息只能被一個接收者消費。編程
與點到點模型不一樣,發佈/訂閱模型中,消息生產者產生一個消息後,把這個消息發送到一個Topic中,這個Topic能夠同時有多個接收者在監聽,當一個消息到達這個Topic以後,全部消息接收者都會收到這個消息。session
(1)建立鏈接使用的工廠類JMS ConnectionFactory
(2)使用管理對象JMS ConnectionFactory創建鏈接Connection
(3)使用鏈接Connection 創建會話Session
(4)使用會話Session和管理對象Destination建立消息生產者MessageSender
(5)使用消息生產者MessageProducer發送消息app
(1)建立鏈接使用的工廠類JMS ConnectionFactory
(2)使用管理對象JMS ConnectionFactory創建鏈接Connection
(3)使用鏈接Connection 創建會話Session
(4)使用會話Session和管理對象Destination建立消息消費者MessageReceiver
(5)使用消息消費者MessageConsumer接受消息tcp
整個工程需引入activemq-all-5.11.1.jar做爲類庫文件。其次創建JmsSender.java與JmsReceiver.java兩個文件模擬消息產生者向JMS發送消息和消息消費者從JMS接受消息。學習
JmsSender.java文件測試
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package testactivemq; import javax.jms.*; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * * @author LIN NP */ public class JmsSender { private ConnectionFactory connectionFactory = null; private Connection connection = null; private Session session = null; private Destination destination = null; private MessageProducer producer = null; private static final int SEND_NUMBER = 5; /** * */ public void init() { // 構造ConnectionFactory實例對象,此處採用ActiveMq的實現jar connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616"); // ActiveMQ默認使用的TCP鏈接端口是61616 try{ // 構造從工廠獲得鏈接對象 connection = connectionFactory.createConnection(); connection.start(); // 獲取操做鏈接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); /** * 第一種方式:Queue */ // destination = session.createQueue("xkey"); // "xkey"能夠取其餘的。 // producer = session.createProducer(destination); // 獲得消息生成者【發送者】 /** * 第二種方式:Topic */ Topic topic = session.createTopic("xkey.Topic"); producer = session.createProducer(topic); /** * */ // 設置不持久化,此處學習,實際根據項目決定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 構造消息,此處寫死,項目就是參數,或者方法獲取 sendMessage(session,producer); session.commit(); } catch(Exception e) { e.printStackTrace(); } finally { try { connection.close(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void sendMessage(Session session,MessageProducer producer) throws JMSException { for (int i = 1; i <= SEND_NUMBER; i ++) { TextMessage message = session.createTextMessage("ActiveMq 發送的消息: " + i); // 發送消息 System.out.println("發送消息:" + "ActiveMq 發送的消息: " + i); producer.send(message); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JmsSender jms = new JmsSender(); jms.init(); } }
JmsReceiver.java文件網站
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package testactivemq; import javax.jms.*; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * * @author LIN NP */ public class JmsReceiver { private ConnectionFactory connectionFactory = null; private Connection connection = null; private Session session = null; private MessageConsumer consumer = null; private Destination destination = null; public void init() { connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616"); // ActiveMQ默認使用的TCP鏈接端口是61616 try { // 構造從工廠獲得鏈接對象 connection = connectionFactory.createConnection(); connection.start(); // 獲取操做鏈接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); /** * 第一種方式:Queue */ // destination = session.createQueue("xkey"); // consumer = session.createConsumer(destination); /** * 第二種方式:Topic */ Topic topic = session.createTopic("xkey.Topic"); consumer = session.createConsumer(topic); /** * */ while (true) { //設置接收者接收消息的時間,爲了便於測試,這裏誰定爲500s TextMessage message = (TextMessage) consumer.receive(500000); if (null != message) { System.out.println("Receiver " + message.getText()); } else { break; } } } catch(Exception e) { e.printStackTrace(); } finally { try { connection.close(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JmsReceiver jms = new JmsReceiver(); jms.init(); } }
在整個過程當中,要保證activemq.bat服務是運行的。
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
發送消息:ActiveMq 發送的消息: 1
發送消息:ActiveMq 發送的消息: 2
發送消息:ActiveMq 發送的消息: 3
發送消息:ActiveMq 發送的消息: 4
發送消息:ActiveMq 發送的消息: 5
其中警告信息能夠忽略,暫時也沒去找警告來自哪裏。
run:
log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Receiver ActiveMq 發送的消息: 1
Receiver ActiveMq 發送的消息: 2
Receiver ActiveMq 發送的消息: 3
Receiver ActiveMq 發送的消息: 4
Receiver ActiveMq 發送的消息: 5
主要參考:
http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html