This project has retired. For details please refer to its Attic page.
JPluginAgent xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.inputtools.jplugin;
20  
21  import java.util.Calendar;
22  import java.util.Timer;
23  import java.util.TimerTask;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.chukwa.util.DaemonWatcher;
28  import org.apache.hadoop.chukwa.util.ExceptionUtil;
29  
30  public class JPluginAgent {
31    private static Log log = LogFactory.getLog(JPluginAgent.class);
32  
33    private static class MetricsTimerTask extends TimerTask {
34      @SuppressWarnings( { "unchecked" })
35      private JPlugin plugin;
36  
37      @SuppressWarnings("unchecked")
38      public MetricsTimerTask(JPlugin plugin) {
39        this.plugin = plugin;
40      }
41  
42      @SuppressWarnings("unchecked")
43      @Override
44      public void run() {
45        try {
46          ChukwaMetricsList metrics = plugin.getMetrics();
47          String xml = metrics.toXml();
48          log.info(xml);
49        } catch (Throwable e) {
50          log.error(e.getMessage(), e);
51        }
52      }
53    }
54  
55    private static class StatusTimerTask extends TimerTask {
56      @SuppressWarnings( { "unchecked" })
57      private JPlugin plugin;
58  
59      @SuppressWarnings("unchecked")
60      public StatusTimerTask(JPlugin plugin) {
61        this.plugin = plugin;
62      }
63  
64      @SuppressWarnings("unchecked")
65      @Override
66      public void run() {
67        try {
68          ChukwaMetricsList metrics = plugin.getStatus();
69          String xml = metrics.toXml();
70          log.info(xml);
71        } catch (Throwable e) {
72          log.error(e.getMessage(), e);
73        }
74      }
75    }
76  
77    @SuppressWarnings("unchecked")
78    public static void main(String[] args) {
79      if (args.length < 1) {
80        System.out
81            .println("Usage: java -DPERIOD=nn JavaPluginAgent <class name> [parameters]");
82        System.exit(0);
83      }
84  
85      int period = -1;
86      try {
87        if (System.getProperty("PERIOD") != null) {
88          period = Integer.parseInt(System.getProperty("PERIOD"));
89        }
90      } catch (NumberFormatException ex) {
91        ex.printStackTrace();
92        System.out.println("PERIOD should be numeric format of seconds.");
93        System.exit(0);
94      }
95  
96      JPlugin plugin = null;
97      try {
98        plugin = (JPlugin) Class.forName(args[0]).newInstance();
99        plugin.init(args);
100     } catch (Throwable e) {
101       e.printStackTrace();
102       System.exit(-1);
103     }
104 
105     try {
106       DaemonWatcher.createInstance(plugin.getRecordType() + "-data-loader");
107     } catch (Exception e) {
108       e.printStackTrace();
109     }
110 
111     Calendar cal = Calendar.getInstance();
112     long now = cal.getTime().getTime();
113     cal.set(Calendar.SECOND, 3);
114     cal.set(Calendar.MILLISECOND, 0);
115     cal.add(Calendar.MINUTE, 1);
116     long until = cal.getTime().getTime();
117     try {
118       if (period == -1) {
119         new MetricsTimerTask(plugin).run();
120       } else {
121         Thread.sleep(until - now);
122         Timer timer = new Timer();
123         timer.scheduleAtFixedRate(new MetricsTimerTask(plugin), 0,
124             period * 1000);
125       }
126     } catch (Exception ex) {
127       log.debug(ExceptionUtil.getStackTrace(ex));
128     }
129   }
130 }