5353import org .apache .logging .log4j .core .layout .PatternLayout ;
5454import org .apache .logging .log4j .core .util .Integers ;
5555
56+ import org .springframework .amqp .AmqpApplicationContextClosedException ;
5657import org .springframework .amqp .AmqpException ;
5758import org .springframework .amqp .core .DirectExchange ;
5859import org .springframework .amqp .core .Exchange ;
7273import org .springframework .amqp .rabbit .core .RabbitTemplate ;
7374import org .springframework .amqp .rabbit .support .RabbitExceptionTranslator ;
7475import org .springframework .amqp .utils .JavaUtils ;
76+ import org .springframework .context .ApplicationContext ;
77+ import org .springframework .context .event .ContextClosedEvent ;
78+ import org .springframework .context .support .GenericApplicationContext ;
7579import org .springframework .core .io .Resource ;
7680import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
7781import org .springframework .retry .RetryPolicy ;
@@ -354,6 +358,9 @@ protected void doSend(Event event, LogEvent logEvent, MessageProperties amqpProp
354358 message = postProcessMessageBeforeSend (message , event );
355359 this .rabbitTemplate .send (this .manager .exchangeName , routingKey , message );
356360 }
361+ catch (AmqpApplicationContextClosedException e ) {
362+ getHandler ().error ("Could not send log message " + logEvent .getMessage () + " appender is stopped" );
363+ }
357364 catch (AmqpException e ) {
358365 int retries = event .incrementRetries ();
359366 if (this .manager .async && retries < this .manager .maxSenderRetries ) {
@@ -380,7 +387,7 @@ public void run() {
380387 @ Override
381388 protected boolean stop (long timeout , TimeUnit timeUnit , boolean changeLifeCycleState ) {
382389 boolean stopped = super .stop (timeout , timeUnit , changeLifeCycleState );
383- return stopped || this .manager .stop (timeout , timeUnit );
390+ return this .manager .stop (timeout , timeUnit ) || stopped ;
384391 }
385392
386393 /**
@@ -449,6 +456,8 @@ protected static class AmqpManager extends AbstractManager {
449456
450457 private static final int DEFAULT_MAX_SENDER_RETRIES = 30 ;
451458
459+ private final ApplicationContext context = new GenericApplicationContext ();
460+
452461 /**
453462 * True to send events on separate threads.
454463 */
@@ -656,6 +665,7 @@ private boolean activateOptions() {
656665 .withNoConsoleNoAnsi (true )
657666 .build ();
658667 this .connectionFactory = new CachingConnectionFactory (rabbitConnectionFactory );
668+ this .connectionFactory .setApplicationContext (this .context );
659669 if (StringUtils .hasText (this .connectionName )) {
660670 this .connectionFactory .setConnectionNameStrategy (cf -> this .connectionName );
661671 }
@@ -745,6 +755,7 @@ protected boolean releaseSub(long timeout, TimeUnit timeUnit) {
745755 this .retryTimer .cancel ();
746756 this .senderPool .shutdownNow ();
747757 this .connectionFactory .destroy ();
758+ this .connectionFactory .onApplicationEvent (new ContextClosedEvent (this .context ));
748759 try {
749760 return this .senderPool .awaitTermination (timeout , timeUnit );
750761 }
0 commit comments