![51%71ZMOPR][email protected]](http://upload-images.jianshu.io/upload_images/3269064-2b5de5b351294297.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1. ActiveMQ安装
1.1 下载(版本5.14.5)
点我官网下载
1.2 安装
解压下载的压缩文件到任意目录中(
eg. C:\Program Files (x86)\apache-activemq-5.14.5
),进入%ACTIVEMQ_HOME%/bin目录,根据自己的系统位数,进入32/64目录,点击activemq.bat
启动ActiveMQ;
2. ActiveMQ与Spring整合使用
2.1 在Maven中添加ActiveMQ和JMS相关的pom,如下:
org.springframework
spring-jms
4.2.5.RELEASE
org.apache.xbean
xbean-spring
3.16
org.apache.activemq
activemq-core
5.7.0
org.apache.activemq
activemq-pool
5.12.1
2.2 添加配置文件spring-activemq.xml
在配置文件中加入以下配置信息,每个配置信息都有具体的解释:
testQueue
注:在配置文件中,一定不要忘记加入ActiveMQ和JMS相关的schema
2.3 创建Producer和Consumer相关的Service
创建ProducerService,用于发送信息到消息中心
@Service
public class ProducerService {
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
private Queue queue;
/**
* 根据目的地发送消息
*/
public void sendMessage(Destination destination, final String msg) {
System.out.println(Thread.currentThread().getName() + " 向队列" + destination.toString()
+ "发送消息------->" + msg);
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
}
public String send(String userId, String msg) {
System.out.println(
Thread.currentThread().getName() + " 向 " + userId + " 的队列" + userId.toString() + "发送消息------>" + msg);
queue = new ActiveMQQueue(userId);
jmsTemplate.send(queue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage message=session.createTextMessage(msg);
message.setStringProperty(userId, msg);
return message;
}
});
return "发送成功";
}
/**
* 向默认目的地发送消息
*/
public String sendMessage(final String msg) {
String destination = jmsTemplate.getDefaultDestinationName();
System.out
.println(Thread.currentThread().getName() + " 向队列" + destination + "发送消息---------------------->" + msg);
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
return "发送成功";
}
}
创建ConsumerService,用于接受消息
@Service
public class ConsumerService{
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
public String receive(Destination destination) {
TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
try {
System.out.println("从队列" + destination.toString() + "收到了消息:\t" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
return textMessage.toString();
}
public String receive(String userId) {
Queue queue=new ActiveMQQueue(userId+"?consumer.prefetchSize=4");
Message message = null;
String property=null;
try {
message=jmsTemplate.receive(queue);
property=message.getStringProperty(userId);
System.out.println("从队列" + queue.toString() + "收到了消息:\t" + property);
} catch (JMSException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return property;
}
}
2.4 添加Controller,用于曝露接口
@Controller
@RequestMapping(value="/mq")
public class MessageController {
private Logger logger = Logger.getLogger(MessageController.class);
@Resource(name = "demoQueueDestination")
private Destination destination;
@Autowired
private ProducerService producer;
@Autowired
private ConsumerService consumer;
@RequestMapping(value = "/SendMessage", method = RequestMethod.POST,produces="application/json")
@ResponseBody
public void send(@RequestParam(value = "userId",required=false)String userId,@RequestParam(value = "msg")String msg) {
logger.info(Thread.currentThread().getName() + "------------send to jms Start");
if (userId==null||"".equals(userId)) {
producer.sendMessage(destination, msg);
}else {
producer.send(userId, msg);
}
logger.info(Thread.currentThread().getName() + "------------send to jms End");
}
@RequestMapping(value = "/ReceiveMessage", method = RequestMethod.GET)
@ResponseBody
public Object receive(@RequestParam(value = "userId",required=false)String userId) {
logger.info(Thread.currentThread().getName() + "------------receive from jms Start");
String tm=null;
if (userId==null||"".equals(userId)) {
tm = consumer.receive(destination);
} else {
tm = consumer.receive(userId);
}
logger.info(Thread.currentThread().getName() + "------------receive from jms End");
return tm.toString();
}
}
2.5 配置监听器(ek)
如果在配置文件中打开了监听器的注释,即打开监听器,消费者会立即去消费消息,则还需要添加如下代码:
public class QueueMessageListener implements MessageListener{
@Override
public void onMessage(Message message) {
TextMessage tm=(TextMessage) message;
try {
System.out.println("QueueMessageListener监听到了文本消息:\t"
+ tm.getText());
//do other work
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3. 测试
启动tomcat,将Javaweb项目运行在tomcat中,通过postman测试接口和方法
接受消息接口:http://localhost:8080/{project_neme}
/mq/ReceiveMessage?userId={消息队列名称}
发送消息接口:http://localhost:8080/{project_neme}
/mq/SendMessage?userId={消息队列名称}&msg={参数}