Hello there again!
It passed 2 months since my last article, so I decided to write a new one.
Today we will focus on RabbitMq and what we can do with it using Camel.
Dependencies
Maven repository -> Camel RabbitMq
Connection to RabbitMq
When using SpringBoot:
- Create a class with @Configuration annotation.
- Create a method which will return ConnectionFactory and annotate it with @Bean.
- Instance a connection factory and set properties.
@Bean public ConnectionFactory rabbitConnectionFactory() { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("localhost); connectionFactory.setPort(5672); connectionFactory.setUsername("Username"); connectionFactory.setPassword("Password); return connectionFactory; } *note: You can use application properties to connect to the server.
If you are using pure Camel:
Using RabbitMq
Now that we successfully connected to RabbitMq it's time to start consuming/producing messages to queue/exchange.
Camel RabbitMq component documentation.
Consuming Message from the queue:
- We will set the rabbitMq component to fetch from the queue which is bind to exchange.
- Log message body received.
public void configure() {
from("rabbitmq:exchangeA?queue=QueueA&declare=true") .routeId("RabbiqMqConsumer") .log("Message received: ${body}"); Producing Message to exchange:
- We will create a route.
- Set message body.
- Send a message to the exchange.
public void configure() {
from("timer:fooo?period=10000") .routeId("RabbiqMqProducer) .setBody().constant("{\"foo\":\"bar\"}") .log("Message to be sent: ${body}") .to("rabbitmq:exchangeB"); Aand that's all folks, if anyone have some question, feel free to ask me via DM or just leave a comment.


Top comments (0)