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;
029
030public class KafkaManager extends AbstractManager {
031
032    public static final String DEFAULT_TIMEOUT_MILLIS = "30000";
033
034    /**
035     * package-private access for testing.
036     */
037    static KafkaProducerFactory producerFactory = new DefaultKafkaProducerFactory();
038
039    private final Properties config = new Properties();
040    private Producer<byte[], byte[]> producer = null;
041    private final int timeoutMillis;
042
043    private final String topic;
044
045    public KafkaManager(final String name, final String topic, final Property[] properties) {
046        super(name);
047        this.topic = topic;
048        config.setProperty("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
049        config.setProperty("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
050        config.setProperty("batch.size", "0");
051        for (final Property property : properties) {
052            config.setProperty(property.getName(), property.getValue());
053        }
054        this.timeoutMillis = Integer.parseInt(config.getProperty("timeout.ms", DEFAULT_TIMEOUT_MILLIS));
055    }
056
057    @Override
058    public void releaseSub() {
059        if (producer != null) {
060            // This thread is a workaround for this Kafka issue: https://issues.apache.org/jira/browse/KAFKA-1660
061            final Thread closeThread = new Thread(new Runnable() {
062                @Override
063                public void run() {
064                    producer.close();
065                }
066            });
067            closeThread.setName("KafkaManager-CloseThread");
068            closeThread.setDaemon(true); // avoid blocking JVM shutdown
069            closeThread.start();
070            try {
071                closeThread.join(timeoutMillis);
072            } catch (final InterruptedException ignore) {
073                // ignore
074            }
075        }
076    }
077
078    public void send(final byte[] msg) throws ExecutionException, InterruptedException, TimeoutException {
079        if (producer != null) {
080            producer.send(new ProducerRecord<byte[], byte[]>(topic, msg)).get(timeoutMillis, TimeUnit.MILLISECONDS);
081        }
082    }
083
084    public void startup() {
085        producer = producerFactory.newKafkaProducer(config);
086    }
087
088}