基于WebSphere MQ发送消息的简单JMS实例。
MQSend.java
package com.bijian.study;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
* MQ发送消息
*/
public class MQSend {
/**
* jms模板,封装链接工厂、队列、消息生产者
*/
private JmsTemplate jmsTemplate;
public MQSend() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");
}
/**
* 发送消息
*
* @param msg消息
*/
public void sendMessage(String msg) {
// Text消息
MessageCreator messageCreator = createTxtMsg(msg);
jmsTemplate.send(messageCreator);
// Map消息
messageCreator = createMapMsg(msg);
jmsTemplate.send(messageCreator);
}
/**
* 创建text消息
*
* @param msg 消息
* @return message
*/
private MessageCreator createTxtMsg(final String msg) {
return new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText(msg);
return textMessage;
}
};
}
/**
* 创建map消息
*
* @param msg消息
* @return message
*/
private MessageCreator createMapMsg(final String msg) {
return new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("map", "javax.jms.MapMessage");
mapMessage.setString("map1", msg);
return mapMessage;
}
};
}
/**
* @param args
*/
public static void main(String[] args) {
MQSend send = new MQSend();
send.sendMessage("start, send!");
System.out.println("发送完成!");
}
}
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- ConnectionFactory --> <bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQConnectionFactory"> <property name="connectionNameList" value="10.36.68.88(1414)" /> <property name="CCSID" value="1051"/> <property name="queueManager" value="QmgrName"/> <property name="channel" value="DC.SVRCONN"/> <property name="transportType" value="1"/> </bean> <!-- jmsTemplate --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsConnectionFactory"/> <property name="defaultDestination" ref="requestQueue" /> </bean> <bean id="requestQueue" class="com.ibm.mq.jms.MQQueue"> <property name="baseQueueName" value="LOCALQ"></property> </bean> </beans>
完整实例见附件《JMSMQDemo.zip》,在运行前请到\WebSphere MQ\java\lib目录下将如下jar包拷贝到工程的lib目录中。