ActiveMQ笔记36-Spring整合ActiveMQ之队列消费者
原创
©著作权归作者所有:来自51CTO博客作者qq59ce45caba461的原创作品,请联系作者获取转载授权,否则将追究法律责任
Spring相关配置文件参考前面这篇博客,这里就不重复写了:javascript:void(0)
创建SpringMQ_Consumer类,代码如下。
package com.wsy.spring.activemq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
@Service
public class SpringMQ_Consumer {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过Spring容器获取SpringMQ_Producer对象的实例,在getBean时候,默认是类名首字母小写
SpringMQ_Consumer springMQ_consumer = (SpringMQ_Consumer) applicationContext.getBean("springMQ_Consumer");
String message = (String) springMQ_consumer.jmsTemplate.receiveAndConvert();
System.out.println(message);
}
}