Delayed message in RabbitMQ

后端 未结 8 1268
别跟我提以往
别跟我提以往 2020-12-08 02:31

Is it possible to send message via RabbitMQ with some delay? For example I want to expire client session after 30 minutes, and I send a message which will be processed after

8条回答
  •  旧时难觅i
    2020-12-08 02:53

    As I don't have enough reputation to add comment, posting a new answer. This is just an addition to what has been already discussed at http://www.javacodegeeks.com/2012/04/rabbitmq-scheduled-message-delivery.html

    Except instead of setting ttl on messages, you can set it at queue level. Also you can avoid creating a new exchange just for the sake of redirecting the messages to different Queue. Here is sample Java code:

    Producer:

    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    import java.util.HashMap;
    import java.util.Map;
    
    public class DelayedProducer {
        private final static String QUEUE_NAME = "ParkingQueue";
        private final static String DESTINATION_QUEUE_NAME = "DestinationQueue";
    
        public static void main(String[] args) throws Exception{
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("localhost");
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
    
            Map arguments = new HashMap();
            arguments.put("x-message-ttl", 10000);
            arguments.put("x-dead-letter-exchange", "");
            arguments.put("x-dead-letter-routing-key", DESTINATION_QUEUE_NAME );
            channel.queueDeclare(QUEUE_NAME, false, false, false, arguments);
    
            for (int i=0; i<5; i++) {
                String message = "This is a sample message " + i;
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
                System.out.println("message "+i+" got published to the queue!");
                Thread.sleep(3000);
            }
    
            channel.close();
            connection.close();
        }
    }
    

    Consumer:

    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.QueueingConsumer;
    
    public class Consumer {
       private final static String DESTINATION_QUEUE_NAME = "DestinationQueue";
    
        public static void main(String[] args) throws Exception{
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            QueueingConsumer consumer = new QueueingConsumer(channel);
            boolean autoAck = false;
            channel.basicConsume(DESTINATION_QUEUE_NAME, autoAck, consumer);
    
            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());
                System.out.println(" [x] Received '" + message + "'");
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    }
    

提交回复
热议问题