View Javadoc

1   /*
2    * $Id: MuleClientReceiveJob.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.providers.quartz.jobs;
12  
13  import org.mule.MuleManager;
14  import org.mule.extras.client.MuleClient;
15  import org.mule.providers.quartz.QuartzConnector;
16  import org.mule.providers.quartz.i18n.QuartzMessages;
17  import org.mule.umo.UMOException;
18  import org.mule.umo.UMOMessage;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.quartz.Job;
23  import org.quartz.JobDataMap;
24  import org.quartz.JobExecutionContext;
25  import org.quartz.JobExecutionException;
26  
27  /**
28   * Will receive on an endpoint and dispatch the result on another.
29   */
30  public class MuleClientReceiveJob 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  
41          String dispatchEndpoint = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_DISPATCH_ENDPOINT);
42          if (dispatchEndpoint == null)
43          {
44              throw new JobExecutionException(
45                  QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_DISPATCH_ENDPOINT).getMessage());
46          }
47  
48          String receiveEndpoint = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_RECEIVE_ENDPOINT);
49          if (receiveEndpoint == null)
50          {
51              throw new JobExecutionException(
52                  QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_RECEIVE_ENDPOINT).getMessage());
53          }
54          long timeout = MuleManager.getConfiguration().getSynchronousEventTimeout();
55          String timeoutString = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_RECEIVE_TIMEOUT);
56          if (timeoutString != null)
57          {
58              timeout = Long.parseLong(timeoutString);
59          }
60          try
61          {
62              MuleClient client = new MuleClient();
63              logger.debug("Attempting to receive event on: " + receiveEndpoint);
64              UMOMessage result = client.receive(receiveEndpoint, timeout);
65              if (result != null)
66              {
67                  logger.debug("Received event on: " + receiveEndpoint);
68                  logger.debug("Dispatching result on: " + dispatchEndpoint);
69                  result.addProperties(jobDataMap);
70                  client.dispatch(dispatchEndpoint, result);
71              }
72          }
73          catch (UMOException e)
74          {
75              throw new JobExecutionException(e);
76          }
77      }
78  }