Skip to content

Commit

Permalink
Revert "GH-1148: Revert Log4j Appender Change"
Browse files Browse the repository at this point in the history
This reverts commit bb83aa7.
  • Loading branch information
garyrussell committed Feb 12, 2020
1 parent fdda7a9 commit 7af0f90
Showing 1 changed file with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand All @@ -667,7 +677,6 @@ private boolean activateOptions() {
this.clientConnectionProperties);
}
setUpExchangeDeclaration();
this.senderPool = Executors.newCachedThreadPool();
return true;
}
return false;
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 7af0f90

Please sign in to comment.