This project has retired. For details please refer to its
Attic page.
JPluginAgent xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }