diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j2/AmqpAppender.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j2/AmqpAppender.java index 0904f2fe7a..27d6840f44 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j2/AmqpAppender.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/log4j2/AmqpAppender.java @@ -126,7 +126,7 @@ public class AmqpAppender extends AbstractAppender { /** * The template. */ - private final RabbitTemplate rabbitTemplate = new RabbitTemplate(); + private RabbitTemplate rabbitTemplate; /** * Where LoggingEvents are queued to send. @@ -250,18 +250,12 @@ public static Builder newBuilder() { * Submit the required number of senders into the pool. */ private void startSenders() { - this.rabbitTemplate.setConnectionFactory(this.manager.connectionFactory); if (this.manager.async) { + this.manager.senderPool = Executors.newCachedThreadPool(); for (int i = 0; i < this.manager.senderPoolSize; i++) { this.manager.senderPool.submit(new EventSender()); } } - else if (this.manager.maxSenderRetries > 0) { - RetryTemplate retryTemplate = new RetryTemplate(); - RetryPolicy retryPolicy = new SimpleRetryPolicy(this.manager.maxSenderRetries); - retryTemplate.setRetryPolicy(retryPolicy); - this.rabbitTemplate.setRetryTemplate(retryTemplate); - } } @Override @@ -287,6 +281,22 @@ protected Message postProcessMessageBeforeSend(Message message, Event event) { } protected void sendEvent(Event event, Map properties) { + synchronized (this) { + if (this.rabbitTemplate == null) { + if (this.manager.activateOptions()) { + this.rabbitTemplate = new RabbitTemplate(this.manager.connectionFactory); + if (!this.manager.async && this.manager.maxSenderRetries > 0) { + RetryTemplate retryTemplate = new RetryTemplate(); + RetryPolicy retryPolicy = new SimpleRetryPolicy(this.manager.maxSenderRetries); + retryTemplate.setRetryPolicy(retryPolicy); + this.rabbitTemplate.setRetryTemplate(retryTemplate); + } + } + else { + throw new AmqpException("Cannot create template"); + } + } + } LogEvent logEvent = event.getEvent(); String name = logEvent.getLoggerName(); Level level = logEvent.getLevel(); @@ -352,7 +362,7 @@ protected void doSend(Event event, LogEvent logEvent, MessageProperties amqpProp message = new Message(msgBody.toString().getBytes(), amqpProps); //NOSONAR (default charset) } message = postProcessMessageBeforeSend(message, event); - this.rabbitTemplate.send(this.manager.exchangeName, routingKey, message); + this.rabbitTemplate.send(this.manager.exchangeName, routingKey, message); // NOSONAR (sync) } catch (AmqpException e) { int retries = event.incrementRetries(); @@ -645,7 +655,7 @@ protected AmqpManager(LoggerContext loggerContext, String name) { super(loggerContext, name); } - private boolean activateOptions() { + boolean activateOptions() { ConnectionFactory rabbitConnectionFactory = createRabbitConnectionFactory(); if (rabbitConnectionFactory != null) { Assert.state(this.applicationId != null, "applicationId is required"); @@ -667,7 +677,6 @@ private boolean activateOptions() { this.clientConnectionProperties); } setUpExchangeDeclaration(); - this.senderPool = Executors.newCachedThreadPool(); return true; } return false; @@ -1182,11 +1191,8 @@ public AmqpAppender build() { } AmqpAppender appender = buildInstance(this.name, this.filter, theLayout, this.ignoreExceptions, manager, eventQueue); - if (manager.activateOptions()) { - appender.startSenders(); - return appender; - } - return null; + appender.startSenders(); + return appender; } /**