Wiki source code of JMX plugin
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{content/}} | ||
2 | |||
3 | == IMonitorPlugin == | ||
4 | |||
5 | === Use cases === | ||
6 | |||
7 | This type of plugin allows you to perform monitoring via the JMX interface. It can be used to monitor custom plugins or to check the availability of third-party web services. | ||
8 | |||
9 | === Method signatures === | ||
10 | |||
11 | {{code language="java"}} | ||
12 | /** | ||
13 | * This methods gets called every time the monitor queries the state of the monitoring plugin. To indicate that there | ||
14 | * is no error, this method must return the value of the constant 'IMonitorPlugin.CHECK_SUCCESS'. | ||
15 | * To indicate an error, the following options exist: | ||
16 | * - This method throws a Java exception. | ||
17 | * - This method returns an error message that will be shown in the monitoring MBean. You can either use | ||
18 | * a custom error message or use the value of the constant 'IMonitorPlugin.CHECK_ERROR'. However, make sure you do | ||
19 | * not accidentally return the value of the constant 'IMonitorPlugin.CHECK_SUCCESS'. | ||
20 | */ | ||
21 | public String check() | ||
22 | |||
23 | /** | ||
24 | * Can be used for custom initialization. Do not use the constructor. | ||
25 | * Initialization happens once, the 'check' method is then called multiple times as necessary. | ||
26 | * The default implementation does nothing. | ||
27 | */ | ||
28 | public void init() | ||
29 | |||
30 | /** | ||
31 | * Can be used for custom actions when the plugin is destroyed. The default implementation does nothing. | ||
32 | */ | ||
33 | public void shutdown() | ||
34 | {{/code}} | ||
35 | |||
36 | === Examples === | ||
37 | |||
38 | Example 1: The following code shows how to check the availability of some third-party web service, here //http:~/~/example.com//. | ||
39 | |||
40 | {{code language="java"}} | ||
41 | |||
42 | import java.net.HttpURLConnection; | ||
43 | import java.net.URL; | ||
44 | |||
45 | import de.xima.fc.plugin.interfaces.IMonitorPlugin; | ||
46 | |||
47 | public class MyMonitor implements IMonitorPlugin { | ||
48 | |||
49 | @Override | ||
50 | public String check() { | ||
51 | try { | ||
52 | URL url = new URL("http://example.com"); | ||
53 | HttpURLConnection urlConnection = null; | ||
54 | // Open a connection to http://example.com | ||
55 | urlConnection = (HttpURLConnection)url.openConnection(); | ||
56 | urlConnection.setConnectTimeout(1000); | ||
57 | urlConnection.setReadTimeout(1000); | ||
58 | urlConnection.connect(); | ||
59 | // Return 'success' only when the web service returns a 200. | ||
60 | if (HttpURLConnection.HTTP_OK != urlConnection.getResponseCode()) { | ||
61 | return urlConnection.getResponseMessage(); | ||
62 | } | ||
63 | return IMonitorPlugin.CHECK_SUCCESS; | ||
64 | } | ||
65 | catch (Exception e) { | ||
66 | return IMonitorPlugin.CHECK_ERROR; | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | {{/code}} | ||
71 | |||
72 | Example 2: The following code shows how to query the error status of a status processing plugin. | ||
73 | |||
74 | {{code language="java"}} | ||
75 | import org.apache.commons.lang3.StringUtils; | ||
76 | |||
77 | import de.xima.fc.plugin.interfaces.IMonitorPlugin; | ||
78 | import de.xima.foobar.plugin.MyTestPlugin; | ||
79 | |||
80 | public class MyMonitor implements IMonitorPlugin { | ||
81 | |||
82 | @Override | ||
83 | public String check() throws Exception { | ||
84 | String state = MyTestPlugin.getMonitoringState(); | ||
85 | if (StringUtils.isBlank(state) || StringUtils.equalsIgnoreCase(IMonitorPlugin.CHECK_SUCCESS, state)) { | ||
86 | return IMonitorPlugin.CHECK_SUCCESS; | ||
87 | } else { | ||
88 | return state; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | {{/code}} | ||
93 | |||
94 | {{code language="java"}} | ||
95 | import java.util.ArrayList; | ||
96 | import java.util.HashMap; | ||
97 | import java.util.LinkedHashMap; | ||
98 | import java.util.List; | ||
99 | import java.util.Map; | ||
100 | |||
101 | import org.apache.commons.lang3.StringUtils; | ||
102 | |||
103 | import de.xima.fc.bl.fdv.processing.result.ProcessingResultMultiValue; | ||
104 | import de.xima.fc.interfaces.plugin.param.workflow.IPluginProcessingParams; | ||
105 | import de.xima.fc.interfaces.plugin.retval.workflow.IPluginProcessingRetVal; | ||
106 | import de.xima.fc.plugin.abstracts.AFCPlugin; | ||
107 | import de.xima.fc.plugin.config.IPluginConfigParam; | ||
108 | import de.xima.fc.plugin.config.IPluginConfigParam.EPluginParamBehavior; | ||
109 | import de.xima.fc.plugin.config.IPluginConfigParamList; | ||
110 | import de.xima.fc.plugin.exception.FCPluginException; | ||
111 | import de.xima.fc.plugin.interfaces.IMonitorPlugin; | ||
112 | import de.xima.fc.plugin.interfaces.workflow.IPluginProcessing; | ||
113 | import de.xima.fc.plugin.models.config.PluginConfigGroupItem; | ||
114 | import de.xima.fc.plugin.models.config.PluginConfigParam; | ||
115 | |||
116 | |||
117 | @SuppressWarnings("serial") | ||
118 | public class MyTestPlugin extends AFCPlugin implements IPluginProcessing, IPluginConfigParamList { | ||
119 | |||
120 | private static final String PARAM_IN_1 = "InputParameter_1"; | ||
121 | private static final String PARAM_OUT = "OutputParameter"; | ||
122 | |||
123 | private static String monitoringState; | ||
124 | |||
125 | public String getName() { | ||
126 | return "MyPlugin"; | ||
127 | } | ||
128 | |||
129 | public void initPlugin() throws FCPluginException { } | ||
130 | |||
131 | public static String getMonitoringState() { | ||
132 | return monitoringState; | ||
133 | } | ||
134 | |||
135 | @Override | ||
136 | public IPluginProcessingRetVal execute(IPluginProcessingParams processingParams) throws FCPluginException { | ||
137 | // read input values | ||
138 | Map<String, String> pluginParams = processingParams.getParams(); | ||
139 | String param1 = pluginParams.get(PARAM_IN_1); | ||
140 | |||
141 | // check and write state for monitoring | ||
142 | if (StringUtils.isBlank(param1)) { | ||
143 | monitoringState = "Param1 is empty!"; | ||
144 | } else { | ||
145 | monitoringState = IMonitorPlugin.CHECK_SUCCESS; | ||
146 | } | ||
147 | // plugin processing | ||
148 | // .... do something | ||
149 | |||
150 | // write return values | ||
151 | String resultValue = "AnyResultValue"; | ||
152 | return new ProcessingResultMultiValue(createResult(resultValue), true); | ||
153 | } | ||
154 | |||
155 | @Override | ||
156 | public String getDescription() { | ||
157 | return "The plugin does something important..."; | ||
158 | } | ||
159 | |||
160 | @Override | ||
161 | public Map<String, IPluginConfigParam> getConfigParameter() { | ||
162 | Map<String, IPluginConfigParam> map = new LinkedHashMap<>(); | ||
163 | map.put("input", new PluginConfigGroupItem("base data:")); | ||
164 | map.put(PARAM_IN_1, new PluginConfigParam(PARAM_IN_1, "This is a mandatory parameter!", true, EPluginParamBehavior.IN)); | ||
165 | map.put("output", new PluginConfigGroupItem("output data:")); | ||
166 | map.put(PARAM_OUT, new PluginConfigParam(PARAM_OUT, null, false, EPluginParamBehavior.OUT)); | ||
167 | return map; | ||
168 | } | ||
169 | |||
170 | private List<Map<String, String>> createResult(String result) { | ||
171 | List<Map<String, String>> resultList = new ArrayList<Map<String, String>>(); | ||
172 | Map<String, String> resultMap = new HashMap<String, String>(); | ||
173 | resultMap.put(PARAM_OUT, result); | ||
174 | resultList.add(resultMap); | ||
175 | return resultList; | ||
176 | } | ||
177 | } | ||
178 | {{/code}} |