tcp://127.0.0.1:61616
生产者
package com.manong.controller;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class MQController { @Autowired private JmsTemplate jmsTemplate; @Autowired private Destination destination; @ResponseBody @RequestMapping("mqsend") public void messageSend() { final String message = "测试发送消息~"; jmsTemplate.send(destination,new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } }
实际项目中消息的消费者多以监听器实现,在配置文件中如下配置监听即可
package com.manong.listener;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;import org.springframework.stereotype.Component;@Componentpublic class QueueListener implements MessageListener{ @Override public void onMessage(Message message) { TextMessage msg=(TextMessage) message; try { System.out.println(msg.getText());//处理实际业务 } catch (JMSException e) { e.printStackTrace(); } }}