1、导入rabbitMQ依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2、配置application.yml文件
server: port: 8089 spring: application: name: mq-demo # config rabbitmq rabbitmq: host: localhost port: 5672 username: guest password: guest # 消息发送到交换机确认机制,是否确认回调 publisher-confirms: true
3、结构目录如下
4、rabbitMQ配置文件
4.1、QueueConfig队列配置文件
/** * FileName: QueueConfig * Author: DING WEI * Date: 2018/12/21 9:24:24 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.rabbitmq.mq.config; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 〈一句话功能简述〉<br> * 队列配置 * @author DING WEI * @create 2018/12/21 9:24:24 * @since 1.0.0 */ @Configuration public class QueueConfig { /** * 1、durable=“true”持久化,rabbitmq重启的时候不用创建新的队列 * 2、autoDelete=“false”表示消息队列没有在使用时将自动被删除,默认false * 3、exclusive=“false”表示当前消息队列是否在当前connection生效,默认false * @prama * @return * @author DING WEI * @date 2018/12/21 9:26 */ @Bean public Queue firstQueue(){ return new Queue("first-queue",true,false,false); } @Bean public Queue secondQueue(){ return new Queue("second-queue",true,false,false); } }
4.2、ExchangeConfig交换机配置文件
/** * FileName: ExchangeConfig * Author: DING WEI * Date: 2018/12/21 9:18:18 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.rabbitmq.mq.config; import org.springframework.amqp.core.DirectExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 〈一句话功能简述〉<br> * 〈网关启动文件〉 * * @author DING WEI * @create 2018/12/21 9:18:18 * @since 1.0.0 */ @Configuration public class ExchangeConfig { /** * 交换机配置 * 1、durable为true,rabbitmq重启的时候不用重新创建新的交换机 * @prama * @return * @author DING WEI * @date 2018/12/21 9:21 */ @Bean public DirectExchange directExchange(){ DirectExchange directExchange = new DirectExchange(RabbitMqConfig.EXCHANGE,true,false); return directExchange; } }
4.3、RabbitMQConfig配置文件
/** * FileName: RabbitMqConfig * Author: DING WEI * Date: 2018/12/21 9:31:31 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.rabbitmq.mq.config; import com.rabbitmq.mq.mqcallback.MsgSendConfigCallBack; import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 〈一句话功能简述〉<br> * 〈网关启动文件〉 * * @author DING WEI * @create 2018/12/21 9:31:31 * @since 1.0.0 */ @Configuration public class RabbitMqConfig { //消息交换机名称 public static final String EXCHANGE = "TEST_EXCHANGE"; //队列key1 public static final String ROUTINGKEY1 = "QUEUE_FIRST_KEY"; //队列key2 public static final String ROUTINGKEY2 = "QUEUE_SECOND_KEY"; //连接工厂 @Autowired private ConnectionFactory factory; //交换机配置 @Autowired private ExchangeConfig exchangeConfig; //队列配置 @Autowired private QueueConfig queueConfig; /** * 将交换机和消息队列1绑定 * @prama * @return * @author DING WEI * @date 2018/12/21 9:42 */ @Bean public Binding binbing_one(){ return BindingBuilder.bind(queueConfig.firstQueue()).to(exchangeConfig.directExchange()).with(ROUTINGKEY1); } /** * 将交换机和消息队列2绑定 * @prama * @return * @author DING WEI * @date 2018/12/21 9:43 */ @Bean public Binding binbing_two(){ return BindingBuilder.bind(queueConfig.secondQueue()).to(exchangeConfig.directExchange()).with(ROUTINGKEY2); } /** * 有消息到达时会通知监听在对应队列上的监听对象 * @prama * @return * @author DING WEI * @date 2018/12/21 9:48 */ @Bean public SimpleMessageListenerContainer simpleMessageListenerContainer_one(){ SimpleMessageListenerContainer listener = new SimpleMessageListenerContainer(factory); listener.addQueues(queueConfig.firstQueue()); listener.setExposeListenerChannel(true); listener.setMaxConcurrentConsumers(5); listener.setConcurrentConsumers(1); //设置确认默认手工确认 listener.setAcknowledgeMode(AcknowledgeMode.MANUAL); return listener; } /** * 自定义rabbittemplate用于数据的接受和发送 * @prama * @return * @author DING WEI * @date 2018/12/21 9:58 */ @Bean public RabbitTemplate rabbitTemplate (){ RabbitTemplate rabbitTemplate = new RabbitTemplate(factory); rabbitTemplate.setConfirmCallback(msgSendConfigCallBack()); return rabbitTemplate; } /** * 消息确认机制 * @prama * @return * @author DING WEI * @date 2018/12/21 9:57 */ @Bean public MsgSendConfigCallBack msgSendConfigCallBack(){ return new MsgSendConfigCallBack(); } }
5、消息发送到交换机确认回调
/** * FileName: MsgSendConfigCallBack * Author: DING WEI * Date: 2018/12/21 9:51:51 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.rabbitmq.mq.mqcallback; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; /** * 〈一句话功能简述〉<br> * 消息发送到交换机确认机制 * @author DING WEI * @create 2018/12/21 9:51:51 * @since 1.0.0 */ public class MsgSendConfigCallBack implements RabbitTemplate.ConfirmCallback { @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { System.out.println("MsgSendConfigCallBack,回调:"+correlationData); if(ack){ System.out.println("消息消费成功!"); }else { System.out.println("消息消费失败:"+cause+"\n重新发送!"); } } }
6、消息生产者
/** * FileName: FirstProducer * Author: DING WEI * Date: 2018/12/21 10:01:01 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.rabbitmq.mq.producer; import com.rabbitmq.mq.config.RabbitMqConfig; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 〈一句话功能简述〉<br> * @author DING WEI * @create 2018/12/21 10:01:01 * @since 1.0.0 */ @Component public class FirstProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMsg(String uuid,Object message){ CorrelationData correlationId = new CorrelationData(uuid); rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE,RabbitMqConfig.ROUTINGKEY2,message,correlationId); } }
7、消息消费者
/** * FileName: FirstConsumer * Author: DING WEI * Date: 2018/12/21 10:06:06 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.rabbitmq.mq.consumer; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * 〈一句话功能简述〉<br> * 〈网关启动文件〉 * * @author DING WEI * @create 2018/12/21 10:06:06 * @since 1.0.0 */ @Component public class FirstConsumer { @RabbitListener(queues = {"first-queue","second-queue"},containerFactory = "rabbitListenerContainerFactory") public void handleMessage(String message){ System.out.println("FirstConsumer{}handleMessage:"+message); } }
8、测试
/** * FileName: SendController * Author: DING WEI * Date: 2018/12/21 10:11:11 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.rabbitmq.mq.rest; import com.rabbitmq.mq.producer.FirstProducer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.Mapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; /** * 〈一句话功能简述〉<br> * 〈网关启动文件〉 * * @author DING WEI * @create 2018/12/21 10:11:11 * @since 1.0.0 */ @RestController public class SendController { @Autowired private FirstProducer firstProducer; @GetMapping("/send") public String sendMessage(String message){ String uuid = UUID.randomUUID().toString().replaceAll("-",""); firstProducer.sendMsg(uuid,message); return uuid; } }
9、结果如下