1
2
3
4
5
6
7 package org.mule.transport.quartz.jobs;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.config.MuleProperties;
11 import org.mule.transport.quartz.QuartzConnector;
12 import org.mule.transport.quartz.i18n.QuartzMessages;
13
14 import org.quartz.Job;
15 import org.quartz.JobDataMap;
16 import org.quartz.JobExecutionContext;
17 import org.quartz.JobExecutionException;
18 import org.quartz.SchedulerContext;
19 import org.quartz.SchedulerException;
20
21
22
23
24
25
26
27
28
29
30 public class CustomJob implements Job
31 {
32 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
33 {
34 MuleContext muleContext = lookupMuleContext(jobExecutionContext);
35
36 JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
37 Object tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_OBJECT);
38 if (tempJob == null)
39 {
40 tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_REF);
41 if (tempJob == null)
42 {
43 throw new JobExecutionException(QuartzMessages.invalidPayloadType().getMessage());
44 }
45 else
46 {
47 tempJob = muleContext.getRegistry().lookupObject((String) tempJob);
48 if (tempJob == null)
49 {
50 throw new JobExecutionException("Job not found: " + tempJob);
51 }
52 if (!(tempJob instanceof Job))
53 {
54 throw new JobExecutionException(QuartzMessages.invalidJobObject().getMessage());
55 }
56 }
57 }
58 else if (!(tempJob instanceof Job))
59 {
60 throw new JobExecutionException(QuartzMessages.invalidJobObject().toString());
61 }
62 ((Job)tempJob).execute(jobExecutionContext);
63 }
64
65 private MuleContext lookupMuleContext(JobExecutionContext jobExecutionContext) throws JobExecutionException
66 {
67 try
68 {
69 SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
70 return (MuleContext) schedulerContext.get(MuleProperties.MULE_CONTEXT_PROPERTY);
71 }
72 catch (SchedulerException e)
73 {
74 throw new JobExecutionException("Failed to retrieve Mulecontext from the Scheduler Context: " + e.getMessage(), e);
75 }
76 }
77 }