5555import org .springframework .amqp .core .MessageDeliveryMode ;
5656import org .springframework .amqp .core .MessageProperties ;
5757import org .springframework .amqp .core .TopicExchange ;
58- import org .springframework .amqp .rabbit .connection .AbstractConnectionFactory ;
5958import org .springframework .amqp .rabbit .connection .CachingConnectionFactory ;
6059import org .springframework .amqp .rabbit .connection .RabbitConnectionFactoryBean ;
6160import org .springframework .amqp .rabbit .core .DeclareExchangeConnectionListener ;
6261import org .springframework .amqp .rabbit .core .RabbitAdmin ;
6362import org .springframework .amqp .rabbit .core .RabbitTemplate ;
6463import org .springframework .amqp .rabbit .support .LogAppenderUtils ;
64+ import org .springframework .context .ApplicationContext ;
65+ import org .springframework .context .event .ContextClosedEvent ;
66+ import org .springframework .context .support .GenericApplicationContext ;
6567import org .springframework .retry .RetryPolicy ;
6668import org .springframework .retry .policy .SimpleRetryPolicy ;
6769import org .springframework .retry .support .RetryTemplate ;
@@ -308,6 +310,9 @@ protected void doSend(final Event event, LogEvent logEvent, MessageProperties am
308310 message = postProcessMessageBeforeSend (message , event );
309311 this .rabbitTemplate .send (this .manager .exchangeName , routingKey , message );
310312 }
313+ catch (IllegalStateException e ) {
314+ getHandler ().error ("Could not send log message " + logEvent .getMessage () + " appender is stopped" );
315+ }
311316 catch (AmqpException e ) {
312317 int retries = event .incrementRetries ();
313318 if (this .manager .async && retries < this .manager .maxSenderRetries ) {
@@ -334,7 +339,7 @@ public void run() {
334339 @ Override
335340 protected boolean stop (long timeout , TimeUnit timeUnit , boolean changeLifeCycleState ) {
336341 boolean stopped = super .stop (timeout , timeUnit , changeLifeCycleState );
337- return stopped & this .manager .stop (timeout , timeUnit );
342+ return this .manager .stop (timeout , timeUnit ) || stopped ;
338343 }
339344
340345 /**
@@ -397,6 +402,8 @@ public int incrementRetries() {
397402
398403 protected static class AmqpManager extends AbstractManager {
399404
405+ private final ApplicationContext context = new GenericApplicationContext ();
406+
400407 /**
401408 * True to send events on separate threads.
402409 */
@@ -440,7 +447,7 @@ protected static class AmqpManager extends AbstractManager {
440447 /**
441448 * RabbitMQ ConnectionFactory.
442449 */
443- private AbstractConnectionFactory connectionFactory ;
450+ private CachingConnectionFactory connectionFactory ;
444451
445452 /**
446453 * RabbitMQ host to connect to.
@@ -537,6 +544,7 @@ private boolean activateOptions() {
537544 .replaceAll ("%X\\ {applicationId\\ }" , this .applicationId ),
538545 null , null , null , Charset .forName (this .charset ), false , true , null , null );
539546 this .connectionFactory = new CachingConnectionFactory (createRabbitConnectionFactory ());
547+ this .connectionFactory .setApplicationContext (this .context );
540548 if (this .addresses != null ) {
541549 this .connectionFactory .setAddresses (this .addresses );
542550 }
@@ -588,6 +596,7 @@ protected boolean releaseSub(long timeout, TimeUnit timeUnit) {
588596 this .retryTimer .cancel ();
589597 this .senderPool .shutdownNow ();
590598 this .connectionFactory .destroy ();
599+ this .connectionFactory .onApplicationEvent (new ContextClosedEvent (this .context ));
591600 try {
592601 return this .senderPool .awaitTermination (timeout , timeUnit );
593602 }
0 commit comments