1. 程式人生 > >Spring JMS與ActiveMQ通訊示列

Spring JMS與ActiveMQ通訊示列

首先是spr-mq.xml的配置

<!-- 配置JMS連線工廠 -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="failover:(tcp://${sys.mq})"/>
</bean>
//如果你需要多種型別的資料放入各自的佇列,則再按下面佇列的定義再定義佇列則可。下面的bean的命名不同即可。即就是定義多個佇列。
<!-- 定義你的佇列 start  -->
<!--設定訊息佇列的名字,即佇列的目的地--> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="queueDestination" /> </bean> <!-- Spring提供的JMS工具類,它可以進行訊息傳送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"
> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="queueDestination" /> <property name="receiveTimeout" value="10000" /> </bean> <!-- queueDestinationGetDeviceOperate 訊息生成者 --> <bean id="mqProducerService"
class="com.coocaa.activemq.MqProducerService"> <property name="jmsTemplate" ref="jmsTemplate" /> </bean> <!-- 訊息監聽者 --> <bean id="queueMessageListener" class="com.web.queque.QueueMessageListener" /> <!-- 訊息監聽容器 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="queueMessageListener" /> </bean> <!-- 定義你的佇列end -->

傳送訊息:

在你需要傳送訊息的類裡面注入:

@Resource(name = "mqProducerService")
MqProducerService mqProducerService;
@Resource(name = "queueDestination")
Destination queueDestination;

需要傳送的資料:

Map<String,Object> map = new HashMap<String, Object>();
map.put("param", param);
mqProducerService.sendMessage(queueDestination, JSONObject.fromObject(map).toString());

MqProducerService.java:
 
public class MqProducerService {
 
  private JmsTemplate jmsTemplate;
/**
   * 訊息生產者程式碼
* 向指定佇列傳送訊息
*/
public void sendMessage(Destination destination, final String msg) {
    System.out.println("向佇列" + destination.toString() + "傳送了訊息------------" + msg);
jmsTemplate.send(destination, new MessageCreator() {
      public Message createMessage(Session session) throws JMSException {
        return session.createTextMessage(msg);
}
    });
}
 
/**
 * 向預設佇列傳送訊息
*/
public void sendMessage(final String msg) {
    String destination =  jmsTemplate.getDefaultDestination().toString();
System.out.println("向佇列" +destination+ "傳送了訊息------------" + msg);
jmsTemplate.send(new MessageCreator() {
      public Message createMessage(Session session) throws JMSException {
        return session.createTextMessage(msg);
}
    });
}
 
  public void setJmsTemplate(JmsTemplate jmsTemplate) {
    this.jmsTemplate = jmsTemplate;
}
  
}

監聽器接受佇列資料:

QueueMessageListener.java
public class QueueMessageListener implements MessageListener{
   @Override
public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("QueueMessageListenerGetDeviceOperate 收到了文字訊息:"+textMessage.getText());
JSONObject jsonObject = JSONObject.fromObject(textMessage.getText());
Map<String ,Object> map = JSONObject.fromObject(jsonObject);
String param = map.get("param").toString();
JSONObject jsonObjectParam = JSONObject.fromObject(param);
            try {
                //你需要處理的邏輯}catch (Exception e){
                System.out.println("QueueMessageListenerGetDeviceOperate 出錯:"+e.getMessage());
e.printStackTrace();
}

        } catch (JMSException e) {
            e.printStackTrace();
}


    }
}