News    Videos    Explore 


Super Saver Flight Deals - 468x60banner

RabbitMQ – How to get messages with certain routing keys?

This topic contains 1 reply, has 1 voice, and was last updated by  Jonathan65 5 months, 3 weeks ago.

  • Author
    Posts
  • #3173 Reply

    Jonathan65

    Messages can be sent to a queue with several different routing keys, but I only want to get the messages that have “MyRouting.Route.RtKey1” or “MyRouting.Route.RtKey2”. I have the following as my consumer code, but it is getting all messages regardless of what routing key the message has. How can I only get the messages I want?

  • #3174 Reply

    Jonathan65

    This is the code :

    String myMessage;
    String routingKeyReceived;

    private void getMessage() {
    String FROM_QUEUE_NAME = “FromQueue”;
    String EXCHANGE_NAME = “amq.topic”;
    myMessage = “”;
    routingKeyReceived = “”;

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("test");
    factory.setPassword("test");
    factory.setVirtualHost("MyVirtualHost");
    factory.setHost("172...");
    factory.setPort(5672);
    
    try 
    {
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
    
        channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);
    
        channel.queueDeclare(FROM_QUEUE_NAME, true, false, false, null);
    
        channel.queueBind(FROM_QUEUE_NAME, EXCHANGE_NAME, "MyRouting.Route.RtKey1");
        channel.queueBind(FROM_QUEUE_NAME, EXCHANGE_NAME, "MyRouting.Route.RtKey2");
    
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            myMessage = new String(delivery.getBody(), "UTF-8");
            routingKeyReceived = delivery.getEnvelope().getRoutingKey();
        };
        channel.basicConsume(FROM_QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    
        channel.close();
        connection.close();
    
    } catch (Exception e) {
    }
    

    }
    <Strong/>

Reply To: RabbitMQ – How to get messages with certain routing keys?
Your information:





<a href="" title="" rel="" target=""> <blockquote cite=""> <code> <pre> <em> <strong> <del datetime=""> <ul> <ol start=""> <li> <img src="" border="" alt="" height="" width="">