-
配置
// 测试支付队列
public static final String EXCHANGE_PUSH = “my-mq-exchange_PUSH”;//交换机
public static final String QUEUE_PUSH = “QUEUE_PUSH”;//队列
public static final String ROUTINGKEY_PUSH = “spring-boot-routingKey_PUSH”;//路由// 配置交换机实例
@Bean
public DirectExchange exchangePush() {
return new DirectExchange(EXCHANGE_PUSH);
}// 获取队列
@Bean
public Queue queuePush() {
// 队列持久
return new Queue(QUEUE_PUSH, true);
}//将队列绑定到交换机上,并设置消息分发的路由
@Bean
public Binding bindingPush() {
return BindingBuilder.bind(queuePush()).to(exchangePush()).with(ROUTINGKEY_PUSH);
}
2.生产者:发送消息
public void sendMsg(String content) {
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
try {
rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_PUSH, RabbitConfig.ROUTINGKEY_PUSH, content, correlationId);
} catch (AmqpException e) {
e.toString();
}
}
//content是一个json对象 可以传入你需要在消费的时候需要的数据
// 调用的时候使用工具类将对象转化 sendMsg(FastJsonUtil.beanToJson(pushVo));
3.消费者:接收(消费)消息
@RabbitHandler
public void process(String content) {
try {
PushVo pushVo = FastJsonUtil.jsonToBean(content, PushVo.class);
log.error("打印日志" + content);
} catch (Exception e) {
log.error("打印日志" + e.toString() + content);
}
}
- 查看 RabbitMq web管理端 可以查看 队列的情况 是否产生 消费 怎么看下面链接有详细教程
https://www.jianshu.com/p/7b6e575fd451