收发消息基本业务
This commit is contained in:
parent
8a6ff9f231
commit
ce115dba6f
|
@ -7,8 +7,66 @@ import org.springframework.stereotype.Component;
|
|||
@Slf4j
|
||||
@Component
|
||||
public class MqListener {
|
||||
/**
|
||||
* 监听
|
||||
*
|
||||
* @param message 收到消息
|
||||
*/
|
||||
@RabbitListener(queues = "hello.eq")
|
||||
void listenHelloQueue(String message) {
|
||||
System.out.println("消费者收到了消息" + message);
|
||||
System.out.println("消费者[hello.eq]收到了消息" + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听 work.queue 中队列消息
|
||||
* @param message 消息
|
||||
*/
|
||||
@RabbitListener(queues = "work.queue")
|
||||
void workQueue(String message) {
|
||||
System.out.println("消费者[work.queue]收到消息" + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听 work.queue 中队列消息
|
||||
* @param message 消息
|
||||
*/
|
||||
@RabbitListener(queues = "work.queue")
|
||||
void workQueue2(String message) {
|
||||
System.err.println("消费者2[work.queue]收到消息" + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听 direct.queue1 中队列消息
|
||||
* @param message 消息
|
||||
*/
|
||||
@RabbitListener(queues = "direct.queue1")
|
||||
public void listenDirectQueue1(String message) {
|
||||
System.out.println("direct.queue1 收到了 direct.queue1:【消息】" + message);
|
||||
}
|
||||
/**
|
||||
* 监听 direct.queue2 中队列消息
|
||||
* @param message 消息
|
||||
*/
|
||||
@RabbitListener(queues = "direct.queue2")
|
||||
public void listenDirectQueue(String message) {
|
||||
System.out.println("direct.queue2 收到了 direct.queue2:【消息】" + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接受消息 topic.queue1
|
||||
* @param message 消息
|
||||
*/
|
||||
@RabbitListener(queues = "topic.queue1")
|
||||
public void listenTopicQueue1(String message) {
|
||||
System.out.println("topic.queue1 收到了 【消息]" + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接受消息 topic.queue1
|
||||
* @param message 消息
|
||||
*/
|
||||
@RabbitListener(queues = "topic.queue2")
|
||||
public void listenTopicQueue2(String message) {
|
||||
System.out.println("topic.queue2 收到了 【消息]" + message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
|
||||
logging.pattern.dateformat= MM-dd HH:mm:ss:SSS
|
||||
|
||||
logging.pattern.dateformat=MM-dd HH:mm:ss:SSS
|
||||
spring.rabbitmq.username=bunny
|
||||
spring.rabbitmq.password=02120212
|
||||
spring.rabbitmq.port=5672
|
||||
spring.rabbitmq.host=192.168.2.156
|
||||
spring.rabbitmq.host=192.168.2.156
|
||||
spring.rabbitmq.listener.simple.prefetch=1
|
|
@ -20,4 +20,39 @@ class RabbitMqApplicationTests {
|
|||
String message = "Hello";
|
||||
rabbitTemplate.convertAndSend(queueName, message);
|
||||
}
|
||||
|
||||
// 接受消息
|
||||
@Test
|
||||
void testWorkQueue() throws InterruptedException {
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
String queueName = "work.queue";
|
||||
String message = "Hello" + i;
|
||||
rabbitTemplate.convertAndSend(queueName, message);
|
||||
Thread.sleep(20);
|
||||
}
|
||||
}
|
||||
|
||||
// 发给交换机,使用key发送,当前key是所有人都可以接受到
|
||||
@Test
|
||||
void testWorkQueue1() {
|
||||
String exchangeName = "bunny.direct";
|
||||
String message = "所有人都收到";
|
||||
rabbitTemplate.convertAndSend(exchangeName, "red", message);
|
||||
}
|
||||
|
||||
// 发送消息固定key可以接受到
|
||||
@Test
|
||||
void testWorkQueue2() {
|
||||
String exchangeName = "bunny.direct";
|
||||
String message = "blue都收到";
|
||||
rabbitTemplate.convertAndSend(exchangeName, "blue", message);
|
||||
}
|
||||
|
||||
// 使用通配符 chin.# 后面无论写什么这个都能收到,topic可以接受通配符
|
||||
@Test
|
||||
void testWorkQueue3() {
|
||||
String exchangeName = "bunny.topic";
|
||||
String message = "今天天气好";
|
||||
rabbitTemplate.convertAndSend(exchangeName, "chin.weather", message);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue