View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.quartz.jobs;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.module.client.MuleClient;
13  import org.mule.transport.NullPayload;
14  import org.mule.transport.quartz.QuartzConnector;
15  import org.mule.transport.quartz.i18n.QuartzMessages;
16  
17  import java.io.Serializable;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.quartz.Job;
22  import org.quartz.JobDataMap;
23  import org.quartz.JobExecutionContext;
24  import org.quartz.JobExecutionException;
25  import org.quartz.SchedulerContext;
26  import org.quartz.SchedulerException;
27  
28  /**
29   * Will dispatch the current message to a Mule endpoint at a later time.
30   * This job can be used to fire time based events.
31   */
32  public class ScheduledDispatchJob implements Job, Serializable
33  {
34      /**
35       * The logger used for this class
36       */
37      protected transient Log logger = LogFactory.getLog(getClass());
38  
39      public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
40      {
41          JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
42          Object payload = jobDataMap.get(QuartzConnector.PROPERTY_PAYLOAD);
43  
44          if (payload == null)
45          {
46              payload = NullPayload.getInstance();
47          }
48  
49          ScheduledDispatchJobConfig config = (ScheduledDispatchJobConfig) jobDataMap.get(QuartzConnector.PROPERTY_JOB_CONFIG);
50          if (config == null)
51          {
52              throw new JobExecutionException(
53                  QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
54          }
55  
56          try
57          {
58              SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
59              MuleContext muleContext = (MuleContext) schedulerContext.get(MuleProperties.MULE_CONTEXT_PROPERTY);
60  
61              String endpointRef = config.getEndpointRef();
62              if (jobDataMap.containsKey("endpointRef"))
63              {
64                  endpointRef = (String) jobDataMap.get("endpointRef");
65              }
66  
67              logger.debug("Dispatching payload on: " + config.getEndpointRef());
68  
69              MuleClient client = new MuleClient(muleContext);
70              client.dispatch(endpointRef, payload, jobDataMap);
71          }
72          catch (MuleException e)
73          {
74              throw new JobExecutionException(e);
75          }
76          catch (SchedulerException e)
77          {
78              throw new JobExecutionException(e);
79          }
80      }
81  }