阿帕奇骆驼#3-RabbitMq

您好!

自上一篇文章以来已经过去了2个月,所以我决定写一篇新文章。

今天,我们将专注于RabbitMq以及如何使用Camel对其进行处理。

依存关系

中号aven repository -> Camel RabbitMq

连接到RabbitMq

使用Spring Boot时:

  1. 用@Configuration注释创建一个类。创建一个将返回ConnectionFactory并用@Bean注释的方法。实例化连接工厂并设置属性。

@豆 公共ConnectionFactory rabbitConnectionFactory(){ ConnectionFactory connectionFactory =新的ConnectionFactory(); connectionFactory.setHost(“ localhost); connectionFactory.setPort(5672); connectionFactory.setUsername(“ Username”); connectionFactory.setPassword(“ Password);

返回connectionFactory; }

*注意:您可以使用应用程序属性连接到服务器。

如果您使用的是纯骆驼:

阿帕奇骆驼#3-RabbitMq

使用RabbitMq

现在我们已成功连接到RabbitMq,是时候开始使用/生成要排队/交换的消息了。

Camel RabbitMq component documentation.

从队列中消费消息:

  1. 我们将RabbitMq组件设置为从绑定到交换的队列中获取。收到的日志消息正文。

公共无效的configure(){

from("rabbitmq:exchangeA?queue=QueueA&declare=true") .routeId(“ RabbiqMqConsumer”) .log(“收到的消息:$ {body}”);

产生消息以进行交换:

  1. 我们将创建一条路线。设置邮件正文。向交易所发送消息。

公共无效的configure(){

from(“ timer:fooo?period = 10000”) .routeId(“ RabbiqMqProducer) .setBody()。constant(“ {\” foo \“:\” bar \“}”) .log(“要发送的消息:$ {body}”) .to(“ rabbitmq:exchange”);

这就是所有人,如果有人有任何疑问,请随时通过DM询问我或发表评论。

阿帕奇骆驼#3-RabbitMq

from: https://dev.to//djoleb/apache-camel-3-using-rabbitmq-2250