赞
踩
- <!--RabbitMQ-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-amqp</artifactId>
- </dependency>
- spring:
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- username: root
- password: 123456
- url: jdbc:mysql://127.0.0.1:3306/SpringCloud?characterEncoding=utf-8&useSSL=false
- rabbitmq:
- host: localhost
- port: 5672
- username: guest
- password: guest
- package com.hx.order.message;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- /**
- * 接收mq消息
- */
- @Slf4j
- @Component
- public class MqReceiver {
-
- @RabbitListener(queues = "myQueue")
- public void process(String message) {
- log.info("MqReceiver:{}"+message);
- }
- }

- package com.hx.order;
-
- import org.junit.Test;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
-
- import java.util.Date;
-
- /**
- * 发送mq消息测试
- */
- @Component
- public class MqSenderTest extends OrderApplicationTests {
-
- @Autowired
- private AmqpTemplate amqpTemplate;
-
- @Test
- public void send() {
- amqpTemplate.convertAndSend("myQueue","你是说什么啊,发送时间:"+new Date());
- }
- }

- package com.hx.order;
-
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class OrderApplicationTests {
-
- @Test
- public void contextLoads() {
- }
-
- }

- package com.hx.order.message;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.annotation.Queue;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- /**
- * 接收mq消息
- */
- @Slf4j
- @Component
- public class MqReceiver {
-
- // 自动创建队列
- @RabbitListener(queuesToDeclare = @Queue("myQueue"))
- public void process(String message) {
- log.info("MqReceiver:{}"+message);
- }
- }

自动创建 Exchange和Queue
- package com.hx.order.message;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.annotation.Exchange;
- import org.springframework.amqp.rabbit.annotation.Queue;
- import org.springframework.amqp.rabbit.annotation.QueueBinding;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- /**
- * 接收mq消息
- */
- @Slf4j
- @Component
- public class MqReceiver {
-
- // 自动创建 Exchange和Queue
- @RabbitListener(bindings =@QueueBinding(
- value = @Queue("myQueue"),
- exchange = @Exchange("myExchange")
- ))
- public void process(String message) {
- log.info("MqReceiver:{}"+message);
- }
- }

不仅自动创建这俩个,还把他俩绑定在一起。
- package com.hx.order.message;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.rabbit.annotation.Exchange;
- import org.springframework.amqp.rabbit.annotation.Queue;
- import org.springframework.amqp.rabbit.annotation.QueueBinding;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- /**
- * 接收mq消息
- */
- @Slf4j
- @Component
- public class MqReceiver {
-
- // 自动创建 Exchange和Queue
- @RabbitListener(bindings =@QueueBinding(
- value = @Queue("myQueue"),
- exchange = @Exchange("myExchange")
- ))
- public void process(String message) {
- log.info("MqReceiver:{}"+message);
- }
-
- /**书本
- * exchange 组(交换器) key 路由(钥匙) value 队列
- * @param message
- */
- @RabbitListener(bindings =@QueueBinding(
- exchange = @Exchange("myOrder"),
- key = "book",
- value = @Queue("bookOrder")
-
- ))
- public void processBook(String message) {
- log.info("bookMqReceiver:{}"+message);
- }
-
-
- /**
- *
- * @param message
- */
- @RabbitListener(bindings =@QueueBinding(
- exchange = @Exchange("myOrder"),
- key = "fruit",
- value = @Queue("fruitOrder")
-
- ))
- public void processFruit(String message) {
- log.info("fruitMqReceiver:{}"+message);
- }
- }

- package com.hx.order;
-
- import org.junit.Test;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
-
- import java.util.Date;
-
- /**
- * 发送mq消息测试
- */
- @Component
- public class MqSenderTest extends OrderApplicationTests {
-
- @Autowired
- private AmqpTemplate amqpTemplate;
-
- @Test
- public void send() {
- amqpTemplate.convertAndSend("myQueue","你是说什么啊,发送时间:"+new Date());
- }
-
- @Test
- public void sendOrder() {
- // 第一的参数是交换器 第二个是路由, 第三个是消息
- amqpTemplate.convertAndSend("myOrder","fruit","你是说什么啊,发送时间:"+new Date());
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。