Hi,<div><br></div><div>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.</div><div><br></div><div>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.</div>
<div><br></div><div>Test case:</div><div><br></div><div><div>package foobar;</div><div><br></div><div>import com.rabbitmq.client.*;</div><div><br></div><div>import java.io.IOException;</div><div><br></div><div><br></div><div>
public class TestRabbitShutdownListener implements ShutdownListener, Runnable</div><div>{</div><div> private Connection connection;</div><div> private Channel channel;</div><div> private String exchange;</div><div> private int ticket;</div>
<div><br></div><div> public static void main(String[] args) throws IOException, InterruptedException</div><div> {</div><div> ConnectionParameters cp = new ConnectionParameters();</div><div> cp.setUsername("guest");</div>
<div> cp.setPassword("guest");</div><div><br></div><div> ConnectionFactory cf = new ConnectionFactory(cp);</div><div><br></div><div> TestRabbitShutdownListener trsl = new TestRabbitShutdownListener(</div>
<div> cf,</div><div> "foobar"</div><div> );</div><div><br></div><div> Thread t = new Thread(trsl);</div><div> t.start();</div><div> t.join();</div><div> Thread.sleep(5000);</div>
<div><br></div><div> }</div><div><br></div><div> public TestRabbitShutdownListener(ConnectionFactory connectionFactory,</div><div> String exchange) throws IOException</div><div> {</div>
<div> this.exchange = exchange;</div><div> this.connection = connectionFactory.newConnection("localhost");</div><div> this.connection.addShutdownListener(this);</div><div><br></div><div> setupChannel();</div>
<div> }</div><div><br></div><div> private void setupChannel() throws IOException</div><div> {</div><div> this.channel = connection.createChannel();</div><div> this.ticket = channel.accessRequest("/data");</div>
<div><br></div><div> channel.exchangeDeclare(</div><div> ticket,</div><div> exchange,</div><div> "fanout"</div><div> );</div><div> }</div><div><br></div><div> public void run()</div>
<div> {</div><div> while (true)</div><div> {</div><div> try</div><div> {</div><div> System.out.println("Sending msg..");</div><div><br></div><div> channel.basicPublish(ticket, exchange, "", MessageProperties.BASIC, new byte[]{});</div>
<div> Thread.sleep(1000);</div><div> }</div><div> catch (IOException e)</div><div> {</div><div> e.printStackTrace();</div><div> return;</div><div> }</div><div>
catch (InterruptedException e)</div><div> {</div><div> System.out.println("thread interrupted");</div><div> return;</div><div> }</div><div> }</div><div> }</div>
<div><br></div><div> public void shutdownCompleted(ShutdownSignalException e)</div><div> {</div><div> System.out.println("shutdownCompleted: " + e.getMessage());</div><div> e.printStackTrace();</div>
<div> }</div><div>}</div><div><br></div></div><div><div><br>-- <br>.henric.larsson.<br>
</div></div>