PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/src/com/googlecode/jmxtrans/jobs/ServerJob.java

http://jmxtrans.googlecode.com/
Java | 53 lines | 39 code | 8 blank | 6 comment | 2 complexity | 0a734d1367d54a5b08a8470d134cb6cb MD5 | raw file
 1package com.googlecode.jmxtrans.jobs;
 2
 3import javax.management.remote.JMXConnector;
 4
 5import org.apache.commons.pool.impl.GenericKeyedObjectPool;
 6import org.quartz.Job;
 7import org.quartz.JobDataMap;
 8import org.quartz.JobExecutionContext;
 9import org.quartz.JobExecutionException;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13import com.googlecode.jmxtrans.model.Server;
14import com.googlecode.jmxtrans.util.JmxUtils;
15
16/**
17 * This is a quartz job that is responsible for executing a Server object
18 * on a cron schedule that is defined within the Server object.
19 *
20 * @author jon
21 */
22public class ServerJob implements Job {
23	private static final Logger log = LoggerFactory.getLogger(ServerJob.class);
24
25	public void execute(JobExecutionContext context) throws JobExecutionException {
26		JobDataMap map = context.getMergedJobDataMap();
27		Server server = (Server) map.get(Server.class.getName());
28		GenericKeyedObjectPool pool = (GenericKeyedObjectPool) map.get(Server.JMX_CONNECTION_FACTORY_POOL);
29
30		if (log.isDebugEnabled()) {
31			log.debug("+++++ Started server job: " + server);
32		}
33
34		JMXConnector conn = null;
35		try {
36	        conn = (JMXConnector)pool.borrowObject(server);
37			JmxUtils.processServer(server, conn);
38		} catch (Exception e) {
39			log.error("Error", e);
40			throw new JobExecutionException(e);
41		} finally {
42		    try {
43		        pool.returnObject(server, conn);
44		    } catch (Exception ex) {
45		        log.error("Error returning object to pool for server: " + server);
46		    }
47		}
48
49		if (log.isDebugEnabled()) {
50			log.debug("+++++ Finished server job: " + server);
51		}
52	}
53}