View Javadoc

1   /*
2    * $Id: ScheduledDispatchJob.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
11  package org.mule.transport.quartz.jobs;
12  
13  import org.mule.api.MuleException;
14  import org.mule.module.client.MuleClient;
15  import org.mule.transport.NullPayload;
16  import org.mule.transport.quartz.QuartzConnector;
17  import org.mule.transport.quartz.i18n.QuartzMessages;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.quartz.Job;
24  import org.quartz.JobDataMap;
25  import org.quartz.JobExecutionContext;
26  import org.quartz.JobExecutionException;
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              MuleClient client = new MuleClient(config.getMuleContext());
59  
60              String endpointRef = config.getEndpointRef();
61              if (jobDataMap.containsKey("endpointRef")) 
62              {
63                  endpointRef = (String) jobDataMap.get("endpointRef");
64              }
65  
66              logger.debug("Dispatching payload on: " + config.getEndpointRef());
67  
68              client.dispatch(endpointRef, payload, jobDataMap);
69          }
70          catch (MuleException e)
71          {
72              throw new JobExecutionException(e);
73          }
74      }
75  }