日期:2014-05-16 浏览次数:21093 次
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <import resource="spring-jms.xml"/> <bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> <constructor-arg> <bean class="com.viva.server.message.jms.UserRegisterListener"/> </constructor-arg> <property name="defaultListenerMethod" value="onRegister"/> <property name="messageConverter" ref="messageConvert"/> </bean> <bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory"/> <property name="destination" ref="queueDestination"/> <property name="messageListener" ref="messageListener"/> </bean> <bean id="topicListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory"/> <property name="destination" ref="topicDestination"/> <property name="messageListener" ref="messageListener"/> </bean> </beans>
public class UserRegisterListener { public void onRegister(VivaUserRegisterMessage registerMessage){ System.out.println("User Register,tel="+ registerMessage.getTel()+ ",time="+registerMessage.getRegisterTime()); } }
public class JMSTestor extends TestCase { private ApplicationContext context; public void testQueueSend(){ context = new ClassPathXmlApplicationContext("spring-jms.xml"); JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("queueTemplate"); VivaUserRegisterMessage msg = new VivaUserRegisterMessage(); msg.setRegisterTime(new Date()); msg.setTel("1385199XXXX"); msg.setUserId(1); jmsTemplate.convertAndSend(msg); } public void testTopicSend(){ context = new ClassPathXmlApplicationContext("spring-jms.xml"); JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("topicTemplate"); VivaUserRegisterMessage msg = new VivaUserRegisterMessage(); msg.setRegisterTime(new Date()); msg.setTel("1385199XXXX"); msg.setUserId(1); jmsTemplate.convertAndSend(msg); } public void testListener()throws Exception{ context = new ClassPathXmlApplicationContext("spring-listener-jms.xml"); Thread.sleep(1000*60*30); } }