用Websphere MQ自带的工具包通过与Spring整合向Websphere MQ发消息,并通过listener接收消息的例子
首先第一步就是配置一个websphere MQ,这个就不多说了,不会的就去找技术文章看看
然后是spring的配置文件:applicationContext.xml
然后是发消息的JMSSender.java
未完。。。
接着上面写,message sender写完了,现在剩下收消息了。应为我们在spring里配置了是通过listener来监听这个queue的,所以当消息来了,listener就会激活receive里的onmessage方法来收消息。下面是code;
JMSReceiver.java 实现了MessageListener
ok,以上就是整个的收发了。
首先第一步就是配置一个websphere MQ,这个就不多说了,不会的就去找技术文章看看
然后是spring的配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//Spring//DTD Bean//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- Application Context -->
<beans>
<bean id="jmsConnectionFactory"
class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="transportType">
<value>1</value><!-- com.ibm.mq.jms.JMSC.MQJMS_TP_CLIENT_MQ_TCPIP -->
</property>
<property name="queueManager">
<value>***</value>
</property>
<property name="hostName">
<value>***</value>
</property>
<property name="port">
<value>1414</value>
</property>
<property name="channel">
<value>***</value>
</property>
</bean>
<!-- MQ sender queue setup -->
<bean id="senderQueue" class="com.ibm.mq.jms.MQQueue">
<constructor-arg value="TEST" />
<property name="baseQueueManagerName">
<value>***</value>
</property>
<property name="baseQueueName">
<value>***</value>
</property>
</bean>
<!-- MQ receiver queue setup -->
<bean id="receiverQueue" class="com.ibm.mq.jms.MQQueue">
<constructor-arg value="TESTGUI" />
<property name="baseQueueManagerName">
<value>***</value>
</property>
<property name="baseQueueName">
<value>***</value>
</property>
</bean>
<!-- the JMSTemplate102 setup for Sender Queue-->
<bean id="SenderJMSTemplate"
class="org.springframework.jms.core.JmsTemplate102 ">
<property name="connectionFactory">
<ref bean="jmsConnectionFactory" />
</property>
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="defaultDestination">
<ref bean="senderQueue" />
</property>
</bean>
<!-- the JMSTemplate102 setup for Receiver Queue -->
<bean id="ReceiverJMSTemplate"
class="org.springframework.jms.core.JmsTemplate102 ">
<property name="connectionFactory">
<ref bean="jmsConnectionFactory" />
</property>
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="defaultDestination">
<ref bean="receiverQueue" />
</property>
</bean>
<bean id="jmsSender" class="springexample.client.JMSSender">
<property name="jmsTemplate102" ref="SenderJMSTemplate" />
<property name="replyTo" ref="senderQueue" />
<property name="msgheader">
<props>
<prop key="EVENTNAME">****</prop>
<prop key="WIRECENTER">***</prop>
<prop key="DESTINATION">***</prop>
<prop key="embusMessagingMode">***</prop>
<prop key="CLIENTID">***</prop>
<prop key="USERID">***</prop>
<prop key="GROUPID">***</prop>
<prop key="embusApplicationID">***</prop>
<prop key="embusLoggingKey">***</prop>
<prop key="USERID">***</prop>
<!-- below is for message selector -->
<prop key="messageSelector">***</prop>
</props>
</property>
</bean>
<bean id="jmsReceiver" class="springexample.client.JMSReceiver">
<property name="jmsTemplate102">
<ref bean="ReceiverJMSTemplate" />
</property>
<property name="receiveQueue">
<ref bean="receiverQueue" />
</property>
</bean>
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer102">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="receiverQueue" />
<property name="messageListener" ref="jmsReceiver" />
<property name="concurrentConsumers" value="1" />
<property name="messageSelector" value="messageSelector = '***'" />
</bean>
</beans>
然后是发消息的JMSSender.java
/*
* Created on Feb 3, 2012
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author xiaozurun
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
package springexample.client;
import java.util.Iterator;
import java.util.Properties;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessageCreator;
public class JMSSender {
private JmsTemplate102 jmsTemplate102;
private Properties msgheader;
private Destination replyTo;
public JmsTemplate102 getJmsTemplate102() {
return jmsTemplate102;
}
public void setJmsTemplate102(JmsTemplate102 jmsTemplate102) {
this.jmsTemplate102 = jmsTemplate102;
}
/**
* @return Returns the msgheader.
*/
public Properties getMsgheader() {
return msgheader;
}
/**
* @param msgheader The msgheader to set.
*/
public void setMsgheader(Properties msgheader) {
this.msgheader = msgheader;
}
/**
* @return Returns the replyTo.
*/
public Destination getReplyTo() {
return replyTo;
}
/**
* @param replyTo The replyTo to set.
*/
public void setReplyTo(Destination replyTo) {
this.replyTo = replyTo;
}
public void sendMesage() {
//System.out.println(sendQueue.getBaseQueueName());
jmsTemplate102.send(replyTo, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
String strInput = "";
try {
strInput = util.getString("facs.xml");
System.out.println("send message: " + strInput);
} catch (Exception e) {
}
Message message = session.createTextMessage(strInput);
Iterator it = msgheader.keySet().iterator();
System.out.println("Print message header property:");
while (it.hasNext()) {
String key = (String) it.next();
message.setStringProperty(key, msgheader.getProperty(key));
System.out.print("(" + key + " : "+ msgheader.getProperty(key) + ")\t");
}
message.setStringProperty("TAFIFlag", "1234567890");
message.setJMSReplyTo(replyTo);
return message;
}
});
}
}
未完。。。
接着上面写,message sender写完了,现在剩下收消息了。应为我们在spring里配置了是通过listener来监听这个queue的,所以当消息来了,listener就会激活receive里的onmessage方法来收消息。下面是code;
JMSReceiver.java 实现了MessageListener
package springexample.client;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate102;
import com.ibm.mq.jms.MQQueue;
public class JMSReceiver implements MessageListener {
private JmsTemplate102 jmsTemplate102;
private MQQueue receiveQueue;
/**
* @return Returns the jmsTemplate102.
*/
public JmsTemplate102 getJmsTemplate102() {
return jmsTemplate102;
}
/**
* @param jmsTemplate102 The jmsTemplate102 to set.
*/
public void setJmsTemplate102(JmsTemplate102 jmsTemplate102) {
this.jmsTemplate102 = jmsTemplate102;
}
/**
* @return Returns the receiveQueue.
*/
public MQQueue getReceiveQueue() {
return receiveQueue;
}
/**
* @param receiveQueue The receiveQueue to set.
*/
public void setReceiveQueue(MQQueue receiveQueue) {
this.receiveQueue = receiveQueue;
}
public void onMessage(Message message) {
try {
System.out.println("reviced msg is:" +(new java.util.Date())+":"+ ((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
public void processMessage(){
System.out.println("Start to receive message");
Message msg = jmsTemplate102.receive(receiveQueue.getBaseQueueName());
try{
TextMessage textMessage = (TextMessage) msg;
if( msg!=null){
System.out.println(" Message Received -->" + textMessage.getText());
} else {
System.out.println(" there is no message in the Queue :" + receiveQueue.getBaseQueueName());
}
}catch(Exception e){
e.printStackTrace();
}
}
}
ok,以上就是整个的收发了。