@Bean(destroyMethod = "stop") public PooledConnectionFactory pooledConnectionFactory(){ ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616"); return new PooledConnectionFactory(activeMQConnectionFactory); }
二.注册Destination Bean(使用队列发送消息).
@Bean public ActiveMQQueue activeMQQueue(){ return new ActiveMQQueue("ExamQueueName"); }
@Bean public ExamQueueListener examQueueListener(){ return new ExamQueueListener(); }
package com.exam.jms; import javax.jms.Message; import javax.jms.MessageListener; /** * Created by nil on 2014/8/26. */ public class ExamQueueListener implements MessageListener { @Override public void onMessage(Message message) { System.out.println(message); } }
@Bean public DefaultMessageListenerContainer defaultMessageListenerContainer(){ DefaultMessageListenerContainer defaultMessageListenerContainer=new DefaultMessageListenerContainer(); defaultMessageListenerContainer.setConnectionFactory(pooledConnectionFactory()); defaultMessageListenerContainer.setDestination(activeMQQueue()); defaultMessageListenerContainer.setMessageListener(examQueueListener()); return defaultMessageListenerContainer; }
@Bean public JmsTemplate jmsTemplate(){ return new JmsTemplate(pooledConnectionFactory()); }2.将destination和jmsTemplate注入到examPublisher来注册Producer Bean.
@Bean public ExamPublisher examPublisher(){ return new ExamPublisher(jmsTemplate(),activeMQQueue()); }其中,ExamPublisher.java内容如下:
package com.exam.jms; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import java.util.Random; /** * Created by nil on 2014/8/26. */ public class ExamPublisher { private JmsTemplate jmsTemplate; private Destination destination; public ExamPublisher(){} public ExamPublisher(JmsTemplate jmsTemplate,Destination destination){ this.jmsTemplate = jmsTemplate; this.destination = destination; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void setDestination(Destination destination) { this.destination = destination; } public void sendMessage(final String msg){ jmsTemplate.send(destination,new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage("test message " + msg); } }); } }
package com.exam.jms; import com.exam.config.AppConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; import javax.annotation.Resource; /** * Created by nil on 2014/8/26. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader=AnnotationConfigContextLoader.class,classes={AppConfig.class}) public class ExamPublisherTest { @Resource private ExamPublisher examPublisher; @Test public void testSendMessage(){ examPublisher.sendMessage("x123x"); } }
WARN | Transport Connection to: tcp://127.0.0.1:65279 failed: java.net.SocketException: Connection reset
源码:http://download.csdn.net/detail/xiejx618/7822573