View Javadoc

1   /*
2    * $Id: AbstractJob.java 22399 2011-07-13 07:06:27Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.quartz.jobs;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.config.MuleProperties;
14  import org.mule.transport.quartz.QuartzConnector;
15  
16  import org.quartz.Job;
17  import org.quartz.JobExecutionContext;
18  import org.quartz.JobExecutionException;
19  import org.quartz.SchedulerContext;
20  import org.quartz.SchedulerException;
21  
22  /**
23   * A superclass for Quartz jobs.
24   */
25  public abstract class AbstractJob implements Job
26  {
27      protected MuleContext muleContext;
28  
29      @Override
30      public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
31      {
32          muleContext = getMuleContext(jobExecutionContext);
33          if (muleContext.isPrimaryPollingInstance() ||
34              jobExecutionContext.getJobDetail().getJobDataMap().get(QuartzConnector.PROPERTY_JOB_DYNAMIC) == Boolean.TRUE)
35          {
36              doExecute(jobExecutionContext);
37          }
38      }
39  
40      /**
41       * Execute the job.
42       */
43      protected abstract void doExecute(JobExecutionContext jobExecutionContext) throws JobExecutionException;
44  
45      protected MuleContext getMuleContext(JobExecutionContext jobExecutionContext) throws JobExecutionException
46      {
47          try
48          {
49              SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
50              return (MuleContext) schedulerContext.get(MuleProperties.MULE_CONTEXT_PROPERTY);
51          }
52          catch (SchedulerException e)
53          {
54              throw new JobExecutionException("Failed to retrieve MuleContext from the Scheduler Context: " + e.getMessage(), e);
55          }
56      }
57  }