Sping JMS 使用消息转换器发送或接收消息
Spring 中使用 MessageConver 抽象将 POJO 与 JMS 消息的双向转换工作 ,jmsTemplate 中的几个 converAndSend() 和 receiverAndConvert() 方法 , 这些方法为自动完成消息的转换工作 .
Spring API
Org.springframework.jms.support.converter 包中 ,Spring 提供消息转换的支持
MessageConverter 接口
Object fromMessage(Message message)
Message toMessage(Object obj,Session session)
MessageConverter 接口的目的就是向用户屏蔽 JMS 的细节 , 在 JMS 之上搭建一个隔离层 , 用户可以直接发送或接收 POJO
Spring 提供的两处简单的消息转换器
SimpleMessageConverter SimpleMessageConverter102
双向转换
Java.jms.TextMessage String
Java.jms.MapMessage java.util.Map
Java.jms.ObjectMessage java.io.Serializable
Java.jms.ByteMessage byte[] byte
如果转换出错会抛出 :MessageConverterException
MessageSender.java 使用转换器发送消息
package cn.com.snt.spring_jms_convert;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.jms.core.support.JmsGatewaySupport;
public class MessageSender extends JmsGatewaySupport {
public void sendUserMessage(User usr){
getJmsTemplate().convertAndSend(usr);
}
public void sendUserMessage1(User usr){
//getJmsTemplate().convertAndSend(destination, usr);
}
/**
* 使用 MessagePostProcessor 对
* POJO 转换为 Meesage 后进行后处理
* @param usr
*/
public void sendUserMessage2(User usr){
getJmsTemplate().convertAndSend(usr, new MessagePostProcessor (){
public Message postProcessMessage(Message message)
throws JMSException {
message.setJMSExpiration(System.currentTimeMillis ()+60*60*1000);
message.setIntProperty( "level" , 4);
return message;
}
});
}
}
MessagePostProcessor 对 Message 进行后处理
convertAndSend() 有多个方法重载
接收 POJO 消息
package cn.com.snt.spring_jms_convert;
import org.springframework.jms.core.support.JmsGatewaySupport;
/**
* 接收 POJO 消息
* @author LEPING LI
*/
public class MessageReceiver extends JmsGatewaySupport {
public void receive(){
//receiverAndConvert() 方法有多个重载 ,
// 可以指定接收消息的地址
User usr=(User)getJmsTemplate().receiveAndConvert();
System. out .println( "receive Pojo:" +usr.getUsername());
}
}
applicationContext-sender.xml
<? xml version = "1.0" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:amq = "http://activemq.org/config/1.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.org/config/1.0 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd" >
< bean id = "connectionFactory" class = "org.apache.activemq.pool.PooledConnectionFactory" >
< property name = "connectionFactory" >
< bean class = "org.apache.activemq.ActiveMQConnectionFactory" >
< property name = "brokerURL" value = "tcp://localhost:61616" />
</ bean >
</ property >
</ bean >
< bean id = "dest" class = "org.apache.activemq.command.ActiveMQQueue" >
< constructor-arg value = "usrMag" />
</ bean >
< bean id = "jmsTemplate" class = "org.springframework.jms.core.JmsTemplate" >
< property name = "connectionFactory" ref = "connectionFactory" />
< property name = "defaultDestination" ref = "dest" />
</ bean >
< bean id = "messageSender" class = "cn.com.snt.spring_jms_convert.MessageSender" >
< property name = "jmsTemplate" ref = "jmsTemplate" />
</ bean >
</ beans >
applicationContext-receiver.xml
<? xml version = "1.0" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:amq = "http://activemq.org/config/1.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.org/config/1.0 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd" >
< bean id = "connectionFactory" class = "org.apache.activemq.pool.PooledConnectionFactory" >
< property name = "connectionFactory" >
< bean class = "org.apache.activemq.ActiveMQConnectionFactory" >
< property name = "brokerURL" value = "tcp://localhost:61616" />
</ bean >
</ property >
</ bean >
< bean id = "dest" class = "org.apache.activemq.command.ActiveMQQueue" >
< constructor-arg value = "usrMag" />
</ bean >
< bean id = "jmsTemplate" class = "org.springframework.jms.core.JmsTemplate" >
< property name = "connectionFactory" ref = "connectionFactory" />
< property name = "defaultDestination" ref = "dest" />
</ bean >
< bean id = "messageReceiver" class = "cn.com.snt.spring_jms_convert.MessageReceiver" >
< property name = "jmsTemplate" ref = "jmsTemplate" />
</ bean >
</ beans >
Sender.java
package cn.com.snt.spring_jms_convert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Sender {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("cn/com/snt/spring_jms_convert/applicationContext-sender.xml");
MessageSender sender=(MessageSender)ctx.getBean("messageSender");
User usr=new User();
usr.setUsername("TOM");
usr.setUserpwd("123");
sender.sendUserMessage(usr);
}
}
Revicer.java
package cn.com.snt.spring_jms_convert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Receiver {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("cn/com/snt/spring_jms_convert/applicationContext-receiver.xml");
MessageReceiver receiver=(MessageReceiver)ctx.getBean("messageReceiver");
receiver.receive();
}
}