maven配置
//pom.xml
com.rabbitmq
amqp-client
3.5.1
org.springframework.amqp
spring-rabbit
1.4.5.RELEASE
rabbitMQ配置文件
//rabbitmq-config.properties
mq.host=127.0.0.1
mq.username=test
mq.password=123456
mq.port=5672
mq.vhost=testmq
mq.queueName=test
Spring配置
申明一个消息队列Queue
//application-mq.xml
- tips
durable:是否持久化
exclusive: 仅创建者可以使用的私有队列,断开后自动删除
auto_delete: 当所有消费客户端连接断开后,是否自动删除队列
- tips
rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。
rabbit:binding:设置消息queue匹配的key
// 定义rabbitMQ接口
public interface RabbitMQService {
/**
* 发送消息到指定队列
* @param queueKey
* @param object
*/
public void sendDataToQueue(String queueKey, Object object);
}
- 实现接口
import com.haomo.delivery.service.RabbitMQService;
import org.jboss.logging.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by yide on 2018/9/7.
*/
@Service
public class RabbitMQImpl implements RabbitMQService {
@Autowired
private AmqpTemplate amqpTemplate;
private final static Logger LOGGER = Logger.getLogger(RabbitMQImpl.class);
/* (non-Javadoc)
* @see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object)
*/
@Override
public void sendDataToQueue(String queueKey, Object object) {
try {
amqpTemplate.convertAndSend(queueKey, object);
} catch (Exception e) {
LOGGER.error(e);
}}
}
- tips:
convertAndSend:将Java对象转换为消息发送到匹配Key的交换机中Exchange,由于配置了JSON转换,这里是将Java对象转换成JSON字符串的形式。原文:Convert a Java object to an Amqp Message and send it to a default exchange with a specific routing key.
@Controller
public class OrderInterface {// 注入rabbitmq服务
@Autowired
RabbitMQService rabbitMQService;
@RequestMapping(value = "https://www.it610.com/test/new",
method = RequestMethod.POST,
produces = "application/json;
charset=UTF-8")
@ResponseBody
public Object createOvertime(
HttpServletRequest request,
@RequestHeader(value = "https://www.it610.com/article/X-Auth-Token",required = false) String token,
@RequestParam(value = "https://www.it610.com/article/content") String content
) {
Map map = new HashMap();
map.put("content",content);
// 存储到rabbitmq中
rabbitMQService.sendDataToQueue("test",map);
return "success";
}
}
消费者
@Component
public class QueueListenter implements MessageListener {
@Override
public void onMessage(Message msg) {
try{
String content = new String(msg.getBody(), "UTF-8");
System.out.println(content);
}catch(Exception e){
e.printStackTrace();
}
}
}
监听配置
- tips
queues:监听的队列,多个的话用逗号(,)分隔
ref:监听器
@Component
public class QueueListenter implements ChannelAwareMessageListener {
@Override
public synchronized void onMessage(Message message, Channel channel) throws Exception {
try{
String content = new String(Message.getBody(), "UTF-8");
System.out.println(content);
// 删除rabbit队列消息
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
}catch(Exception e){
e.printStackTrace();
}
}
监听配置
【spring集成rabbitmq】参考链接:https://blog.csdn.net/JaCman/article/details/50261915
官网:http://www.rabbitmq.com/