Data privacy notice

 

When this content is loaded, usage information is transmitted to Vimeo and may be processed there.

 

             

JMX plugin

Modified on Mon, 16 Jan, 2023 at 10:58 PM


IMonitorPlugin


Use cases

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.


Method signatures

/**
  * This methods gets called every time the monitor queries the state of the monitoring plugin. To indicate that there
  * is no error, this method must return the value of the constant 'IMonitorPlugin.CHECK_SUCCESS'.
  * To indicate an error, the following options exist:
  * - This method throws a Java exception.
  * - This method returns an error message that will be shown in the monitoring MBean. You can either use
  *   a custom error message or use the value of the constant 'IMonitorPlugin.CHECK_ERROR'. However, make sure you do
  *   not accidentally return the value of the constant 'IMonitorPlugin.CHECK_SUCCESS'.
  */
public String check()

/**
  * Can be used for custom initialization. Do not use the constructor.
  * Initialization happens once, the 'check' method is then called multiple times as necessary.
  * The default implementation does nothing.
  */
public void init()

/**
  * Can be used for custom actions when the plugin is destroyed. The default implementation does nothing.
  */
public void shutdown()


Examples

Example 1: The following code shows how to check the availability of some third-party web service, here http://example.com.

import java.net.HttpURLConnection;
import java.net.URL;

import de.xima.fc.plugin.interfaces.IMonitorPlugin;

public class MyMonitor implements IMonitorPlugin {

  @Override
  public String check() {
    try {
      URL url = new URL("http://example.com");
      HttpURLConnection urlConnection = null;
      // Open a connection to http://example.com
      urlConnection = (HttpURLConnection)url.openConnection();
      urlConnection.setConnectTimeout(1000);
      urlConnection.setReadTimeout(1000);
      urlConnection.connect();
      // Return 'success' only when the web service returns a 200.
      if (HttpURLConnection.HTTP_OK != urlConnection.getResponseCode()) { 
        return urlConnection.getResponseMessage();
      }
      return IMonitorPlugin.CHECK_SUCCESS;
    }
    catch (Exception e) {
      return IMonitorPlugin.CHECK_ERROR;
    }
  }
}

Example 2: The following code shows how to query the error status of a status processing plugin.

import org.apache.commons.lang3.StringUtils;

import de.xima.fc.plugin.interfaces.IMonitorPlugin;
import de.xima.foobar.plugin.MyTestPlugin;

public class MyMonitor implements IMonitorPlugin {

  @Override
  public String check() throws Exception {
    String state = MyTestPlugin.getMonitoringState();
    if (StringUtils.isBlank(state) || StringUtils.equalsIgnoreCase(IMonitorPlugin.CHECK_SUCCESS, state)) {
      return IMonitorPlugin.CHECK_SUCCESS;
    } else {
      return state;
    }
  }
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import de.xima.fc.bl.fdv.processing.result.ProcessingResultMultiValue;
import de.xima.fc.interfaces.plugin.param.workflow.IPluginProcessingParams;
import de.xima.fc.interfaces.plugin.retval.workflow.IPluginProcessingRetVal;
import de.xima.fc.plugin.abstracts.AFCPlugin;
import de.xima.fc.plugin.config.IPluginConfigParam;
import de.xima.fc.plugin.config.IPluginConfigParam.EPluginParamBehavior;
import de.xima.fc.plugin.config.IPluginConfigParamList;
import de.xima.fc.plugin.exception.FCPluginException;
import de.xima.fc.plugin.interfaces.IMonitorPlugin;
import de.xima.fc.plugin.interfaces.workflow.IPluginProcessing;
import de.xima.fc.plugin.models.config.PluginConfigGroupItem;
import de.xima.fc.plugin.models.config.PluginConfigParam;


@SuppressWarnings("serial")
public class MyTestPlugin extends AFCPlugin implements IPluginProcessing, IPluginConfigParamList {

  private static final String PARAM_IN_1 = "InputParameter_1";
  private static final String PARAM_OUT = "OutputParameter";

  private static String monitoringState;

  public String getName() {
    return "MyPlugin";
  }

  public void initPlugin() throws FCPluginException { }

  public static String getMonitoringState() {
    return monitoringState;
  }

  @Override
  public IPluginProcessingRetVal execute(IPluginProcessingParams processingParams) throws FCPluginException {
    // read input values
    Map<String, String> pluginParams = processingParams.getParams();
    String param1 = pluginParams.get(PARAM_IN_1);

    // check and write state for monitoring
    if (StringUtils.isBlank(param1)) {
      monitoringState = "Param1 is empty!";
    } else {
      monitoringState = IMonitorPlugin.CHECK_SUCCESS;
    }
    // plugin processing
    // .... do something

    // write return values
    String resultValue = "AnyResultValue";
    return new ProcessingResultMultiValue(createResult(resultValue), true);
  }

  @Override
  public String getDescription() {
    return "The plugin does something important...";
  }

  @Override
  public Map<String, IPluginConfigParam> getConfigParameter() {
    Map<String, IPluginConfigParam> map = new LinkedHashMap<>();
    map.put("input", new PluginConfigGroupItem("base data:"));
    map.put(PARAM_IN_1, new PluginConfigParam(PARAM_IN_1, "This is a mandatory parameter!", true, EPluginParamBehavior.IN));
    map.put("output", new PluginConfigGroupItem("output data:"));
    map.put(PARAM_OUT, new PluginConfigParam(PARAM_OUT, null, false, EPluginParamBehavior.OUT));
    return map;
  }

  private List<Map<String, String>> createResult(String result) {
    List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
    Map<String, String> resultMap = new HashMap<String, String>();
    resultMap.put(PARAM_OUT, result);
    resultList.add(resultMap);
    return resultList;
  }
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article