在Spring JMS、中可以通过实现MessageConverter自定义自己的消息转换器实现消息的转换。在Spring 的JMS1.1有SimpleMessageConverter实现基本的消息转换,在JMS1.02中SimpleMessageConverter102实现消息的转换的工作。
package com.easyway.jboss.jms.ptp.service;
public class SMS implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String message;
public SMS(){
}
public SMS(String username,String message){
this.username=username;
this.message=message;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
消息转换器:
package com.easyway.jboss.jms.ptp.spring.service;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import com.easyway.jboss.jms.ptp.service.SMS;
/**
* 自定义消息转换器MessageConverter实现
* 在JmsTemplate提供了各种send方法,供发送消息使用,其中JmsTemplate中的
* convertAndSend和receiveAndConvert方法能够借助于MessageConverter接口
* 实现消息做相应的转换。
* JMS1.1中SimpleMessageConverter实现String与TextMessage,等之间的转换
* @author longgangbai
*
*/
public class SMSConverter implements MessageConverter {
@Override
public Object fromMessage(Message message) throws JMSException,
MessageConversionException {
SMS sms=new SMS();
MapMessage mapmessage= (MapMessage )message;
sms.setMessage(mapmessage.getString("message"));
sms.setUsername(mapmessage.getString("username"));
return sms;
}
@Override
public Message toMessage(Object object, Session session)
throws JMSException, MessageConversionException {
SMS sms=(SMS)object;
MapMessage mapmessage=session.createMapMessage();
mapmessage.setString("username", sms.getUsername());
mapmessage.setString("message", sms.getMessage());
return mapmessage;
}
}
applicationContext-cjms-sender.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 设置消息服务的初始化参数信息 -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.provider.url">
jnp://localhost:1099
</prop>
<prop key="java.naming.factory.url.pkgs">
org.jboss.naming:org.jnp.interfaces
</prop>
</props>
</property>
</bean>
<!--
创建消息服务的连接工厂的信息
JMS1.02 TopicConnectionFactory 和QueueConnectionFactory
JMS1.1 TopicConnectionFactory 和QueueConnectionFactory合并为ConnectionFactory
-->
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>ConnectionFactory</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"/>
</property>
</bean>
<!-- 创建JMS的目的地信息 -->
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>queue/longgangbai</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"/>
</property>
</bean>
<!--
创建自定义转换器
-->
<bean id="smsconverter" class="com.easyway.jboss.jms.ptp.spring.service.SMSConverter"></bean>
<!-- 创建JMS发送信息的模板的对象 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="connectionFactory"/>
</property>
<property name="defaultDestination">
<ref local="destination"/>
</property>
<!-- 设置自定义转换器 -->
<property name="messageConverter">
<ref local="smsconverter"/>
</property>
</bean>
</beans>
发送消息的代码那:
package com.easyway.jboss.jms.ptp.spring.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import com.easyway.jboss.jms.ptp.service.SMS;
/**
* Spring中JMS的简单应用
* Spring JMS的创建并发送
* @author longgangbai
*
*/
public class SpringCJMSQueueSender {
private static final Log log=LogFactory.getLog(SpringCJMSQueueSender.class);
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-cjms-sender.xml");
JmsTemplate jt=(JmsTemplate)ctx.getBean("jmsTemplate");
SMS sms=new SMS();
sms.setMessage("message");
sms.setUsername("wangna");
jt.convertAndSend(sms);
log.info("发送信息成功!");
}
}
applicationContext-cjms-receive.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 设置消息服务的初始化参数信息 -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.provider.url">
jnp://localhost:1099
</prop>
<prop key="java.naming.factory.url.pkgs">
org.jboss.naming:org.jnp.interfaces
</prop>
</props>
</property>
</bean>
<!--
创建消息服务的连接工厂的信息
JMS1.02 TopicConnectionFactory 和QueueConnectionFactory
JMS1.1 TopicConnectionFactory 和QueueConnectionFactory合并为ConnectionFactory
-->
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>ConnectionFactory</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"/>
</property>
</bean>
<!-- 创建JMS的目的地信息 -->
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>queue/longgangbai</value>
</property>
<property name="jndiTemplate">
<ref local="jndiTemplate"/>
</property>
</bean>
<!--
创建自定义转换器
-->
<bean id="smsconverter" class="com.easyway.jboss.jms.ptp.spring.service.SMSConverter"></bean>
<!-- 创建JMS发送信息的模板的对象 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="connectionFactory"/>
</property>
<property name="defaultDestination">
<ref local="destination"/>
</property>
<!-- 设置自定义转换器 -->
<property name="messageConverter">
<ref local="smsconverter"/>
</property>
</bean>
</beans>
接收消息的代码如下:
package com.easyway.jboss.jms.ptp.spring.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import com.easyway.jboss.jms.ptp.service.SMS;
/**
* Spring JMS 消息的接受
* @author longgangbai
*
*/
public class SpringCJMSQueueReceive {
private static final Log log=LogFactory.getLog(SpringCJMSQueueReceive.class);
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-cjms-receive.xml");
JmsTemplate jt=(JmsTemplate)ctx.getBean("jmsTemplate");
SMS sms=(SMS)jt.receiveAndConvert();
System.out.println("username:"+sms.getUsername());
System.out.println("message:"+sms.getMessage());
System.out.println(".......");
}
}