spring的jms:用spring的JmsTemplate发送和接收消息
2006-06-26 11:12
751 查看
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用的上下文:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>contextConfigLocation</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/WEB-INF/jmsconfig.xml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
jmsconfig.xml用来装配jms,内容如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsConnectionFactory"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/RequestQCF</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="destination"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/RequestQ</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsConnectionFactoryForReceive"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/ResponseQCF</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="destinationForReceive"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/ResponseQ</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="connectionFactory">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsConnectionFactory"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="defaultDestination">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="destination"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="messageConverter">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTrxConverter"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="receiveTimeout">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>1</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsTemplateForReceive" class="org.springframework.jms.core.JmsTemplate102">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="connectionFactory">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsConnectionFactoryForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="defaultDestination">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="destinationForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="messageConverter">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTrxConverter"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="receiveTimeout">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>1</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsTrxConverter" class="co.transport.jms.JmsTransactionConverter">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="rspQueue">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="destinationForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsRequestTransport" class="co.transport.jms.JmsRequestTransport">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jmsTemplate">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTemplate"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jmsTemplateForReceive">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTemplateForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其中:
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如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class JmsRequestTransport implements RequestTransport ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JmsTemplate jmsTemplate;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JmsTemplate jmsTemplateForReceive;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String messageSelector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String destinationName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setJmsTemplate(JmsTemplate template)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.jmsTemplate = template;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void request(Object request) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
jmsTemplate.convertAndSend(request);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object receive() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("in JmsRequestTransport: destinationName = " + this.jmsTemplateForReceive.getDefaultDestinationName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("in JmsRequestTransport: messageSelector = " + messageSelector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object obj = this.jmsTemplateForReceive.receiveSelectedAndConvert(this.messageSelector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getMessageSelector() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return messageSelector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setMessageSelector(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
messageSelector = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getDestinationName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return destinationName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDestinationName(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
destinationName = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public JmsTemplate getJmsTemplateForReceive() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return jmsTemplateForReceive;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setJmsTemplateForReceive(JmsTemplate template) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
jmsTemplateForReceive = template;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
要实现一个消息转换器,只需要实现MessageConverter接口,MessageConverter很简单,它只有两个方法需要实现:
public Object fromMessage(Message msg){}
public Message toMessage(Object obj, Session session) throws JMSException{}
fromMessage为接收消息后,对消息进行的转换(通常是把一个message转化为一个Object对象)
toMessage为发送消息前需要的转化(通常为把一个Object转化为一个message对象)
我的JmsTransactionConverter转换器如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class JmsTransactionConverter implements MessageConverter ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Queue rspQueue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public JmsTransactionConverter()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object fromMessage(Message msg)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBean msgBean = new MessageBean();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TextMessage massage = (TextMessage)msg;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str = massage.getText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.setHead("HeadTest");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.setOutput(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.setStatus("success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (JMSException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated catch block
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return msgBean;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Message toMessage(Object obj, Session session) throws JMSException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s = (String)obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TextMessage message = session.createTextMessage(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.setStringProperty("mytype","java");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Queue getRspQueue() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return rspQueue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setRspQueue(Queue queue) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rspQueue = queue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其中:MessageBean是一个简单javabean:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class MessageBean ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String head;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String output;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String status;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getHead() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return head;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getOutput() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return output;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getStatus() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return status;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHead(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
head = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setOutput(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setStatus(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
status = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后就可以用两个servlet来测试发送和接收消息:
发送消息:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class JMSTestServlet extends HttpServlet ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doPost(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws ServletException, IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
doGet(req, resp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doGet(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws ServletException, IOException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletOutputStream out = resp.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletContext servletContext = this.getServletContext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JmsRequestTransport transport = (JmsRequestTransport)wac.getBean("jmsRequestTransport");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("in JMSTestServlet.doGet:begin send message");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String messageToSend = req.getParameter("message");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(messageToSend == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
messageToSend = "Default Message";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transport.request(messageToSend);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s = "in JMSTestServlet.doGet:after send message and message is: " + messageToSend;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br><input type=button name=back value=back onclick=history.back()>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br><a href=receiveServlet>receive</a>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch(Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println(e.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
接收消息:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class receiveServlet extends HttpServlet ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doPost(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws ServletException, IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
doGet(req, resp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doGet(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws ServletException, IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletOutputStream out = resp.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String selector = req.getParameter("selector");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String distination = req.getParameter("distination");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s = "nothing";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletContext servletContext = this.getServletContext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JmsRequestTransport transport = (JmsRequestTransport)wac.getBean("jmsRequestTransport");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("selector = " + selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("distination = " + distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transport.setDestinationName(distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transport.setMessageSelector(selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object obj = transport.receive();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(obj != null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBean msgBean = (MessageBean)obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println("in receiveServlet:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = "receive the message is: " + msgBean.getHead() + "," +
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.getOutput() + ", " +
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.getStatus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = "there is no message where " + selector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>selector is :" + selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>distination is :" + distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br><a href=jsp/sendMessage.jsp>sendMessage</a>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch(Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>selector is :" + selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>distination is :" + distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>" + e.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
用Spring,自我感觉,可以使你的代码至少节约30%,它的很多template很不错的,比如简单封装后的jdbctemplate,jmstemplate等。
ioc用后就会爱不释手,它让你的代码风格完全改观。以前我要产生个对象,绝大多数时候只回想到new一个对象,而用spring的ioc,beanfactory,只需要配置一个xml文件即可。这种方式实现了松偶合,对单元测试非常有利。
还有它的aop,也是很不错的,
用spring的感觉,正如它的名字一样:如沐春风.
关于spring中的jms,最近一个项目用到,刚好研究了一下,在这里做一个整理备份.在此感谢同事Eric的帮助.
spring把jms进行了简单的封装,用它的JmsTemplate可以很方便的发送和接收消息,并且资源的管理都由template来管理了.
1:在web.xml文件中配置一个spring用的上下文:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>contextConfigLocation</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/WEB-INF/jmsconfig.xml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
jmsconfig.xml用来装配jms,内容如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsConnectionFactory"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/RequestQCF</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="destination"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/RequestQ</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsConnectionFactoryForReceive"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/ResponseQCF</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="destinationForReceive"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class="org.springframework.jndi.JndiObjectFactoryBean">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jndiName">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>jms/Ntelagent/ResponseQ</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="connectionFactory">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsConnectionFactory"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="defaultDestination">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="destination"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="messageConverter">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTrxConverter"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="receiveTimeout">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>1</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsTemplateForReceive" class="org.springframework.jms.core.JmsTemplate102">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="connectionFactory">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsConnectionFactoryForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="defaultDestination">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="destinationForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="messageConverter">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTrxConverter"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="receiveTimeout">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>1</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsTrxConverter" class="co.transport.jms.JmsTransactionConverter">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="rspQueue">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="destinationForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="jmsRequestTransport" class="co.transport.jms.JmsRequestTransport">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jmsTemplate">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTemplate"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="jmsTemplateForReceive">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="jmsTemplateForReceive"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其中:
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如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class JmsRequestTransport implements RequestTransport ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JmsTemplate jmsTemplate;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JmsTemplate jmsTemplateForReceive;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String messageSelector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String destinationName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setJmsTemplate(JmsTemplate template)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.jmsTemplate = template;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void request(Object request) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
jmsTemplate.convertAndSend(request);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object receive() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("in JmsRequestTransport: destinationName = " + this.jmsTemplateForReceive.getDefaultDestinationName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("in JmsRequestTransport: messageSelector = " + messageSelector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object obj = this.jmsTemplateForReceive.receiveSelectedAndConvert(this.messageSelector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getMessageSelector() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return messageSelector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setMessageSelector(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
messageSelector = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getDestinationName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return destinationName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDestinationName(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
destinationName = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public JmsTemplate getJmsTemplateForReceive() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return jmsTemplateForReceive;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setJmsTemplateForReceive(JmsTemplate template) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
jmsTemplateForReceive = template;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
要实现一个消息转换器,只需要实现MessageConverter接口,MessageConverter很简单,它只有两个方法需要实现:
public Object fromMessage(Message msg){}
public Message toMessage(Object obj, Session session) throws JMSException{}
fromMessage为接收消息后,对消息进行的转换(通常是把一个message转化为一个Object对象)
toMessage为发送消息前需要的转化(通常为把一个Object转化为一个message对象)
我的JmsTransactionConverter转换器如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class JmsTransactionConverter implements MessageConverter ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Queue rspQueue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public JmsTransactionConverter()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object fromMessage(Message msg)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBean msgBean = new MessageBean();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TextMessage massage = (TextMessage)msg;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str = massage.getText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.setHead("HeadTest");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.setOutput(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.setStatus("success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (JMSException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated catch block
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return msgBean;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Message toMessage(Object obj, Session session) throws JMSException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s = (String)obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TextMessage message = session.createTextMessage(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.setStringProperty("mytype","java");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Queue getRspQueue() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return rspQueue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setRspQueue(Queue queue) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rspQueue = queue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其中:MessageBean是一个简单javabean:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class MessageBean ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String head;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String output;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String status;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getHead() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return head;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getOutput() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return output;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getStatus() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return status;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHead(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
head = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setOutput(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
output = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setStatus(String string) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
status = string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后就可以用两个servlet来测试发送和接收消息:
发送消息:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class JMSTestServlet extends HttpServlet ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doPost(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws ServletException, IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
doGet(req, resp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doGet(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws ServletException, IOException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletOutputStream out = resp.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletContext servletContext = this.getServletContext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JmsRequestTransport transport = (JmsRequestTransport)wac.getBean("jmsRequestTransport");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("in JMSTestServlet.doGet:begin send message");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String messageToSend = req.getParameter("message");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(messageToSend == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
messageToSend = "Default Message";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transport.request(messageToSend);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s = "in JMSTestServlet.doGet:after send message and message is: " + messageToSend;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br><input type=button name=back value=back onclick=history.back()>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br><a href=receiveServlet>receive</a>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch(Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println(e.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
接收消息:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class receiveServlet extends HttpServlet ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doPost(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws ServletException, IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
doGet(req, resp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doGet(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws ServletException, IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletOutputStream out = resp.getOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String selector = req.getParameter("selector");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String distination = req.getParameter("distination");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s = "nothing";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletContext servletContext = this.getServletContext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JmsRequestTransport transport = (JmsRequestTransport)wac.getBean("jmsRequestTransport");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("selector = " + selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("distination = " + distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transport.setDestinationName(distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transport.setMessageSelector(selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object obj = transport.receive();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(obj != null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBean msgBean = (MessageBean)obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println("in receiveServlet:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = "receive the message is: " + msgBean.getHead() + "," +
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.getOutput() + ", " +
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgBean.getStatus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = "there is no message where " + selector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>selector is :" + selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>distination is :" + distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br><a href=jsp/sendMessage.jsp>sendMessage</a>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch(Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<HTML><BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>selector is :" + selector);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>distination is :" + distination);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("<br>" + e.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</HTML></BODY>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 轻量级Spring 之 JmsTemplate 接收消息和发送消息
- 轻量级Spring 之 JmsTemplate 接收消息和发送消息
- spring的jms:用spring的JmsTemplate发送和接收消息 .
- spring的jms:用spring的JmsTemplate发送和接收消息
- jms+spring+activemq配置(发送和接收消息)
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- Spring与ActiveMQ整合及用JmsTemplate发送消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- Spring JMS 异常消息接收&发送Pub/Sub域消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)