Loss of messages in RabbitMQ
I am trying to create a persistent message queue with some delay per message. In Java code, it looks like this:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("WorkExchange", "direct");
channel.queueDeclare("WorkQueue", true, false, false, null);
channel.queueBind("WorkQueue", "WorkExchange", "");
Map<String, Object> args = new HashMap<>();
args.put("x-dead-letter-exchange", "WorkExchange");
channel.exchangeDeclare("RetryExchange", "direct");
channel.queueDeclare("RetryQueue", true, false, false, args);
channel.queueBind("RetryQueue", "RetryExchange", "");
channel.confirmSelect();
BasicProperties properties = new BasicProperties();
properties.setDeliveryMode(2);
properties.setExpiration("120000");
channel.basicPublish("RetryExchange", "", properties, "Hello world!".getBytes());
channel.waitForConfirmsOrDie();
connection.close();
However, I have some persistence issues. When I stop the server wait for a while and start again, the messages that should go to the WorkQueue will just disappear. What am I doing wrong? Or is it by design?
source to share
However, I have some persistence issues. When I stop the server, wait for a while and start it again, the messages that need to be moved to the WorkQueue just disappear. What am I doing wrong? Or is it by design?
You must use MessageProperties for your messages to persist.
channel.basicPublish("", "task_queue",
MessageProperties.PERSISTENT_TEXT_PLAIN,
message.getBytes());
Your current code is `channel.queueDeclare (" RetryQueue ", true , false, false, args); will do the persistence of the queue, but not the message.
More details here RabbitMQ Doc
source to share