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.ExceptionUtil;
28  
29  public class JPluginAgent {
30    private static Log log = LogFactory.getLog(JPluginAgent.class);
31  
32    private static class MetricsTimerTask extends TimerTask {
33      @SuppressWarnings( { "unchecked" })
34      private JPlugin plugin;
35  
36      @SuppressWarnings("unchecked")
37      public MetricsTimerTask(JPlugin plugin) {
38        this.plugin = plugin;
39      }
40  
41      @SuppressWarnings("unchecked")
42      @Override
43      public void run() {
44        try {
45          ChukwaMetricsList metrics = plugin.getMetrics();
46          String xml = metrics.toXml();
47          log.info(xml);
48        } catch (Throwable e) {
49          log.error(e.getMessage(), e);
50        }
51      }
52    }
53  
54    private static class StatusTimerTask extends TimerTask {
55      @SuppressWarnings( { "unchecked" })
56      private JPlugin plugin;
57  
58      @SuppressWarnings("unchecked")
59      public StatusTimerTask(JPlugin plugin) {
60        this.plugin = plugin;
61      }
62  
63      @SuppressWarnings("unchecked")
64      @Override
65      public void run() {
66        try {
67          ChukwaMetricsList metrics = plugin.getStatus();
68          String xml = metrics.toXml();
69          log.info(xml);
70        } catch (Throwable e) {
71          log.error(e.getMessage(), e);
72        }
73      }
74    }
75  
76    @SuppressWarnings("unchecked")
77    public static void main(String[] args) {
78      if (args.length < 1) {
79        System.out
80            .println("Usage: java -DPERIOD=nn JavaPluginAgent <class name> [parameters]");
81        return;
82      }
83  
84      int period = -1;
85      try {
86        if (System.getProperty("PERIOD") != null) {
87          period = Integer.parseInt(System.getProperty("PERIOD"));
88        }
89      } catch (NumberFormatException ex) {
90        ex.printStackTrace();
91        System.out.println("PERIOD should be numeric format of seconds.");
92        return;
93      }
94  
95      JPlugin plugin = null;
96      try {
97        plugin = (JPlugin) Class.forName(args[0]).newInstance();
98        plugin.init(args);
99      } catch (Throwable e) {
100       e.printStackTrace();
101       return;
102     }
103 
104     try {
105     } catch (Exception e) {
106       e.printStackTrace();
107     }
108 
109     Calendar cal = Calendar.getInstance();
110     long now = cal.getTime().getTime();
111     cal.set(Calendar.SECOND, 3);
112     cal.set(Calendar.MILLISECOND, 0);
113     cal.add(Calendar.MINUTE, 1);
114     long until = cal.getTime().getTime();
115     try {
116       if (period == -1) {
117         new MetricsTimerTask(plugin).run();
118       } else {
119         Thread.sleep(until - now);
120         Timer timer = new Timer();
121         timer.scheduleAtFixedRate(new MetricsTimerTask(plugin), 0,
122             period * 1000);
123       }
124     } catch (Exception ex) {
125       log.debug(ExceptionUtil.getStackTrace(ex));
126     }
127   }
128 }