Java tutorial
/** * Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. * * THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd., * WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY * CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ package acromusashi.kafka.log.producer; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import kafka.producer.ProducerConfig; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import acromusashi.kafka.log.producer.util.ProducerConfigConverter; import acromusashi.kafka.log.producer.util.YamlReadUtil; /** * Linux?Tail???KafkaBroker???KafkaProducer * * @author kimura */ public class LinuxApacheLogProducer { /** Logger */ private static final Logger logger = LoggerFactory.getLogger(LinuxApacheLogProducer.class); /** * ????? */ public LinuxApacheLogProducer() { } /** * ?<br/> * <br/> * ?/?<br/> * <ul> * <li>-c LinuxApacheLogProducer(</li> * <li>-h </li> * </ul> * * @param args */ public static void main(String... args) { LinuxApacheLogProducer producer = new LinuxApacheLogProducer(); producer.startProducer(args); } /** * ???Producer? * * @param args */ protected void startProducer(String... args) { Options cliOptions = createOptions(); CommandLineParser parser = new PosixParser(); CommandLine commandLine = null; HelpFormatter help = new HelpFormatter(); try { commandLine = parser.parse(cliOptions, args); } catch (ParseException pex) { help.printHelp(LinuxApacheLogProducer.class.getName(), cliOptions, true); return; } if (commandLine.hasOption("h")) { // ?????????? help.printHelp(LinuxApacheLogProducer.class.getName(), cliOptions, true); return; } // ?? String confPath = commandLine.getOptionValue("c"); Map<String, Object> configMap = null; try { configMap = YamlReadUtil.readYaml(confPath); } catch (IOException ex) { // ??????? ex.printStackTrace(); return; } startTailLog(configMap); } /** * ????Log?Tail? * * @param configMap ?Map */ protected void startTailLog(Map<String, Object> configMap) { String tailCommand = configMap.get("tail.command").toString(); String tailPath = configMap.get("tail.path").toString(); String tailCommandStr = tailCommand + " " + tailPath; String kafkaTopic = configMap.get("kafka.topic").toString(); String apacheLogFormat = configMap.get("apachelog.format").toString(); String jsonDateFormat = configMap.get("jsondate.format").toString(); String hostname = "defaultHost"; try { hostname = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException ex) { logger.warn("HostName resolve failed. Use default. : default=" + hostname, ex); } ProducerConfig producerConfig = ProducerConfigConverter.convertToProducerConfig(configMap); logger.info("Producer starting. Command=" + tailCommandStr); ExecutorService executorService = Executors.newSingleThreadExecutor(); LinuxLogTailExecutor executor = new LinuxLogTailExecutor(tailCommandStr, kafkaTopic, apacheLogFormat, jsonDateFormat, hostname); executor.initialize(producerConfig); executorService.execute(executor); logger.info("Producer started"); } /** * ??? * * @return ?? */ public static Options createOptions() { Options cliOptions = new Options(); // OptionBuilder.hasArg(true); OptionBuilder.withArgName("LinuxApacheLogProducer Conf Path"); OptionBuilder.withDescription("LinuxApacheLogProducer Conf Path"); OptionBuilder.isRequired(true); Option confPathOption = OptionBuilder.create("c"); // OptionBuilder.withDescription("show help"); Option helpOption = OptionBuilder.create("h"); cliOptions.addOption(confPathOption); cliOptions.addOption(helpOption); return cliOptions; } }