Spring整合JMS、IBM MQ傳送和接收訊息
最近才接觸到MQ,由於之前完全不知道是幹嘛用的,還是很花了一點時間研究的~先來簡單解釋一下名詞啦
一、名詞解釋
MQ
MQ(message queue)指訊息佇列,是應用程式對應用程式的通訊方法。可以利用訊息佇列暫存資料報文。
MQ的原理其實就是生產者-消費者模式。有關生產者-消費者模式的詳細解釋可以看這篇博文http://blog.csdn.net/yolanda_nuonuo/article/details/62038122
MQ是一種非同步通訊。
生產者,傳送訊息到MQ伺服器
消費者,到MQ伺服器,獲取生產者傳送的訊息(根據MQ的不同配置,可以是MQ推送訊息給消費者,或者是消費者主動從MQ獲取訊息)JMS
JMS即
JAVA訊息服務(Java Message Service),是一個Java平臺中關於面向訊息中介軟體(MOM)的API,用於在兩個應用程式之間,或分散式系統中傳送訊息,進行非同步通訊。
使用JMS 的應用程式被稱為JMS 客戶端,處理訊息路由與傳遞的訊息系統被稱為JMS Provider,而JMS 應用則是由多個JMS 客戶端和一個JMS Provider 構成的業務系統。
傳送訊息的JMS 客戶端被稱為生產者(producer),而接收訊息的JMS 客戶端則被稱為消費者(consumer)。同一JMS 客戶端既可以是生產者也可以是消費者。
所以MQ跟JMS的關係就是,我們可以通過JMS來跟MQ進行通訊。
二、具體實現
1.首先是傳送資料,新建application-send.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder ignore-unresolvable="false" location="classpath:application.properties" />
<context:annotation-config />
<!-- <context:component-scan base-package="com.*" /> -->
<bean id="ibmJmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="${ibm.mqHostUrl}" />
<property name="port" value="${ibm.mqPort}" />
<property name="CCSID" value="0000" />
<property name="queueManager" value="${ibm.mqManager}" />
<property name="channel" value="${ibm.mqChannel}" />
<property name="transportType" value="1" />
</bean>
<bean id="ibmQueueConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="ibmJmsConnectionFactory"/>
<property name="sessionCacheSize" value="10"/>
</bean>
<bean id="ibmQueue" class="com.ibm.mq.jms.MQQueue">
<property name="baseQueueName" value="${ibm.mqName}" />
</bean>
<bean id="ibmJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="ibmQueueConnectionFactory" />
<property name="defaultDestination" ref="ibmQueue" />
<property name="pubSubDomain" value="false" />
</bean>
<bean id="ibmMessageSender" class="com.yolanda.sender.MessageSenderImpl">
</bean>
</beans>
2.傳送資料的程式碼
先寫一個介面
public interface MessageSender {
public void sendMessage(String message);
}
再寫實現類
import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
@Component("ibmMessageSender")
public class MessageSenderImpl implements MessageSender {
@Resource(name="ibmJmsTemplate")
private JmsTemplate jmsTemplate;
/**
* 這裡為什麼要用final呢?是下面那個createTextMessage方法要求的...
*/
public void sendMessage(final String message) {
this.jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
3.接下來就是收資料辣,新建application-recv.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="ibmJmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="${ibm.mqHostUrl}" />
<property name="port" value="${ibm.mqPort}" />
<property name="CCSID" value="1381" />
<property name="queueManager" value="${ibm.mqManager}" />
<property name="channel" value="${ibm.mqChannel}" />
<property name="transportType" value="1" />
</bean>
<bean id="ibmQueueConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="ibmJmsConnectionFactory"/>
<property name="sessionCacheSize" value="10"/>
</bean>
<bean id="ibmQueue" class="com.ibm.mq.jms.MQQueue">
<property name="baseQueueName" value="MQTEST" />
</bean>
<bean id="queueContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="ibmQueueConnectionFactory" />
<property name="destination" ref="ibmQueue" />
<property name="pubSubDomain" value="false" />
<property name="concurrentConsumers" value="20" />
<property name="messageListener" ref="ibmTextMessageListener" />
</bean>
<!-- 非同步接收訊息處理類 -->
<bean id="ibmextMessageListener" class="com.yolanda.receive.TextMessageListener" />
</beans>
4.由於整合了Spring,又是用JMS,所以程式碼還是灰常簡單的,這裡我們只需要實現MessageListener的onMessage(),監聽一下就可以啦
package com.yolanda.receiver;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
public class TextMessageListener implements MessageListener {
@Autowired
public void onMessage(Message message) {
try {
TextMessage msg = (TextMessage) message;
String msgStr = msg.getText();
System.out.println("Receive message:" + msgStr);
} catch (Exception e) {
e
.printStackTrace();
}
}
好啦,圓滿結束啦,不用spring整合的話,程式碼還是比較繁瑣的,果然還是用比較好呢!