5151import org .apache .logging .log4j .core .layout .PatternLayout ;
5252import org .apache .logging .log4j .core .util .Integers ;
5353
54+ import org .springframework .amqp .AmqpApplicationContextClosedException ;
5455import org .springframework .amqp .AmqpException ;
5556import org .springframework .amqp .core .DirectExchange ;
5657import org .springframework .amqp .core .Exchange ;
7071import org .springframework .amqp .rabbit .core .RabbitTemplate ;
7172import org .springframework .amqp .rabbit .support .RabbitExceptionTranslator ;
7273import org .springframework .amqp .utils .JavaUtils ;
74+ import org .springframework .context .ApplicationContext ;
75+ import org .springframework .context .event .ContextClosedEvent ;
76+ import org .springframework .context .support .GenericApplicationContext ;
7377import org .springframework .core .io .Resource ;
7478import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
7579import org .springframework .retry .RetryPolicy ;
@@ -360,6 +364,9 @@ protected void doSend(Event event, LogEvent logEvent, MessageProperties amqpProp
360364 message = postProcessMessageBeforeSend (message , event );
361365 this .rabbitTemplate .send (this .manager .exchangeName , routingKey , message );
362366 }
367+ catch (AmqpApplicationContextClosedException e ) {
368+ getHandler ().error ("Could not send log message " + logEvent .getMessage () + " appender is stopped" );
369+ }
363370 catch (AmqpException e ) {
364371 int retries = event .incrementRetries ();
365372 if (this .manager .async && retries < this .manager .maxSenderRetries ) {
@@ -386,7 +393,7 @@ public void run() {
386393 @ Override
387394 protected boolean stop (long timeout , TimeUnit timeUnit , boolean changeLifeCycleState ) {
388395 boolean stopped = super .stop (timeout , timeUnit , changeLifeCycleState );
389- return stopped || this .manager .stop (timeout , timeUnit );
396+ return this .manager .stop (timeout , timeUnit ) || stopped ;
390397 }
391398
392399 /**
@@ -455,6 +462,8 @@ protected static class AmqpManager extends AbstractManager {
455462
456463 private static final int DEFAULT_MAX_SENDER_RETRIES = 30 ;
457464
465+ private final ApplicationContext context = new GenericApplicationContext ();
466+
458467 /**
459468 * True to send events on separate threads.
460469 */
@@ -662,6 +671,7 @@ private boolean activateOptions() {
662671 .withNoConsoleNoAnsi (true )
663672 .build ();
664673 this .connectionFactory = new CachingConnectionFactory (rabbitConnectionFactory );
674+ this .connectionFactory .setApplicationContext (this .context );
665675 if (StringUtils .hasText (this .connectionName )) {
666676 this .connectionFactory .setConnectionNameStrategy (cf -> this .connectionName );
667677 }
@@ -751,6 +761,7 @@ protected boolean releaseSub(long timeout, TimeUnit timeUnit) {
751761 this .retryTimer .cancel ();
752762 this .senderPool .shutdownNow ();
753763 this .connectionFactory .destroy ();
764+ this .connectionFactory .onApplicationEvent (new ContextClosedEvent (this .context ));
754765 try {
755766 return this .senderPool .awaitTermination (timeout , timeUnit );
756767 }
0 commit comments