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  package org.apache.logging.log4j.flume.appender;
18  
19  import com.google.common.base.Preconditions;
20  import org.apache.flume.lifecycle.LifecycleAware;
21  import org.apache.flume.lifecycle.LifecycleState;
22  import org.apache.flume.lifecycle.LifecycleSupervisor;
23  import org.apache.flume.node.NodeConfiguration;
24  import org.apache.flume.node.NodeManager;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   *
30   */
31  public class FlumeNode implements LifecycleAware {
32  
33      private static final Logger logger = LoggerFactory.getLogger(FlumeNode.class);
34  
35      private LifecycleState lifecycleState;
36      private final NodeManager nodeManager;
37      private final LifecycleSupervisor supervisor;
38      private final NodeConfiguration conf;
39  
40      public FlumeNode(NodeManager manager, NodeConfiguration conf) {
41          this.nodeManager = manager;
42          this.conf =conf;
43          supervisor = new LifecycleSupervisor();
44      }
45  
46      public void start() {
47  
48          Preconditions.checkState(nodeManager != null,
49              "Node manager can not be null");
50  
51          supervisor.start();
52  
53          logger.info("Flume node starting");
54  
55          supervisor.supervise(nodeManager,
56              new LifecycleSupervisor.SupervisorPolicy.AlwaysRestartPolicy(), LifecycleState.START);
57  
58          lifecycleState = LifecycleState.START;
59      }
60  
61      public void stop() {
62  
63          logger.info("Flume node stopping");
64  
65          supervisor.stop();
66  
67          lifecycleState = LifecycleState.STOP;
68      }
69  
70      public NodeManager getNodeManager() {
71          return nodeManager;
72      }
73  
74      public NodeConfiguration getConfiguration() {
75          return conf;
76      }
77  
78      public LifecycleState getLifecycleState() {
79          return lifecycleState;
80      }
81  
82  }