1.controller
@Controller
@RequestMapping( "/test")
public class TestController {
@Resource(name= "jmsTemplate")
private JmsTemplate jmsTemplate;
@Resource(name= "jmsTemplate2")
private JmsTemplate jmsTemplate2;
@RequestMapping(value = "/send")
public void sendmessage(){
Examination examination= new Examination();
examination.setSno( "学校");
TestMessage testMessage = new TestMessage();
jmsTemplate.send(testMessage);
jmsTemplate.convertAndSend(examination);
jmsTemplate2.convertAndSend(examination);
}
@RequestMapping( "/test")
public class TestController {
@Resource(name= "jmsTemplate")
private JmsTemplate jmsTemplate;
@Resource(name= "jmsTemplate2")
private JmsTemplate jmsTemplate2;
@RequestMapping(value = "/send")
public void sendmessage(){
Examination examination= new Examination();
examination.setSno( "学校");
TestMessage testMessage = new TestMessage();
jmsTemplate.send(testMessage);
jmsTemplate.convertAndSend(examination);
jmsTemplate2.convertAndSend(examination);
}
}
2.converter
public class ExaminationMessageConverter
implements MessageConverter {
@Override
public Object fromMessage(Message message) throws JMSException,
MessageConversionException {
if(!(message instanceof MapMessage)){
throw new MessageConversionException( "Message isn't a MapMessage");
}
MapMessage msg = (MapMessage)message;
//Examination为实体类
Examination messageDescription = new Examination();
messageDescription.setSno(msg.getString( "sno"));
return messageDescription;
}
@Override
public Message toMessage(Object object, Session session) throws JMSException,
MessageConversionException {
if(!(object instanceof Examination)){
throw new MessageConversionException( "Object isn't a Examination");
}
Examination messageDescription = (Examination)object;
MapMessage message = session.createMapMessage();
message.setString( "sno", messageDescription.getSno());
return message;
}
@Override
public Object fromMessage(Message message) throws JMSException,
MessageConversionException {
if(!(message instanceof MapMessage)){
throw new MessageConversionException( "Message isn't a MapMessage");
}
MapMessage msg = (MapMessage)message;
//Examination为实体类
Examination messageDescription = new Examination();
messageDescription.setSno(msg.getString( "sno"));
return messageDescription;
}
@Override
public Message toMessage(Object object, Session session) throws JMSException,
MessageConversionException {
if(!(object instanceof Examination)){
throw new MessageConversionException( "Object isn't a Examination");
}
Examination messageDescription = (Examination)object;
MapMessage message = session.createMapMessage();
message.setString( "sno", messageDescription.getSno());
return message;
}
}
3.entity
public class Examination {
private String sno;
public String getSno() {
return sno;
}
public void setSno( String sno) {
this. sno = sno;
}
private String sno;
public String getSno() {
return sno;
}
public void setSno( String sno) {
this. sno = sno;
}
}
4.listener
public class FilterListener
implements MessageListener {
@Autowired
private ExaminationMessageConverter examinationMessageConverter;
@Override
public void onMessage(Message msg) {
try {
Examination entity=(Examination) examinationMessageConverter.fromMessage(msg);
System. out.println( "1号监听器收到消息:"+entity.getSno());
} catch (JMSException e) {
e.printStackTrace();
}
}
@Autowired
private ExaminationMessageConverter examinationMessageConverter;
@Override
public void onMessage(Message msg) {
try {
Examination entity=(Examination) examinationMessageConverter.fromMessage(msg);
System. out.println( "1号监听器收到消息:"+entity.getSno());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
5.message
public class TestMessage
implements MessageCreator {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
message.setJMSMessageID( "101");
return message;
}
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
message.setJMSMessageID( "101");
return message;
}
}
6.spring-servlet.xml
<!-- activeMQ 配置 -->
<!-- 连接工厂 -->
< bean id ="connectionFactory" class ="org.apache.activemq.ActiveMQConnectionFactory" >
< property name ="brokerURL" value ="tcp://localhost:61616" />
< property name ="useAsyncSend" value ="true" />
</ bean >
<!-- 目的地地址 -->
< bean id ="destination" class ="org.apache.activemq.command.ActiveMQQueue" >
< constructor-arg index ="0" value ="myqueue" ></ constructor-arg >
</ bean >
<!-- jms模板 -->
< bean id ="jmsTemplate" class ="org.springframework.jms.core.JmsTemplate" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="defaultDestination" ref ="destination" ></ property >
< property name ="messageConverter" ref ="innerMessageConverter" ></ property >
</ bean >
<!-- 类型转换器 -->
< bean id ="innerMessageConverter" class ="com.yyc.base.converter.ExaminationMessageConverter" ></ bean >
<!-- 监听器 -->
< bean id ="messageListener" class ="com.yyc.base.filter.FilterListener" ></ bean >
<!-- 绑定监听器 -->
< bean id ="jmsContainer"
class ="org.springframework.jms.listener.DefaultMessageListenerContainer"
lazy-init ="false" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="destination" ref ="destination" ></ property >
< property name ="messageListener" ref ="messageListener" ></ property >
< property name ="concurrentConsumers" value ="5" ></ property >
< property name ="cacheLevel" value ="0" />
</ bean >
<!-- 配置绑定第二个监听器 -->
< bean id ="destination2" class ="org.apache.activemq.command.ActiveMQQueue" >
< constructor-arg index ="0" value ="myqueue2" ></ constructor-arg >
</ bean >
< bean id ="jmsTemplate2" class ="org.springframework.jms.core.JmsTemplate" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="defaultDestination" ref ="destination2" ></ property >
< property name ="messageConverter" ref ="innerMessageConverter" ></ property >
</ bean >
< bean id ="messageListener2" class ="com.yyc.base.filter.FilterListener2" ></ bean >
< bean id ="jmsContainer2"
class ="org.springframework.jms.listener.DefaultMessageListenerContainer"
lazy-init ="false" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="destination" ref ="destination2" ></ property >
< property name ="messageListener" ref ="messageListener2" ></ property >
< property name ="concurrentConsumers" value ="5" ></ property >
< property name ="cacheLevel" value ="0" />
<!-- 连接工厂 -->
< bean id ="connectionFactory" class ="org.apache.activemq.ActiveMQConnectionFactory" >
< property name ="brokerURL" value ="tcp://localhost:61616" />
< property name ="useAsyncSend" value ="true" />
</ bean >
<!-- 目的地地址 -->
< bean id ="destination" class ="org.apache.activemq.command.ActiveMQQueue" >
< constructor-arg index ="0" value ="myqueue" ></ constructor-arg >
</ bean >
<!-- jms模板 -->
< bean id ="jmsTemplate" class ="org.springframework.jms.core.JmsTemplate" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="defaultDestination" ref ="destination" ></ property >
< property name ="messageConverter" ref ="innerMessageConverter" ></ property >
</ bean >
<!-- 类型转换器 -->
< bean id ="innerMessageConverter" class ="com.yyc.base.converter.ExaminationMessageConverter" ></ bean >
<!-- 监听器 -->
< bean id ="messageListener" class ="com.yyc.base.filter.FilterListener" ></ bean >
<!-- 绑定监听器 -->
< bean id ="jmsContainer"
class ="org.springframework.jms.listener.DefaultMessageListenerContainer"
lazy-init ="false" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="destination" ref ="destination" ></ property >
< property name ="messageListener" ref ="messageListener" ></ property >
< property name ="concurrentConsumers" value ="5" ></ property >
< property name ="cacheLevel" value ="0" />
</ bean >
<!-- 配置绑定第二个监听器 -->
< bean id ="destination2" class ="org.apache.activemq.command.ActiveMQQueue" >
< constructor-arg index ="0" value ="myqueue2" ></ constructor-arg >
</ bean >
< bean id ="jmsTemplate2" class ="org.springframework.jms.core.JmsTemplate" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="defaultDestination" ref ="destination2" ></ property >
< property name ="messageConverter" ref ="innerMessageConverter" ></ property >
</ bean >
< bean id ="messageListener2" class ="com.yyc.base.filter.FilterListener2" ></ bean >
< bean id ="jmsContainer2"
class ="org.springframework.jms.listener.DefaultMessageListenerContainer"
lazy-init ="false" >
< property name ="connectionFactory" ref ="connectionFactory" ></ property >
< property name ="destination" ref ="destination2" ></ property >
< property name ="messageListener" ref ="messageListener2" ></ property >
< property name ="concurrentConsumers" value ="5" ></ property >
< property name ="cacheLevel" value ="0" />
</bean>
7.pom.xml
<
dependency
>
< groupId >javax.jms </ groupId >
< artifactId >jms </ artifactId >
< version >1.1 </ version >
</ dependency >
< dependency >
< groupId >org.apache.activemq </ groupId >
< artifactId >activemq-core </ artifactId >
< version >5.5.0 </ version >
</ dependency >
< dependency >
< groupId >org.apache.activemq </ groupId >
< artifactId >activemq-pool </ artifactId >
< version >5.7.0 </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >org.springframework.jms </ artifactId >
< version >3.2.5.RELEASE </ version >
< groupId >javax.jms </ groupId >
< artifactId >jms </ artifactId >
< version >1.1 </ version >
</ dependency >
< dependency >
< groupId >org.apache.activemq </ groupId >
< artifactId >activemq-core </ artifactId >
< version >5.5.0 </ version >
</ dependency >
< dependency >
< groupId >org.apache.activemq </ groupId >
< artifactId >activemq-pool </ artifactId >
< version >5.7.0 </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >org.springframework.jms </ artifactId >
< version >3.2.5.RELEASE </ version >
</dependency>
注;web项目需要添加webapp文件夹