53
53
import org .apache .logging .log4j .core .layout .PatternLayout ;
54
54
import org .apache .logging .log4j .core .util .Integers ;
55
55
56
+ import org .springframework .amqp .AmqpApplicationContextClosedException ;
56
57
import org .springframework .amqp .AmqpException ;
57
58
import org .springframework .amqp .core .DirectExchange ;
58
59
import org .springframework .amqp .core .Exchange ;
72
73
import org .springframework .amqp .rabbit .core .RabbitTemplate ;
73
74
import org .springframework .amqp .rabbit .support .RabbitExceptionTranslator ;
74
75
import 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 ;
75
79
import org .springframework .core .io .Resource ;
76
80
import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
77
81
import org .springframework .retry .RetryPolicy ;
@@ -354,6 +358,9 @@ protected void doSend(Event event, LogEvent logEvent, MessageProperties amqpProp
354
358
message = postProcessMessageBeforeSend (message , event );
355
359
this .rabbitTemplate .send (this .manager .exchangeName , routingKey , message );
356
360
}
361
+ catch (AmqpApplicationContextClosedException e ) {
362
+ getHandler ().error ("Could not send log message " + logEvent .getMessage () + " appender is stopped" );
363
+ }
357
364
catch (AmqpException e ) {
358
365
int retries = event .incrementRetries ();
359
366
if (this .manager .async && retries < this .manager .maxSenderRetries ) {
@@ -380,7 +387,7 @@ public void run() {
380
387
@ Override
381
388
protected boolean stop (long timeout , TimeUnit timeUnit , boolean changeLifeCycleState ) {
382
389
boolean stopped = super .stop (timeout , timeUnit , changeLifeCycleState );
383
- return stopped || this .manager .stop (timeout , timeUnit );
390
+ return this .manager .stop (timeout , timeUnit ) || stopped ;
384
391
}
385
392
386
393
/**
@@ -449,6 +456,8 @@ protected static class AmqpManager extends AbstractManager {
449
456
450
457
private static final int DEFAULT_MAX_SENDER_RETRIES = 30 ;
451
458
459
+ private final ApplicationContext context = new GenericApplicationContext ();
460
+
452
461
/**
453
462
* True to send events on separate threads.
454
463
*/
@@ -656,6 +665,7 @@ private boolean activateOptions() {
656
665
.withNoConsoleNoAnsi (true )
657
666
.build ();
658
667
this .connectionFactory = new CachingConnectionFactory (rabbitConnectionFactory );
668
+ this .connectionFactory .setApplicationContext (this .context );
659
669
if (StringUtils .hasText (this .connectionName )) {
660
670
this .connectionFactory .setConnectionNameStrategy (cf -> this .connectionName );
661
671
}
@@ -745,6 +755,7 @@ protected boolean releaseSub(long timeout, TimeUnit timeUnit) {
745
755
this .retryTimer .cancel ();
746
756
this .senderPool .shutdownNow ();
747
757
this .connectionFactory .destroy ();
758
+ this .connectionFactory .onApplicationEvent (new ContextClosedEvent (this .context ));
748
759
try {
749
760
return this .senderPool .awaitTermination (timeout , timeUnit );
750
761
}
0 commit comments