1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.qcron;
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
23
24 import javax.management.ObjectName;
25 import javax.management.MBeanServer;
26 import javax.management.MBeanServerFactory;
27 import javax.management.InstanceNotFoundException;
28 import javax.management.MBeanException;
29 import javax.management.ReflectionException;
30
31 import org.quartz.Job;
32 import org.quartz.JobDetail;
33 import org.quartz.JobDataMap;
34 import org.quartz.JobExecutionContext;
35 import org.quartz.JobExecutionException;
36
37 import org.apache.log4j.Logger;
38 /***
39 * A Quartz job that will delegate to an MBean.
40 * <p>Delegation is enforced by saving a the MBeanJob in the JobDataMap of the JJobDetail this is connected to. Since MBeanJobb is a serializable class this wil work even if JobDataMap is saved to peristent store. To make this work one have to create an MBeanJob and set it in the JobDataMap of the JobDetail that should have this job as its executor, eg:</p>
41 <pre>
42 jobDetail = new JobDetail(name,
43 group,
44 MBeanJob.class
45 );
46 MBeanJob job = new MBeanJob( jobDetail.getJobDataMap(),
47 jobObjectName);
48 </pre>
49 <p>This will the work automagically.</p>
50 *
51 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
52 * @version $Revision: 1.1.1.1 $
53 */
54
55 public class MBeanJob implements Job, java.io.Serializable{
56 private transient Logger log;
57 private static final String[] JOB_SIG = new String[]{JobExecutionContext.class.getName()};
58 private static final String JOB_METH = "execute";
59 private static final String MBEAN_KEY = "org.backsource.qcron.MBeanJob";
60 private static final boolean DONT_REFIRE = false;
61 protected ObjectName mbean;
62 protected String methodName;
63 /***
64 * Empty constructor when created through Quartz.
65 */
66 public MBeanJob (){
67 log = Logger.getLogger(MBeanJob.class);
68 log.debug("MBean jobb constructed");
69
70 }
71 /***
72 * Object name of an MBean implementing the job interface to delegate calls to job to.
73 */
74 public MBeanJob (JobDataMap map,ObjectName mbean){
75 this(map,mbean,null);
76 }
77 /***
78 * Delegate to MBean, but invoke method methodName, wich must be a non param method.
79 */
80 public MBeanJob (JobDataMap map,ObjectName mbean, String methodName){
81 this.mbean = mbean;
82 this.methodName = methodName;
83 map.put(MBEAN_KEY,this);
84 log = Logger.getLogger(MBeanJob.class);
85 }
86
87 /***
88 * Set the job in the map.
89 */
90 public static void setMBeanJob(JobDataMap map,MBeanJob job) {
91 map.put(MBEAN_KEY,job);
92 }
93
94 /***
95 * Get the job from the map.
96 */
97 public static MBeanJob getMBeanJob(JobDataMap map) {
98 return (MBeanJob)map.get(MBEAN_KEY);
99 }
100
101 public ObjectName getObjectName() {
102 return mbean;
103 }
104
105 /***
106 * impl of job.
107 *
108 * <p>When Quartz triggers a job a new instance of this class will be created and this method gets called.
109 */
110 public void execute(JobExecutionContext context)
111 throws JobExecutionException {
112 log.debug("MBean jobb called:" +context);
113 try {
114
115 Object o = context.getJobDetail().getJobDataMap().get(MBEAN_KEY);
116 if ( o != null && o instanceof MBeanJob) {
117 MBeanJob job = (MBeanJob)o;
118 MBeanServer server = (MBeanServer)MBeanServerFactory.findMBeanServer(null).iterator().next();
119 String method = JOB_METH;
120 String[] sig = JOB_SIG;
121 Object[] params = new Object[]{context};
122 if ( job.methodName != null) {
123
124 method = job.methodName;
125 sig = new String[]{};
126 params = new Object[]{};
127 }
128
129 server.invoke(job.mbean,method,params,sig);
130 } else {
131 log.error("Could not find MBean object to use");
132 throw new JobExecutionException("Could not find MBean object to use",null,DONT_REFIRE);
133 }
134
135 } catch (NoSuchElementException e) {
136 log.error(e);
137 throw new JobExecutionException("Could not find MBean server",e,DONT_REFIRE);
138 } catch (InstanceNotFoundException e) {
139 log.error(e);
140 throw new JobExecutionException("Could not find MBean "+mbean,e,DONT_REFIRE);
141 }catch (MBeanException e) {
142 log.error(e);
143 Exception se = e.getTargetException();
144 throw new JobExecutionException("MBean operation throw exception: " +e,se,DONT_REFIRE);
145 } catch (ReflectionException e) {
146 log.error(e);
147 Exception se = e.getTargetException();
148 throw new JobExecutionException("MBean operation probably not invoked correctly: " +e,se,DONT_REFIRE);
149 }
150
151
152
153
154
155 }
156
157 }