spring的jms:用spring的JmsTemplate发送和接收消息

Spring是一个完整的J2EE框架和解决方案,完整并且模块化,你可以用的他的全部也可以只用他的一部分特性,Spring 包含 IoC, AOP, 事务处理,ORM, 也包括一个简单的MVC框架.
用Spring,自我感觉,可以使你的代码至少节约30%,它的很多template很不错的,比如简单封装后的jdbctemplate,jmstemplate等。
ioc用后就会爱不释手,它让你的代码风格完全改观。以前我要产生个对象,绝大多数时候只回想到new一个对象,而用spring的ioc,beanfactory,只需要配置一个xml文件即可。这种方式实现了松偶合,对单元测试非常有利。
还有它的aop,也是很不错的,
用spring的感觉,正如它的名字一样:如沐春风.
关于spring中的jms,最近一个项目用到,刚好研究了一下,在这里做一个整理备份.在此感谢同事Eric的帮助.
spring把jms进行了简单的封装,用它的JmsTemplate可以很方便的发送和接收消息,并且资源的管理都由template来管理了.
1:在web.xml文件中配置一个spring用的上下文:

< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value >
/WEB-INF/jmsconfig.xml
</ param-value >
</ context-param >

jmsconfig.xml用来装配jms,内容如下:

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEbeansPUBLIC"-//SPRING/DTDBEAN/EN""http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="jmsConnectionFactory"
class
="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" >
< value > jms/Ntelagent/RequestQCF </ value >
</ property >
</ bean >
< bean id ="destination"
class
="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" >
< value > jms/Ntelagent/RequestQ </ value >
</ property >
</ bean >

< bean id ="jmsConnectionFactoryForReceive"
class
="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" >
< value > jms/Ntelagent/ResponseQCF </ value >
</ property >
</ bean >
< bean id ="destinationForReceive"
class
="org.springframework.jndi.JndiObjectFactoryBean" >
< property name ="jndiName" >
< value > jms/Ntelagent/ResponseQ </ value >
</ property >
</ bean >


< bean id ="jmsTemplate" class ="org.springframework.jms.core.JmsTemplate102" >
< property name ="connectionFactory" >
< ref bean ="jmsConnectionFactory" />
</ property >
< property name ="defaultDestination" >
< ref bean ="destination" />
</ property >
< property name ="messageConverter" >
< ref bean ="jmsTrxConverter" />
</ property >
< property name ="receiveTimeout" >
< value > 1 </ value >
</ property >
</ bean >

< bean id ="jmsTemplateForReceive" class ="org.springframework.jms.core.JmsTemplate102" >
< property name ="connectionFactory" >
< ref bean ="jmsConnectionFactoryForReceive" />
</ property >
< property name ="defaultDestination" >
< ref bean ="destinationForReceive" />
</ property >
< property name ="messageConverter" >
< ref bean ="jmsTrxConverter" />
</ property >
< property name ="receiveTimeout" >
< value > 1 </ value >
</ property >
</ bean >

< bean id ="jmsTrxConverter" class ="co.transport.jms.JmsTransactionConverter" >
< property name ="rspQueue" >
< ref bean ="destinationForReceive" />
</ property >
</ bean >

< bean id ="jmsRequestTransport" class ="co.transport.jms.JmsRequestTransport" >
< property name ="jmsTemplate" >
< ref bean ="jmsTemplate" />
</ property >
< property name ="jmsTemplateForReceive" >
< ref bean ="jmsTemplateForReceive" />
</ property >
</ bean >

</ beans >

其中:
jmsConnectionFactory为jms连接工厂,属性jndiName的value为server服务配置的jndi名称.
destination为消息队列,属性jndiName为消息队列的jndi名称.
jmsTemplate为配置spring消息模版:采用JmsTemplate102(如果你的JMS实现符合JMS规范1.1版,则应该使用JmsTemplate),其中的messageConverter属性配置了一个消息转换器,因为通常消息在发送前和接收后都需要进行一个前置和后置处理,转换器便进行这个工作。
由于我的sendQueue很receiveRueue是不同的queue,所以我配置了两个jmsTemplate:

jmsTemplateForReceive负责接收消息,jmsTemplate负责发发送消息.


发送消息的代码很简单:
jmsTemplate.convertAndSend(request);
接收消息也很简单:
Object obj = jmsTemplate.receiveAndConvert();
如果需要用一个过滤器接收特定的消息,则:
Object obj = this.jmsTemplateForReceive.receiveSelectedAndConvert(this.messageSelector);

发送和接收消息的class如下:



public class JmsRequestTransport implements RequestTransport ... {
privateJmsTemplatejmsTemplate;

privateJmsTemplatejmsTemplateForReceive;

privateStringmessageSelector;

privateStringdestinationName;

publicvoidsetJmsTemplate(JmsTemplatetemplate)...{
this.jmsTemplate=template;
}


publicvoidrequest(Objectrequest)...{
jmsTemplate.convertAndSend(request);
}


publicObjectreceive()...{
System.out.println(
"inJmsRequestTransport:destinationName="+this.jmsTemplateForReceive.getDefaultDestinationName());
System.out.println(
"inJmsRequestTransport:messageSelector="+messageSelector);
Objectobj
=this.jmsTemplateForReceive.receiveSelectedAndConvert(this.messageSelector);
returnobj;
}


publicStringgetMessageSelector()...{
returnmessageSelector;
}


publicvoidsetMessageSelector(Stringstring)...{
messageSelector
=string;
}


publicStringgetDestinationName()...{
returndestinationName;
}


publicvoidsetDestinationName(Stringstring)...{
destinationName
=string;
}


publicJmsTemplategetJmsTemplateForReceive()...{
returnjmsTemplateForReceive;
}


publicvoidsetJmsTemplateForReceive(JmsTemplatetemplate)...{
jmsTemplateForReceive
=template;
}


}


要实现一个消息转换器,只需要实现MessageConverter接口,MessageConverter很简单,它只有两个方法需要实现:
public Object fromMessage(Message msg){}
public Message toMessage(Object obj, Session session) throws JMSException{}
fromMessage为接收消息后,对消息进行的转换(通常是把一个message转化为一个Object对象)
toMessage为发送消息前需要的转化(通常为把一个Object转化为一个message对象)
我的JmsTransactionConverter转换器如下:

public class JmsTransactionConverter implements MessageConverter ... {

privateQueuerspQueue;

publicJmsTransactionConverter()...{}

publicObjectfromMessage(Messagemsg)...{
MessageBeanmsgBean
=newMessageBean();
TextMessagemassage
=(TextMessage)msg;

try...{
Stringstr
=massage.getText();
msgBean.setHead(
"HeadTest");
msgBean.setOutput(str);
msgBean.setStatus(
"success");

}
catch(JMSExceptione)...{
//TODOAuto-generatedcatchblock
msgBean=null;
e.printStackTrace();
}

finally...{
returnmsgBean;
}


}


publicMessagetoMessage(Objectobj,Sessionsession)throwsJMSException...{

Strings
=(String)obj;

TextMessagemessage
=session.createTextMessage(s);
message.setStringProperty(
"mytype","java");
returnmessage;
}


publicQueuegetRspQueue()...{
returnrspQueue;
}


publicvoidsetRspQueue(Queuequeue)...{
rspQueue
=queue;
}


}


其中:MessageBean是一个简单javabean:

public class MessageBean ... {

privateStringhead;

privateStringoutput;

privateStringstatus;

publicStringgetHead()...{
returnhead;
}


publicStringgetOutput()...{
returnoutput;
}


publicStringgetStatus()...{
returnstatus;
}



publicvoidsetHead(Stringstring)...{
head
=string;
}


publicvoidsetOutput(Stringstring)...{
output
=string;
}
<spa
分享到:
评论

你可能感兴趣的:(spring,bean,jms,配置管理,IOC)