View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.core.appender.mom.kafka;
19  
20  import java.nio.charset.StandardCharsets;
21  import java.util.Objects;
22  import java.util.Properties;
23  import java.util.concurrent.ExecutionException;
24  import java.util.concurrent.Future;
25  import java.util.concurrent.TimeUnit;
26  import java.util.concurrent.TimeoutException;
27  
28  import org.apache.kafka.clients.producer.Callback;
29  import org.apache.kafka.clients.producer.Producer;
30  import org.apache.kafka.clients.producer.ProducerRecord;
31  import org.apache.kafka.clients.producer.RecordMetadata;
32  import org.apache.logging.log4j.core.LoggerContext;
33  import org.apache.logging.log4j.core.appender.AbstractManager;
34  import org.apache.logging.log4j.core.appender.ManagerFactory;
35  import org.apache.logging.log4j.core.config.Property;
36  import org.apache.logging.log4j.core.util.Log4jThread;
37  
38  public class KafkaManager extends AbstractManager {
39  
40      public static final String DEFAULT_TIMEOUT_MILLIS = "30000";
41  
42      /**
43       * package-private access for testing.
44       */
45      static KafkaProducerFactory producerFactory = new DefaultKafkaProducerFactory();
46  
47      private final Properties config = new Properties();
48      private Producer<byte[], byte[]> producer;
49      private final int timeoutMillis;
50  
51      private final String topic;
52      private final String key;
53      private final boolean syncSend;
54      private static final KafkaManagerFactory factory = new KafkaManagerFactory();
55  
56      /*
57       * The Constructor should have been declared private as all Managers are create by the internal factory;
58       */
59      public KafkaManager(final LoggerContext loggerContext, final String name, final String topic, final boolean syncSend,
60                          final Property[] properties, final String key) {
61          super(loggerContext, name);
62          this.topic = Objects.requireNonNull(topic, "topic");
63          this.syncSend = syncSend;
64          config.setProperty("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
65          config.setProperty("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
66          config.setProperty("batch.size", "0");
67          for (final Property property : properties) {
68              config.setProperty(property.getName(), property.getValue());
69          }
70  
71          this.key = key;
72  
73          this.timeoutMillis = Integer.parseInt(config.getProperty("timeout.ms", DEFAULT_TIMEOUT_MILLIS));
74      }
75  
76      @Override
77      public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
78          if (timeout > 0) {
79              closeProducer(timeout, timeUnit);
80          } else {
81              closeProducer(timeoutMillis, TimeUnit.MILLISECONDS);
82          }
83          return true;
84      }
85  
86      private void closeProducer(final long timeout, final TimeUnit timeUnit) {
87          if (producer != null) {
88              // This thread is a workaround for this Kafka issue: https://issues.apache.org/jira/browse/KAFKA-1660
89             final Thread closeThread = new Log4jThread(new Runnable() {
90                  @Override
91                  public void run() {
92                      if (producer != null) {
93                          producer.close();
94                      }
95                  }
96              }, "KafkaManager-CloseThread");
97              closeThread.setDaemon(true); // avoid blocking JVM shutdown
98              closeThread.start();
99              try {
100                 closeThread.join(timeUnit.toMillis(timeout));
101             } catch (final InterruptedException ignore) {
102                 Thread.currentThread().interrupt();
103                 // ignore
104             }
105         }
106     }
107 
108     public void send(final byte[] msg) throws ExecutionException, InterruptedException, TimeoutException {
109         if (producer != null) {
110             byte[] newKey = null;
111 
112             if(key != null && key.contains("${")) {
113                 newKey = getLoggerContext().getConfiguration().getStrSubstitutor().replace(key).getBytes(StandardCharsets.UTF_8);
114             } else if (key != null) {
115                 newKey = key.getBytes(StandardCharsets.UTF_8);
116             }
117 
118             final ProducerRecord<byte[], byte[]> newRecord = new ProducerRecord<>(topic, newKey, msg);
119             if (syncSend) {
120                 final Future<RecordMetadata> response = producer.send(newRecord);
121                 response.get(timeoutMillis, TimeUnit.MILLISECONDS);
122             } else {
123                 producer.send(newRecord, new Callback() {
124                     @Override
125                     public void onCompletion(final RecordMetadata metadata, final Exception e) {
126                         if (e != null) {
127                             LOGGER.error("Unable to write to Kafka in appender [" + getName() + "]", e);
128                         }
129                     }
130                 });
131             }
132         }
133     }
134 
135     public void startup() {
136         producer = producerFactory.newKafkaProducer(config);
137     }
138 
139     public String getTopic() {
140         return topic;
141     }
142 
143     public static KafkaManager getManager(final LoggerContext loggerContext, final String name, final String topic,
144         final boolean syncSend, final Property[] properties, final String key) {
145         StringBuilder sb = new StringBuilder(name);
146         for (Property prop: properties) {
147             sb.append(" ").append(prop.getName()).append("=").append(prop.getValue());
148         }
149         return getManager(sb.toString(), factory, new FactoryData(loggerContext, topic, syncSend, properties, key));
150     }
151 
152     private static class FactoryData {
153         private final LoggerContext loggerContext;
154         private final String topic;
155         private final boolean syncSend;
156         private final Property[] properties;
157         private final String key;
158 
159         public FactoryData(final LoggerContext loggerContext, final String topic, final boolean syncSend,
160             final Property[] properties, final String key) {
161             this.loggerContext = loggerContext;
162             this.topic = topic;
163             this.syncSend = syncSend;
164             this.properties = properties;
165             this.key = key;
166         }
167 
168     }
169 
170     private static class KafkaManagerFactory implements ManagerFactory<KafkaManager, FactoryData> {
171         @Override
172         public KafkaManager createManager(String name, FactoryData data) {
173             return new KafkaManager(data.loggerContext, name, data.topic, data.syncSend, data.properties, data.key);
174         }
175     }
176 
177 }