View Javadoc

1   /*
2    * $Id: ScheduledDispatchJob.java 22396 2011-07-12 21:26:04Z mike.schilling $
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.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.module.client.MuleClient;
17  import org.mule.transport.NullPayload;
18  import org.mule.transport.quartz.QuartzConnector;
19  import org.mule.transport.quartz.i18n.QuartzMessages;
20  
21  import java.io.Serializable;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.quartz.Job;
26  import org.quartz.JobDataMap;
27  import org.quartz.JobExecutionContext;
28  import org.quartz.JobExecutionException;
29  import org.quartz.SchedulerContext;
30  import org.quartz.SchedulerException;
31  
32  /**
33   * Will dispatch the current message to a Mule endpoint at a later time.
34   * This job can be used to fire time based events.
35   */
36  public class ScheduledDispatchJob extends AbstractJob implements Serializable
37  {
38      /**
39       * The logger used for this class
40       */
41      protected transient Log logger = LogFactory.getLog(getClass());
42  
43      protected void doExecute(JobExecutionContext jobExecutionContext) throws JobExecutionException
44      {
45          JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
46          Object payload = jobDataMap.get(QuartzConnector.PROPERTY_PAYLOAD);
47  
48          if (payload == null)
49          {
50              payload = NullPayload.getInstance();
51          }
52  
53          ScheduledDispatchJobConfig config = (ScheduledDispatchJobConfig) jobDataMap.get(QuartzConnector.PROPERTY_JOB_CONFIG);
54          if (config == null)
55          {
56              throw new JobExecutionException(
57                  QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
58          }
59  
60          try
61          {
62              String endpointRef = config.getEndpointRef();
63              if (jobDataMap.containsKey("endpointRef"))
64              {
65                  endpointRef = (String) jobDataMap.get("endpointRef");
66              }
67  
68              logger.debug("Dispatching payload on: " + config.getEndpointRef());
69  
70              MuleClient client = new MuleClient(muleContext);
71              client.dispatch(endpointRef, payload, jobDataMap);
72          }
73          catch (MuleException e)
74          {
75              throw new JobExecutionException(e);
76          }
77      }
78  }