1
votes

I have a JAVA application which creates consumers that listen to rabbitmq . I need to know the started consumer is still working fine and if not then i need to restart the consumer.

Is their any way i can do that. Currently my main application creates an Executor thread pool and passes this executor while creating new connection.

ExecutorService executor = Executors.newFixedThreadPool(30);
Connection connection = factory.newConnection(executor);

The main method then create 30 consumerApp object by calling constructor with new channel as argument and call the listen() method

for(int i=0;i<=30;i++) {
 ConsumerApp consumer = new ConsumerApp(i,connection.createChanell());
 consumer.listen() }

The listen method in consumerApp listen to a queue and start a DefaultConsumer Object which simply prints the received message

listen() { 
  try {
  channel.queueDeclare("test-queue-name", false, false, false, null);
  }
catch {
  System.out.println("Exception on creating Queue")
  }
Consumer consumer = new DefaultConsumer(this.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 in consumer '"+consumerId+" "+  message + "'");
        }
    };
 //Now starting the consumer
  try {
    channel.basicConsume(QUEUE_NAME, true, consumer);   
   }
    catch (ShutdownSignalException | IOException ex) {
        ex.printStackTrace();
  }
 }

I want to know is their any way i can check the consumer is active . My idea is to catch the shutdown signal exception and recreate the consumer object and recall the listen method . Is this necessary as rabbitmq auto recovers and connnect back. ? But how can i ensure this ?

Is this any way achievable using the threadpool passed to rabbitmq connector.

I am using latest version of rabbitmq client 5.3.0

1

1 Answers

0
votes

Consumer has different methods that can help you track the state of your consumer. You're likely to be interested in handleConsumeOk and in handleCancel. Automatic connection recovery will indeed re-register consumers after a connection failure, but that doesn't prevent you from following their state manually to e.g. expose some information on JMX.