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