第一步:创建Maven项目,添加依赖:
junit
junit
4.12
org.projectlombok
lombok
1.18.10
org.apache.activemq
activemq-all
5.15.12
org.apache.activemq
activemq-pool
5.15.12
org.springframework
spring-jms
5.2.5.RELEASE
org.springframework
spring-context
5.2.5.RELEASE
org.springframework
spring-test
5.2.5.RELEASE
第二步:创建Producer
@Service
public class JmsProducer {
@Autowired
private JmsTemplate jmsTemplate;
public void produce(ApplicationContext act) {//注意此处使用Spring单元测试会报错
JmsProducer producer = (JmsProducer) act.getBean("jmsProducer");
producer.jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("spring整合ActiveMQ");
return textMessage;
}
});
System.out.println("produce 发布消息 ...");
}
}
第三步:创建Consumer
阻塞式消费者
@Service
public class JmsConsumer {
@Autowired
private JmsTemplate jmsTemplate;
public void consume(ApplicationContext act) {
JmsConsumer consumer = (JmsConsumer) act.getBean("jmsConsumer");
String res = consumer.jmsTemplate.receiveAndConvert().toString();
System.out.println("消费者消费数据 "+res);
}
}
监听式消费者
@Component
public class JmsListener implements MessageListener {
@Override
public void onMessage(Message message) {
if(null != message && message instanceof TextMessage){
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println("listener方式接收到消息: "+text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
第二步:测试Queue
配置文件applicationContext_queue.xml
配置文件applicationContext_listener_queue.xml
测试代码
public class AppQueue {
private String file = "applicationContext_queue.xml";
@Test
public void testConsume( ) {
ApplicationContext act = new ClassPathXmlApplicationContext(file);
JmsConsumer consumer = (JmsConsumer) act.getBean("jmsConsumer");
consumer.consume(act);
}
@Test
public void testProduce( ) {
ApplicationContext act = new ClassPathXmlApplicationContext(file);
JmsProducer jmsProducer = (JmsProducer) act.getBean("jmsProducer");
jmsProducer.produce(act);
}
@Test
public void testListener( ) {//监听topic
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext_listener_queue.xml");
JmsProducer jmsProducer = (JmsProducer) act.getBean("jmsProducer");
jmsProducer.produce(act);
}
}
测试步骤:
- 运行测试代码testProduce()方法,查看控制台和页面http://hcmaster:8161/admin/topics.jsp
- 运行测试方法testConsume()方法,查看控制台和页面http://hcmaster:8161/admin/topics.jsp
- 运行测试方法testListener()方法,查看控制台和页面http://hcmaster:8161/admin/topics.jsp
配置文件applicationContext_listener_topic.xml
测试代码
public class AppTopic {
private String file="applicationContext_topic.xml";
//对于topic来说,需要先启动消费者,再启动生产者
@Test
public void testConsume( ) {
ApplicationContext act = new ClassPathXmlApplicationContext(file);
JmsConsumer consumer = (JmsConsumer) act.getBean("jmsConsumer");
consumer.consume(act);
}
@Test
public void testProduce( ) {
ApplicationContext act = new ClassPathXmlApplicationContext(file);
JmsProducer jmsProducer = (JmsProducer) act.getBean("jmsProducer");
jmsProducer.produce(act);
}
@Test
public void testListener( ) {//监听topic
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext_listener_topic.xml");
JmsProducer jmsProducer = (JmsProducer) act.getBean("jmsProducer");
jmsProducer.produce(act);
}
}
测试步骤:
- 运行测试代码testProduce()方法,查看控制台和页面http://hcmaster:8161/admin/topics.jsp
- 运行测试方法testConsume()方法,查看控制台和页面http://hcmaster:8161/admin/topics.jsp
- 运行测试方法testListener()方法,查看控制台和页面http://hcmaster:8161/admin/topics.jsp
Spring5、SpringMVC5整合ActiveMQ时,报各种错,暂时没有解决,网上也没有搜到相应的解决方案。具体解决方案待以后研究