Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loss of messages in RabbitMQ

I try to build persistent message queue with some delay per message. In Java-code it's 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 problem with persistency. When I stop server, wait some time and start it again, messages which have to move to WorkQueue just disappear. What I do wrong? Or it's by design?

like image 830
dmitrievanthony Avatar asked Mar 18 '23 11:03

dmitrievanthony


1 Answers

However, I have some problem with persistency. When I stop server, wait some time and start it again, messages which have to move to WorkQueue just disappear. What I do wrong? Or it's by design?

You should use MessageProperties for making your messages persistence.

channel.basicPublish("", "task_queue", 
        MessageProperties.PERSISTENT_TEXT_PLAIN,
        message.getBytes());

Your current code `channel.queueDeclare("RetryQueue", true, false, false, args); will make the queue persistence but not the message.

More here RabbitMQ Doc

like image 121
lambodar Avatar answered Mar 21 '23 01:03

lambodar