View Javadoc

1   /*
2    * $Id: ScheduledDispatchJob.java 11613 2008-04-20 20:30:10Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 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  
26  /**
27   * Will dispatch the current message to a Mule endpoint at a leter time.
28   * This job can be used to fire timebased events.
29   */
30  public class ScheduledDispatchJob implements Job
31  {
32      /**
33       * The logger used for this class
34       */
35      protected transient Log logger = LogFactory.getLog(getClass());
36  
37      public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
38      {
39          JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
40          Object payload = jobDataMap.get(QuartzConnector.PROPERTY_PAYLOAD);
41  
42          if (payload == null)
43          {
44              payload = NullPayload.getInstance();
45          }
46  
47          ScheduledDispatchJobConfig config = (ScheduledDispatchJobConfig)jobDataMap.get(QuartzConnector.PROPERTY_JOB_CONFIG);
48          if (config == null)
49          {
50              throw new JobExecutionException(
51                  QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
52          }
53  
54          try
55          {
56              MuleClient client = new MuleClient();
57              logger.debug("Dispatching payload on: " + config.getEndpointRef());
58              client.dispatch(config.getEndpointRef(), payload, jobDataMap);
59          }
60          catch (MuleException e)
61          {
62              throw new JobExecutionException(e);
63          }
64      }
65  }