[rabbitmq-discuss] ShutdownListener not triggered on server shutdown

Henric Larsson henric.larsson at gmail.com
Mon Nov 3 23:58:13 GMT 2008


Hi,
I'm using the rabbit java client and rabbitmq server for some simple
messaging and I can't seem to get the shutdown listener to work as expected.

Basically, I'm attaching a shutdownlistener to the connection and then start
sending messages every second. If I shutdown the server I would expect the
shutdownlistener to be invoked at some point on the client but this never
occurs. I only get an AlreadyClosedException when calling basicPublish on
the channel.

Test case:

package foobar;

import com.rabbitmq.client.*;

import java.io.IOException;


public class TestRabbitShutdownListener implements ShutdownListener,
Runnable
{
   private Connection connection;
   private Channel channel;
   private String exchange;
   private int ticket;

   public static void main(String[] args) throws IOException,
InterruptedException
   {
      ConnectionParameters cp = new ConnectionParameters();
      cp.setUsername("guest");
      cp.setPassword("guest");

      ConnectionFactory cf = new ConnectionFactory(cp);

      TestRabbitShutdownListener trsl = new TestRabbitShutdownListener(
              cf,
              "foobar"
      );

      Thread t = new Thread(trsl);
      t.start();
      t.join();
      Thread.sleep(5000);

   }

   public TestRabbitShutdownListener(ConnectionFactory connectionFactory,
                                     String exchange) throws IOException
   {
      this.exchange = exchange;
      this.connection = connectionFactory.newConnection("localhost");
      this.connection.addShutdownListener(this);

      setupChannel();
   }

   private void setupChannel() throws IOException
   {
      this.channel = connection.createChannel();
      this.ticket = channel.accessRequest("/data");

      channel.exchangeDeclare(
              ticket,
              exchange,
              "fanout"
      );
   }

   public void run()
   {
      while (true)
      {
         try
         {
            System.out.println("Sending msg..");

            channel.basicPublish(ticket, exchange, "",
MessageProperties.BASIC, new byte[]{});
            Thread.sleep(1000);
         }
         catch (IOException e)
         {
            e.printStackTrace();
            return;
         }
         catch (InterruptedException e)
         {
            System.out.println("thread interrupted");
            return;
         }
      }
   }

   public void shutdownCompleted(ShutdownSignalException e)
   {
      System.out.println("shutdownCompleted: " + e.getMessage());
      e.printStackTrace();
   }
}


-- 
.henric.larsson.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20081104/bd128294/attachment.htm 


More information about the rabbitmq-discuss mailing list