org.quartz.jobs.ee.jmx.JMXInvokerJob Maven / Gradle / Ivy
Show all versions of quartz-jobs Show documentation
/*
* Copyright 2001-2009 Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package org.quartz.jobs.ee.jmx;
import java.util.LinkedList;
import java.util.StringTokenizer;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Generic JMX invoker Job. It supports any number or type of parameters
* to the JMX bean.
*
* The required parameters are as follows (case doesn't matter):
*
* - JMX_OBJECTNAME
*
- This is the fully qualifed name of the object (ie in JBoss to lookup
* the log4j jmx bean you would specify "jboss.system:type=Log4jService,service=Logging"
*
- JMX_METHOD
*
- This is the method to invoke on the specified JMX Bean. (ie in JBoss to
* change the log level you would specify "setLoggerLevel"
*
- JMX_PARAMDEFS
*
- This is a definition of the parameters to be passed to the specified method
* and their corresponding java types. Each parameter definition is comma seperated
* and has the following parts:
:. Type is the java type for the parameter.
* The following types are supported:
* i - is for int
* l - is for long
* f - is for float
* d - is for double
* s - is for String
* b - is for boolean
* For ilfdb use lower for native type and upper for object wrapper. The name portion
* of the definition is the name of the parameter holding the string value. (ie
* s:fname,s:lname would require 2 parameters of the name fname and lname and
* would be passed in that order to the method.
*
* @author James Nelson ([email protected]) -- Provident Solutions LLC
*
*/
public class JMXInvokerJob implements Job {
private final Logger log = LoggerFactory.getLogger(getClass());
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
Object[] params=null;
String[] types=null;
String objName = null;
String objMethod = null;
JobDataMap jobDataMap = context.getMergedJobDataMap();
String[] keys = jobDataMap.getKeys();
for (int i = 0; i < keys.length; i++) {
String value = jobDataMap.getString(keys[i]);
if ("JMX_OBJECTNAME".equalsIgnoreCase(keys[i])) {
objName = value;
} else if ("JMX_METHOD".equalsIgnoreCase(keys[i])) {
objMethod = value;
} else if("JMX_PARAMDEFS".equalsIgnoreCase(keys[i])) {
String[] paramdefs=split(value, ",");
params=new Object[paramdefs.length];
types=new String[paramdefs.length];
for(int k=0;k l = new LinkedList();
StringTokenizer strTok = new StringTokenizer(str, splitStr);
while(strTok.hasMoreTokens()) {
String tok = strTok.nextToken();
l.add(tok);
}
return (String[])l.toArray(new String[l.size()]);
}
private Object invoke(String objectName, String method, Object[] params, String[] types) throws Exception {
MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName mbean = new ObjectName(objectName);
if (server == null) {
throw new Exception("Can't find mbean server");
}
getLog().info("invoking " + method);
return server.invoke(mbean, method, params, types);
}
protected Logger getLog() {
return log;
}
}