1

I have a program in Java that sends messages to RabbitMQ. All I know is the exchange name. No queues, bindings, and so on.

My question is this: how can I see if the program sends these successfully, knowing only the exchange name?

Thanks.

Regards, Serban

Serban Stoenescu
  • 3,136
  • 3
  • 22
  • 41

3 Answers3

2

You can enable publisher confirmation with RabbitMQ. It's like having a send-transaction, where RabbitMQ will tell you whether or not the message was sent successfully.

Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
0

Please look here: https://www.rabbitmq.com/tutorials/tutorial-three-java.html

String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, "EXCHANGE_NAME", "");

System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
Consumer consumer = new DefaultConsumer(channel) {
  @Override
  public void handleDelivery(String consumerTag, 
                             Envelope envelope,
                             AMQP.BasicProperties properties, 
                             byte[] body) throws IOException 
{
    String message = new String(body, "UTF-8");
    System.out.println(" [x] Received '" + message + "'");
  }
};
channel.basicConsume(queueName, true, consumer);

In a few words, you have to:

  1. create a queue, in this case anonymous queue
  2. bind the queue to your exchange

It is important to know what kind of the exchange you have since the binding can change, between fanout or topic or direct

In this example is fanout

Gabriele Santomaggio
  • 21,656
  • 4
  • 52
  • 52
0

Assume that we have RabbitMQ Exchange we need to create an queue to push the message to the exchange and consume it from the queue as following


    private static final String EXCHANGE_NAME = "2022";
    private static final String QUEUE_NAME = "2022";
    private final static boolean durable = true;

// now we need to create a connection to rabbitmq server //

        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        Connection conn = factory.newConnection();
       // create rabbitmq connection chaneel        
       Channel channel = conn.createChannel();
      //Declare Exchange // 
      channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);
     // push message to rabbitmq exchange 

channel.basicPublish(EXCHANGE_NAME, "routingkey", null, yourmessage.getBytes());



the above work as producer now we need to create queue consumer


    private static final String EXCHANGE_NAME = "2022";
    private static final String QUEUE_NAME = "2022";
    private final static boolean durable = true;

// now we need to create a connection to rabbitmq server //

        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        Connection conn = factory.newConnection();
        // create rabbitmq connection chaneel       
        Channel channel = conn.createChannel();


          channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);

          //Queue Declare //
          channel.queueDeclare(QUEUE_NAME, true, false, false, null);
         //Queue bind //
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routingkey");
        // Queue Consume // 
        QueueingConsumer consumer = new QueueingConsumer(channel);

  while (true)
        {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());

            System.out.println(" [x] Received '" + message + "'");


        }