国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ActiveMQ基本配置與示例演示

2019-11-14 23:55:37
字體:
來源:轉載
供稿:網友
ActiveMQ基本配置與示例演示一、下載ActiveMQ

去官方網站下載:http://activemq.apache.org/

二、運行ActiveMQ

將apache-activemq-5.11.1-bin.zip解壓,由于本系統是32位,所以進入apache-activemq-5.11.1/bin/win32目錄。

1、安裝InstallService.bat,如果出現下圖,也許是你電腦已經安裝過該服務。

image

2、點擊wrapper.exe,運行,如果出現下圖所示,表示已經簡單配置好了。

image

不過期間,如果你沒有設置java_HOME環境變量,會出錯,所以,最好是建立一個JAVA_HOME環境變量,其值設置為C:/PRogram Files/Java/jdk1.8.0(該路徑為jdk的安裝路徑)。

3、要想運行activemq,必須開啟activemq.bat服務,點擊它,讓其運行。 三、使用NetBeans創建項目運行 1、JMS基本概念

JMS(Java Message Service) 即Java消息服務。它提供標準的產生、發送、接收消息的接口簡化企業應用的開發。它支持兩種消息通信模型:點到點(point-to-point)(P2P)模型和發布/訂閱(Pub/Sub)模型。P2P模型規定了一個消息只能有一個接收者;Pub/Sub 模型允許一個消息可以有多個接收者。     對于點到點模型,消息生產者產生一個消息后,把這個消息發送到一個Queue(隊列)中,然后消息接收者再從這個Queue中讀取,一旦這個消息被一個接收者讀取之后,它就在這個Queue中消失了,所以一個消息只能被一個接收者消費。

  與點到點模型不同,發布/訂閱模型中,消息生產者產生一個消息后,把這個消息發送到一個Topic中,這個Topic可以同時有多個接收者在監聽,當一個消息到達這個Topic之后,所有消息接收者都會收到這個消息。

2、編程的結構 2.1消息產生者向JMS發送消息的步驟 

(1)創建連接使用的工廠類JMS ConnectionFactory  (2)使用管理對象JMS ConnectionFactory建立連接Connection  (3)使用連接Connection 建立會話session  (4)使用會話Session和管理對象Destination創建消息生產者MessageSender  (5)使用消息生產者MessageProducer發送消息

2.2消息消費者從JMS接受消息的步驟 

(1)創建連接使用的工廠類JMS ConnectionFactory  (2)使用管理對象JMS ConnectionFactory建立連接Connection  (3)使用連接Connection 建立會話Session  (4)使用會話Session和管理對象Destination創建消息消費者MessageReceiver  (5)使用消息消費者MessageConsumer接受消息

3、代碼

整個工程需引入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();    }}
4、測試過程

在整個過程中,要保證activemq.bat服務是運行的。

4.1 運行JmsReceiver.java文件,testActiveMQ (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.

4.2 運行JmsSender.java文件,testActiveMQ (run) #2控制臺會出現

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

其中警告信息可以忽略,暫時也沒去找警告來自哪里。

4.3 返回到testActiveMQ (run)控制臺會出現

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://m.survivalescaperooms.com/xwdreamer/archive/2012/02/21/2360818.html

http://www.open-open.com/lib/view/open1388994166156.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新疆| 彩票| 横山县| 京山县| 阿图什市| 宜春市| 浑源县| 万载县| 平武县| 信宜市| 龙泉市| 尼玛县| 石城县| 凌云县| 墨竹工卡县| 丰台区| 韶山市| 杭州市| 中超| 宁强县| 高淳县| 海安县| 察隅县| 昌图县| 靖边县| 高清| 乾安县| 龙海市| 鲁山县| 台山市| 旬邑县| 陆川县| 海盐县| 循化| 湘阴县| 湘西| 鹿邑县| 洛隆县| 金昌市| 鄢陵县| 宁国市|