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 package org.apache.logging.log4j.flume.appender; 018 019 import com.google.common.base.Preconditions; 020 import org.apache.flume.lifecycle.LifecycleAware; 021 import org.apache.flume.lifecycle.LifecycleState; 022 import org.apache.flume.lifecycle.LifecycleSupervisor; 023 import org.apache.flume.node.NodeConfiguration; 024 import org.apache.flume.node.NodeManager; 025 import org.slf4j.Logger; 026 import org.slf4j.LoggerFactory; 027 028 /** 029 * 030 */ 031 public class FlumeNode implements LifecycleAware { 032 033 private static final Logger logger = LoggerFactory.getLogger(FlumeNode.class); 034 035 private LifecycleState lifecycleState; 036 private final NodeManager nodeManager; 037 private final LifecycleSupervisor supervisor; 038 private final NodeConfiguration conf; 039 040 public FlumeNode(NodeManager manager, NodeConfiguration conf) { 041 this.nodeManager = manager; 042 this.conf =conf; 043 supervisor = new LifecycleSupervisor(); 044 } 045 046 public void start() { 047 048 Preconditions.checkState(nodeManager != null, 049 "Node manager can not be null"); 050 051 supervisor.start(); 052 053 logger.info("Flume node starting"); 054 055 supervisor.supervise(nodeManager, 056 new LifecycleSupervisor.SupervisorPolicy.AlwaysRestartPolicy(), LifecycleState.START); 057 058 lifecycleState = LifecycleState.START; 059 } 060 061 public void stop() { 062 063 logger.info("Flume node stopping"); 064 065 supervisor.stop(); 066 067 lifecycleState = LifecycleState.STOP; 068 } 069 070 public NodeManager getNodeManager() { 071 return nodeManager; 072 } 073 074 public NodeConfiguration getConfiguration() { 075 return conf; 076 } 077 078 public LifecycleState getLifecycleState() { 079 return lifecycleState; 080 } 081 082 }