- 操作步骤:
引入依赖包、配置连接参数、新建队列、生产者代码、消费者代码
- 依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency>
- 配置连接参数
spring.rabbitmq.enable=true
spring.rabbitmq.host=mq-test01.xxx.cn
spring.rabbitmq.port=5672
spring.rabbitmq.username=dms_test01
spring.rabbitmq.password=dms_test01
spring.rabbitmq.virtual-host=dms_test01
- 新建队列
import com.gxr.dmsData.common.Constants; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author :gongxr * @description:RabbitTemplate配置 * @date :Created in 2021/7/1 */ @Configuration public class RabbitConfig { // exchange、Routing Key和queue都是一样的 String queueName = Constants.QUEUE_NAME; // 队列名称 @Bean public Queue queue() { return new Queue(queueName, false); } // 交换机名 @Bean FanoutExchange exchange() { return new FanoutExchange(queueName); } // 绑定:将队列和交换机绑定 @Bean Binding bindingExchange(Queue queue, FanoutExchange exchange) { return BindingBuilder.bind(queue).to(exchange); } }
- 生产者代码
import com.gxr.dmsData.common.BaseTest; import com.gxr.dmsData.common.Constants; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Random; /** * @author :gongxr * @description:rabbitMQ测试类 * @date :Created in 2021/7/1 */ @Slf4j public class TestRabbitMQ extends BaseTest { /** * 引入依赖包、配置参数、配置RabbitConfig、测试代码 * 消费者监听(RabbitMqListener) */ // exchange、Routing Key和queue都是一样的 private String key = Constants.QUEUE_NAME; @Autowired RabbitTemplate rabbitTemplate; @Test public void testSender() { String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); HashMap<String, Object> map = new HashMap<>(); map.put("name", "Tom"); map.put("age", new Random().nextInt(100)); map.put("createTime", createTime); rabbitTemplate.convertAndSend(key, null, map); // rabbitTemplate.convertAndSend(key, null, "String123"); log.info("发送成功!"); } }
- 消费者代码
import com.alibaba.fastjson.JSON; import com.gxr.dmsData.common.Constants; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.messaging.handler.annotation.Headers; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.stereotype.Component; import java.util.Map; /** * @author :gongxr * @description:RabbitMQ消费者消息接收监听类(三种方法,推荐使用最后一种) * @date :Created in 2021/7/2 */ @Slf4j @Component public class RabbitMqListener { // @RabbitListener(queues = Constants.QUEUE_NAME) //监听的队列名称 TestDirectQueue public void handler(Object o) { log.info("1、Rabbit消费:【{}】", o.toString()); } // @RabbitListener(queues = Constants.QUEUE_NAME) public void handler(Message message) { log.info("2、Rabbit消费,BODY:【{}】,PROPERTIES:【{}】", new String(message.getBody()), message.getMessageProperties().toString()); } @RabbitListener(queues = Constants.QUEUE_NAME) public void handler(@Payload String body, @Headers Map<String, Object> headers) { log.info("3、Rabbit消费,BODY:【{}】,HEADER:【{}】", body, JSON.toJSONString(headers)); } }
来源项目:dms-test-data