001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017
018package org.apache.logging.log4j.core.appender.mom.kafka;
019
020import java.util.Properties;
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.TimeUnit;
023import java.util.concurrent.TimeoutException;
024
025import org.apache.kafka.clients.producer.Producer;
026import org.apache.kafka.clients.producer.ProducerRecord;
027import org.apache.logging.log4j.core.appender.AbstractManager;
028import org.apache.logging.log4j.core.config.Property;
029import org.apache.logging.log4j.core.util.Log4jThread;
030
031public class KafkaManager extends AbstractManager {
032
033    public static final String DEFAULT_TIMEOUT_MILLIS = "30000";
034
035    /**
036     * package-private access for testing.
037     */
038    static KafkaProducerFactory producerFactory = new DefaultKafkaProducerFactory();
039
040    private final Properties config = new Properties();
041    private Producer<byte[], byte[]> producer = null;
042    private final int timeoutMillis;
043
044    private final String topic;
045
046    public KafkaManager(final String name, final String topic, final Property[] properties) {
047        super(name);
048        this.topic = topic;
049        config.setProperty("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
050        config.setProperty("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
051        config.setProperty("batch.size", "0");
052        for (final Property property : properties) {
053            config.setProperty(property.getName(), property.getValue());
054        }
055        this.timeoutMillis = Integer.parseInt(config.getProperty("timeout.ms", DEFAULT_TIMEOUT_MILLIS));
056    }
057
058    @Override
059    public void releaseSub() {
060        if (producer != null) {
061            // This thread is a workaround for this Kafka issue: https://issues.apache.org/jira/browse/KAFKA-1660
062            final Thread closeThread = new Log4jThread(new Runnable() {
063                @Override
064                public void run() {
065                    producer.close();
066                }
067            });
068            closeThread.setName("KafkaManager-CloseThread");
069            closeThread.setDaemon(true); // avoid blocking JVM shutdown
070            closeThread.start();
071            try {
072                closeThread.join(timeoutMillis);
073            } catch (final InterruptedException ignore) {
074                // ignore
075            }
076        }
077    }
078
079    public void send(final byte[] msg) throws ExecutionException, InterruptedException, TimeoutException {
080        if (producer != null) {
081            producer.send(new ProducerRecord<byte[], byte[]>(topic, msg)).get(timeoutMillis, TimeUnit.MILLISECONDS);
082        }
083    }
084
085    public void startup() {
086        producer = producerFactory.newKafkaProducer(config);
087    }
088
089    public String getTopic() {
090        return topic;
091    }
092
093}