Camel component RabbitMQ not durable
I try to send durable message from Camel component to RabbitMQ. Although default option is true, I set this option explicitly:
.to(ExchangePattern.InOnly, "rabbitmq:demoExchange?queue=demoQueue" +
"&guaranteedDeliveries=true" +
"&mandatory=true" +
"&autoDelete=false" +
"&durable=true")
Also, I created RabbitMQ component:
@Bean("rabbitmq")
fun rabbitMqComponent(connectionFactory: ConnectionFactory): RabbitMQComponent {
val rabbitMqComponent = RabbitMQComponent()
rabbitMqComponent.connectionFactory = connectionFactory
rabbitMqComponent.isDurable = true
return rabbitMqComponent
}
And my connection factory is:
@Bean
fun connectionFactory(): ConnectionFactory {
val connectionFactory = ConnectionFactory()
connectionFactory.host = "localhost"
connectionFactory.port = 5672
connectionFactory.username = "guest"
connectionFactory.password = "guest"
return connectionFactory
}
The problem description by steps:
-
Send the message to the broker
-
See the message in RabbitMQ management
-
Stop and start broker by commands:
rabbitmq-service stop rabbitmq-service start
-
My queue is absent
How to save the message after server restart?
Also, I tried to make an example with RabbitMQ without Camel and the message was saved after server restart.
AFAIK to have messages that survive a broker restart, you have to met multiple things
- Durable exchange
- Durable queue
- Persistent flag on messages
So I guess with durable=true
Camel takes care that the first two are met. Perhaps you can double check this in some kind of admin GUI?
However, the third one is missing in your post. The Rabbit Docs say
Durability of a queue does not make messages that are routed to that queue durable
According to the Camel docs of the Rabbit component there is a message header rabbitmq.DELIVERY_MODE
you can set on the Message. The value for persistent
seems to be 2
(taken from this tutorial).
To solve the problem, it needed to set header with the key rabbitmq.DELIVERY_MODE
, for example in route:
.setHeader("rabbitmq.DELIVERY_MODE", simple("2"))
Here can be 2 values:
- non persistent
- persistent
After setting this header the message doesn't disappear after server restart.