<div dir="ltr">Hello,<div><br></div><div>Since a week ago, I was having a lot of "com.rabbitmq.client.AlreadyClosedException: clean connection shutdown; reason: Attempt to use closed channel" the worst part was that the "DefaultExceptionHandler" was caught without being rethrowed, so I was unable to automatically restart my app when this occured. I updated to the last amqp-java client version but nothing changed.</div><div><br></div><div>So I extended AMQConnection to specify my own exception handler in order to crash the app (with System.exit()) when this occured.</div><div><br></div><div><div>public ExtendedAMQConnection(ConnectionFactory factory, Integer workerCount) throws Exception {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; super(factory.getUsername(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.getPassword(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SocketFrameHandler(factory.getSocketFactory().createSocket(factory.getHost(), factory.getPort())),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Executors.newFixedThreadPool(workerCount),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.getVirtualHost(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.getClientProperties(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.getRequestedFrameMax(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.getRequestedChannelMax(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.getRequestedHeartbeat(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.getSaslConfig(),</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DefaultExceptionHandler() {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void handleConsumerException(Channel channel,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Throwable exception,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Consumer consumer,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String consumerTag,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String methodName) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: change this to call 4-parameter close and make 6-parm one private</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((AMQConnection) channel.getConnection())</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .close(AMQP.INTERNAL_ERROR,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Internal error in Consumer " + consumerTag,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exception,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -1,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Throwable e) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.error("handleConsumerException", e);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(1);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</div><div>&nbsp; &nbsp; &nbsp; &nbsp; this.start();</div><div>&nbsp; &nbsp; }</div></div><div><br></div><div>But after some time I understood that it was not the main issue, the app was still throwing the same error without doing anything at all to fix it so I added a listener to channel shutdown event:</div><div><br></div><div>Connection conn = new BringrAMQConnection(factory, workerCount);</div><div>Channel channel = conn.createChannel();<br></div><div><div>channel.addShutdownListener(new ShutdownListener() {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void shutdownCompleted(ShutdownSignalException e) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.error("RabbitMQ[channel] Shutdown. Stopping the server");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(1);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; });</div></div><div><br></div><div>and now I got a lot of restarts. I'm not sure I clearly understood if I should do something when "shutdownCompleted" occurs (I never stopped RabbitMQ and RabbitMQ was always running when this happened), or if amqp-client can handle this by itself.</div><div><br></div><div>I use&nbsp;com.rabbitmq:amqp-client:3.1.4 with a cluster of two mirrored RabbitMQ v3.1.1.</div><div><br></div><div><div># rabbitmqctl status</div><div>Status of node rabbit@ir ...</div><div>[{pid,19842},</div><div>&nbsp;{running_applications,</div><div>&nbsp; &nbsp; &nbsp;[{rabbitmq_management,"RabbitMQ Management Console","3.1.1"},</div><div>&nbsp; &nbsp; &nbsp; {rabbitmq_management_agent,"RabbitMQ Management Agent","3.1.1"},</div><div>&nbsp; &nbsp; &nbsp; {rabbit,"RabbitMQ","3.1.1"},</div><div>&nbsp; &nbsp; &nbsp; {os_mon,"CPO &nbsp;CXC 138 46","2.2.9"},</div><div>&nbsp; &nbsp; &nbsp; {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.1.1"},</div><div>&nbsp; &nbsp; &nbsp; {webmachine,"webmachine","1.9.1-rmq3.1.1-git52e62bc"},</div><div>&nbsp; &nbsp; &nbsp; {mochiweb,"MochiMedia Web Server","2.3.1-rmq3.1.1-gitd541e9a"},</div><div>&nbsp; &nbsp; &nbsp; {xmerl,"XML parser","1.3.1"},</div><div>&nbsp; &nbsp; &nbsp; {inets,"INETS &nbsp;CXC 138 49","5.9"},</div><div>&nbsp; &nbsp; &nbsp; {amqp_client,"RabbitMQ AMQP Client","3.1.1"},</div><div>&nbsp; &nbsp; &nbsp; {mnesia,"MNESIA &nbsp;CXC 138 12","4.7"},</div><div>&nbsp; &nbsp; &nbsp; {sasl,"SASL &nbsp;CXC 138 11","2.2.1"},</div><div>&nbsp; &nbsp; &nbsp; {stdlib,"ERTS &nbsp;CXC 138 10","1.18.1"},</div><div>&nbsp; &nbsp; &nbsp; {kernel,"ERTS &nbsp;CXC 138 10","2.15.1"}]},</div><div>&nbsp;{os,{unix,linux}},</div><div>&nbsp;{erlang_version,</div><div>&nbsp; &nbsp; &nbsp;"Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:24:24] [async-threads:30] [hipe] [kernel-poll:true]\n"},</div><div>&nbsp;{memory,</div><div>&nbsp; &nbsp; &nbsp;[{total,122151960},</div><div>&nbsp; &nbsp; &nbsp; {connection_procs,22551824},</div><div>&nbsp; &nbsp; &nbsp; {queue_procs,9699000},</div><div>&nbsp; &nbsp; &nbsp; {plugins,415976},</div><div>&nbsp; &nbsp; &nbsp; {other_proc,9745810},</div><div>&nbsp; &nbsp; &nbsp; {mnesia,650608},</div><div>&nbsp; &nbsp; &nbsp; {mgmt_db,10643096},</div><div>&nbsp; &nbsp; &nbsp; {msg_index,431544},</div><div>&nbsp; &nbsp; &nbsp; {other_ets,2452056},</div><div>&nbsp; &nbsp; &nbsp; {binary,35320928},</div><div>&nbsp; &nbsp; &nbsp; {code,18521111},</div><div>&nbsp; &nbsp; &nbsp; {atom,2525529},</div><div>&nbsp; &nbsp; &nbsp; {other_system,9194478}]},</div><div>&nbsp;{vm_memory_high_watermark,0.4},</div><div>&nbsp;{vm_memory_limit,27064667340},</div><div>&nbsp;{disk_free_limit,1000000000},</div><div>&nbsp;{disk_free,2864417656832},</div><div>&nbsp;{file_descriptors,</div><div>&nbsp; &nbsp; &nbsp;[{total_limit,924},</div><div>&nbsp; &nbsp; &nbsp; {total_used,261},</div><div>&nbsp; &nbsp; &nbsp; {sockets_limit,829},</div><div>&nbsp; &nbsp; &nbsp; {sockets_used,227}]},</div><div>&nbsp;{processes,[{limit,1048576},{used,2804}]},</div><div>&nbsp;{run_queue,0},</div><div>&nbsp;{uptime,8096817}]</div></div><div><br></div><div>I'm able send you privately the output of rabbitmqctl report if you need it.</div><div><br></div></div>