美文网首页
rabbitmq 消息队列的注解配置使用(一)

rabbitmq 消息队列的注解配置使用(一)

作者: 蘑菇Ai布丁 | 来源:发表于2018-11-28 09:31 被阅读0次

rabbitmq是一个比较常用的消息队列,他可以处理一些异步的操作,也可以进行一些模块间的互动,交流什么的。这一篇里面我们简单来看看mq的基于注解配置的普通调用,

首先我们需要添加mq的maven依赖

<dependency>

<groupId>org.springframework.amqp</groupId>

<artifactId>spring-rabbit</artifactId>

</dependency>

第二部我们写队列的配置类

@Configuration

public class RabbitConfig {

@Bean

    public QueuehelloQueue() {

return new Queue("hello");

}

}

第三部,我们写消息的生产者

@Component

@Slf4j

public class HelloSend {

@Autowired

    private AmqpTemplaterabbitTemplate;

public void send(){

String message ="hello chengyuan"+new Date();

log.info("message:"+message);

rabbitTemplate.convertAndSend("hello",message);

log.info("队列发送成功");

}

}

第四部:我们写消息的消费者

@Component

@RabbitListener(queues ="hello")

public class HelloReceiver {

@RabbitHandler

    public void process(String hello) {

System.out.println("Receiver  : " + hello);

}

}

这样,在controller里面调用send时,receiver也将收到消息,注意send类需要自动注入到spring中。

相关文章

网友评论

      本文标题:rabbitmq 消息队列的注解配置使用(一)

      本文链接:https://www.haomeiwen.com/subject/fjaoqqtx.html