mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 rabbitmq 示例
This commit is contained in:
@@ -11,7 +11,10 @@ import org.springframework.context.annotation.Configuration;
|
||||
@Configuration
|
||||
public class RabbitConfig {
|
||||
|
||||
public static class DirectorExchangeDemoConfiguration {
|
||||
/**
|
||||
* Direct Exchange 示例的配置类
|
||||
*/
|
||||
public static class DirectExchangeDemoConfiguration {
|
||||
|
||||
// 创建 Queue
|
||||
@Bean
|
||||
@@ -41,7 +44,4 @@ public class RabbitConfig {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.springboot.lab04.rabbitmqdemo.consumer;
|
||||
|
||||
import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo01Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -9,9 +11,16 @@ import org.springframework.stereotype.Component;
|
||||
@RabbitListener(queues = Demo01Message.QUEUE)
|
||||
public class Demo01Consumer {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@RabbitHandler
|
||||
public void onMessage(Demo01Message message) {
|
||||
System.out.println("Receiver : " + message);
|
||||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
|
||||
}
|
||||
|
||||
// @RabbitHandler(isDefault = true)
|
||||
// public void onMessage(org.springframework.amqp.core.Message message) {
|
||||
// logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
spring:
|
||||
# RabbitMQ 配置项,对应 RabbitProperties 配置类
|
||||
rabbitmq:
|
||||
host: 127.0.0.1 # RabbitMQ 服务的地址
|
||||
port: 5672 # RabbitMQ 服务的端口
|
||||
username: guest # RabbitMQ 服务的账号
|
||||
password: guest # RabbitMQ 服务的密码
|
||||
virtual-host:
|
||||
|
||||
@@ -42,7 +42,7 @@ public class Demo01ProducerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testASyncSend() throws InterruptedException {
|
||||
public void testAsyncSend() throws InterruptedException {
|
||||
int id = (int) (System.currentTimeMillis() / 1000);
|
||||
producer.asyncSend(id).addCallback(new ListenableFutureCallback<Void>() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user