参看此文档此需要先看https://my.oschina.net/sdlvzg/blog/1523360搭建环境
本文档主要介绍Spring Boot 集成 RabbitMQ CallBack,也就是带callback的消息发送
增加回调处理,这里不再使用application.properties默认配置的方式,会在程序中显示的使用文件中的配置信息。
生产者项目:
消息生产者类
package org.morecare.callback;
import java.util.UUID;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Send implements RabbitTemplate.ConfirmCallback {
private RabbitTemplate rabbitTemplate;
/**
* 构造方法注入
*/
@Autowired
public Send(RabbitTemplate rabbitTemplatecallback) {
this.rabbitTemplate = rabbitTemplatecallback;
rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话,那回调就是最后设置的内容
}
/**
* 发送消息
*/
public void sendMsg(String content) {
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
System.out.println("content:"+content);
System.out.println("correlationId:"+correlationId);
rabbitTemplate.convertAndSend("spring-boot-exchange", "spring-boot-routingKey", content, correlationId);
}
/**
* 回调
*/
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
System.out.println(" 回调id:" + correlationData);
if (ack) {
System.out.println("消息成功消费");
} else {
System.out.println("消息消费失败:" + cause);
}
}
}
生产者RabbitMq配置
package org.morecare;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class RabbitmqCallBackConfig {
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses("127.0.0.1:5672");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/");
connectionFactory.setPublisherConfirms(true); //必须要设置
return connectionFactory;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//必须是prototype类型
public RabbitTemplate rabbitTemplatecallback() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
}
}
生产者测试类
package org.morecare.controller;
import org.morecare.callback.Send;
import org.morecare.hello.HelloSender1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RabbitTest {
@Autowired
private HelloSender1 helloSender1;
@Autowired private Send send;
//单生产者-单消费者
@GetMapping("/hello")
public void hello() {
helloSender1.send();
}
//fanout exchange类型rabbitmq测试
@GetMapping("/send")
public void send() {
send.sendMsg("lvgang");
}
}
消费者项目:
消息消费者:
package org.morecare.callback;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "spring-boot-queue")
public class Receiver {
@RabbitHandler
public void process(String msg) {
System.out.println("Receiver : " + msg);
}
}
消费者配置
package org.morecare;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqCallBackConfig {
/**
* 针对消费者配置
* 1. 设置交换机类型
* 2. 将队列绑定到交换机
FanoutExchange: 将消息分发到所有的绑定队列,无routingkey的概念
HeadersExchange :通过添加属性key-value匹配
DirectExchange:按照routingkey分发到指定队列
TopicExchange:多关键字匹配
*/
@Bean
public DirectExchange defaultExchange() {
return new DirectExchange("spring-boot-exchange");
}
@Bean
public Queue queue() {
return new Queue("spring-boot-queue", true); //队列持久
}
@Bean
public Binding binding() {
return BindingBuilder.bind(queue()).to(defaultExchange()).with("spring-boot-routingKey");
}
}
测试
通过工程启动类启动两个或工程,执行工程启动类中的Main就可以。
启动完成之后,通过浏览器访问以下地址:http://localhost:8081/send,然后去看Eclipse的控制台
消息发送端控制台会打印以下信息
消息接收端控制台会打印以下信息
从上面可以看出callbackSender发出的UUID,收到了回应,又传回来了。