ActiveMQ的在實際項目中的簡單使用

簡介

本文達到的目的是會用。java

安裝服務器:linux

官網下載win版的,也但是linux版的;spring

啓動服務器:win版的執行activemq.batapache

看看是否安裝成功:http://localhost:8161/json

 

activeMQ是一個消息服務器,服務器

2中模式:點對點、廣播session

點對點:消息進入隊列後,只會被接收一次,接收完了就在消息隊列裏消失了。異步

廣播:採用的是廣播訂閱的方式,全部的訂閱了此主題的,當此主題有消息時,都會接收到tcp

 

可靠性:ide

mq會將信息存儲

 

/*

                   * 消息的類型(消息體)

                   * TextMessage

                   * ObjectMessage: Java中的可序列化對象

                   * StreamMessage: Java中的輸入輸出流

                   * BytesMessage: 字節流

                   * MapMessage: /值對的集合,名是String對象,值類型能夠是Java任何基本類型

                   */

                 

                  /*

                   * 術語:

                   * ConnectionFactory:鏈接工廠,JMS 用它建立鏈接;

                   * Destination:消息的目的地;

                   * Session:會話,一個發送或接收消息的線程;

                   */

 

發送消息到消息隊列:

場景:程序中記錄日誌,異步的把日誌輸出等。

配置文件:

Spring-context.xml:

 

配置jmsConnectionFactory

<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">

//value的值就是mq服務器的地址

    <property name="brokerURL" value=" tcp://10.128.11.128:61616 " />

<property name="useAsyncSend" value="true" />//表示異步傳送,默認是false,同步

</bean>

配置pooledJmsConnectionFactory:池

<bean id="pooledJmsConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"

    destroy-method="stop">

    <property name="connectionFactory" ref="jmsConnectionFactory" />

    <property name="maxConnections" value="100" />

    <property name="maximumActive" value="50" />

  </bean>

配置jmsTemplate:模板,這是核心

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

    <property name="connectionFactory" ref="pooledJmsConnectionFactory" />

  </bean>

定義發送消息的隊列:

<bean id="sendGSMMessageQueue" class="org.apache.activemq.command.ActiveMQQueue">

    <constructor-arg value="gsm.message.push.queue" />//隊列的名字是任意寫的,只要不重複

  </bean>

定義生產者:將消息發送到這個隊列

<bean id="notificationSender" class="com.gome.gsm.jms.NotificationSender">

<property name="jmsTemplate" ref="jmsTemplate"></property>

//將消息發送到這個隊列

    <property name="queue" ref="sendGSMMessageQueue"></property>

  </bean>

 

NotificationSender.java:

就是普通的類,注入jmsTemplatequeue,而且有個send方法,並調用jmsTemplatesend方法

//這裏發送的是String型的數據

public void send(final String message){

                  this.jmsTemplate.send(queue, new MessageCreator() {

                         

                          @Override

                          public Message createMessage(Session session) throws JMSException {

                                   return session.createTextMessage(message);

                          }

                  });

                  logger.info("-------發送消息到消息服務器通知隊列成功--" + message + "-------");

}

以上,便實現了將消息發送到消息隊列。

既然有了生產者,那麼也應該有消費者,不然消息將一直阻塞。

從消息隊列接收消息

注:這裏不是在那個隊列裏接收的消息

------------------------------------分割線-----------------------------------------

進行消息的接收

配置receiveMessageQueue:接收消息隊列(是否須要定義接收消息隊列與項目需求有關,好比我就是接收我前面已經定義的消息發送隊列的消息,那麼這裏也就沒有必要定義這個隊列了)

<bean id="receiveMessageQueue" class="org.apache.activemq.command.ActiveMQQueue">

    <constructor-arg value="gsm.message.receiver.queue" />//要在這個消息隊列接收消息

  </bean>

配置消息監聽器:

<bean id="notificationListener" class="com.gome.gsm.jms.NotificationListener">

         //這裏須要根據系統具體寫,由於監聽器類裏面注入了這個資源

    <property name="notificationService" ref="notificationService" />

  </bean>

配置消息接收客戶端:

<bean id="notificationConsumer"

    class="org.springframework.jms.listener.DefaultMessageListenerContainer">

    <property name="connectionFactory" ref="pooledJmsConnectionFactory" />

    <property name="destination" ref="receiveMessageQueue" />

    <property name="messageListener" ref="notificationListener" />

  </bean>

其中bean idnotificationService的類:(項目時註解開發)

編寫監聽器類:(消息隊列裏接收消息的)

public class NotificationListener implements MessageListener {

 

         private static final Logger logger = LoggerFactory.getLogger(NotificationListener.class);

         private NotificationService notificationService;

        

         public NotificationService getNotificationService() {

                  return notificationService;

         }

 

         public void setNotificationService(NotificationService notificationService) {

                  this.notificationService = notificationService;

         }

 

         @Override

         public void onMessage(Message message) {

                  try {

                          if(message instanceof TextMessage){

                                   TextMessage receiveMessage = (TextMessage) message;

                                   String jsonStr = receiveMessage.getText();

                                   logger.info("----接收到通知消息: " + jsonStr + "--------------------");

                                   Map map = JsonUtil.jsonStringToMap(jsonStr);

                                   //根據接收到的消息具體作些事情便可。。。

                          }

                  } catch (Exception e) {

                          e.printStackTrace();

                  }

         }

}

注意:

由於項目中只用到了這種點對點的方式隊列,因此這裏只有這個。

發送的消息隊列能夠使可接受的消息隊列是一個。具體看需求。

mq就是一個消息服務器,有生產者將消息放到隊列裏面,至因而誰接收消息,誰是消費者,並不清楚;

 

參考:http://yinbinhome.iteye.com/blog/1273228

相關文章
相關標籤/搜索