View Javadoc

1   /*
2    * $Id: EndpointPollingJob.java 19360 2010-09-03 20:54:28Z rossmason $
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.MuleMessage;
15  import org.mule.api.ThreadSafeAccess;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.endpoint.EndpointBuilder;
18  import org.mule.api.endpoint.InboundEndpoint;
19  import org.mule.api.transaction.Transaction;
20  import org.mule.api.transaction.TransactionCallback;
21  import org.mule.api.transport.PropertyScope;
22  import org.mule.module.client.MuleClient;
23  import org.mule.transaction.MuleTransactionConfig;
24  import org.mule.transaction.TransactionCoordination;
25  import org.mule.transaction.TransactionTemplate;
26  import org.mule.transport.AbstractMessageReceiver;
27  import org.mule.transport.quartz.QuartzConnector;
28  import org.mule.transport.quartz.QuartzMessageReceiver;
29  import org.mule.transport.quartz.i18n.QuartzMessages;
30  
31  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.quartz.Job;
35  import org.quartz.JobDataMap;
36  import org.quartz.JobExecutionContext;
37  import org.quartz.JobExecutionException;
38  import org.quartz.SchedulerException;
39  
40  /**
41   * Will receive on an endpoint and dispatch it to the component set via the Receiver information.
42   */
43  public class EndpointPollingJob implements Job
44  {
45      /**
46       * The logger used for this class
47       */
48      protected transient Log logger = LogFactory.getLog(getClass());
49  
50      public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
51      {
52          MuleContext muleContext;
53          try
54          {
55              muleContext = (MuleContext)jobExecutionContext.getScheduler().getContext().get(MuleProperties.MULE_CONTEXT_PROPERTY);
56          }
57          catch (SchedulerException e)
58          {
59              throw new JobExecutionException("Failed to retrieve Mulecontext from the Scheduler Context: " + e.getMessage(), e);
60          }
61  
62          final JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
63  
64          String receiverKey = (String) jobDataMap.get(QuartzMessageReceiver.QUARTZ_RECEIVER_PROPERTY);
65          if (receiverKey == null)
66          {
67              throw new JobExecutionException(QuartzMessages.receiverNotInJobDataMap().getMessage());
68          }
69  
70          String connectorName = (String) jobDataMap.get(QuartzMessageReceiver.QUARTZ_CONNECTOR_PROPERTY);
71          if (connectorName == null)
72          {
73              throw new JobExecutionException(QuartzMessages.connectorNotInJobDataMap().getMessage());
74          }
75  
76          final QuartzConnector connector = (QuartzConnector) muleContext.getRegistry().lookupConnector(connectorName);
77          if (connector == null)
78          {
79              throw new JobExecutionException(QuartzMessages.noConnectorFound(connectorName).getMessage());
80          }
81  
82          final AbstractMessageReceiver receiver = (AbstractMessageReceiver) connector.lookupReceiver(receiverKey);
83          if (receiver == null)
84          {
85              throw new JobExecutionException(
86                      QuartzMessages.noReceiverInConnector(receiverKey, connectorName).getMessage());
87          }
88  
89  
90          final EndpointPollingJobConfig jobConfig = (EndpointPollingJobConfig) jobDataMap.get(QuartzConnector.PROPERTY_JOB_CONFIG);
91          if (jobConfig == null)
92          {
93              throw new JobExecutionException(
94                      QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
95          }
96  
97  
98          try
99          {
100             logger.debug("Attempting to receive event on: " + jobConfig.getEndpointRef());
101             TransactionTemplate<Void> tt;
102             final AtomicBoolean pollGlobalEndpoint = new AtomicBoolean(false);
103 
104             //TODO MULE-5050 work around because the builder is no longer idempotent, we now cache the endpoint instance
105             InboundEndpoint endpoint = muleContext.getRegistry().lookupObject(jobConfig.getEndpointRef() + ".quartz-job");
106             if(endpoint==null)
107             {
108                 final EndpointBuilder epBuilder = muleContext.getRegistry().lookupEndpointBuilder(jobConfig.getEndpointRef());
109                 pollGlobalEndpoint.set(epBuilder != null);
110 
111                 if (pollGlobalEndpoint.get())
112                 {
113                     // referencing a global endpoint, fetch configuration from it
114                     endpoint = epBuilder.buildInboundEndpoint();
115 
116                     //TODO MULE-5050 work around because the builder is no longer idempotent, we now cache the endpoint instance
117                     muleContext.getRegistry().registerObject(jobConfig.getEndpointRef() + ".quartz-job", endpoint);
118                     tt = new TransactionTemplate<Void>(endpoint.getTransactionConfig(), muleContext);
119                 }
120                 else
121                 {
122                     // a simple inline endpoint
123                     tt = new TransactionTemplate<Void>(new MuleTransactionConfig(), muleContext);
124                 }
125             }
126             else
127             {
128                 tt = new TransactionTemplate<Void>(endpoint.getTransactionConfig(), muleContext);
129             }
130 
131 
132             final InboundEndpoint finalEndpoint = endpoint;
133             TransactionCallback<Void> cb = new TransactionCallback<Void>()
134             {
135                 public Void doInTransaction() throws Exception
136                 {
137                     Transaction tx = TransactionCoordination.getInstance().getTransaction();
138                     if (tx != null)
139                     {
140                         tx.begin();
141                     }
142 
143                     MuleMessage result = null;
144                     if (pollGlobalEndpoint.get())
145                     {
146                         result = finalEndpoint.getConnector().request(finalEndpoint, jobConfig.getTimeout());
147                     }
148                     else
149                     {
150                         MuleClient client = new MuleClient(connector.getMuleContext());
151                         result = client.request(jobConfig.getEndpointRef(), jobConfig.getTimeout());
152                     }
153 
154                     if (result != null)
155                     {
156                         if (logger.isDebugEnabled())
157                         {
158                             logger.debug("Received event on: " + jobConfig.getEndpointRef());
159                         }
160                         if (pollGlobalEndpoint.get())
161                         {
162                             result.applyTransformers(null, finalEndpoint.getTransformers());
163                         }
164 
165                         //we need to do this because
166                         result = (MuleMessage) ((ThreadSafeAccess) result).newThreadCopy();
167 
168                         //Add the context properties to the message.
169                         result.addProperties(jobDataMap, PropertyScope.INVOCATION);
170 
171                         receiver.routeMessage(result);
172                     }
173                     // nowhere to return
174                     return null;
175                 }
176             };
177 
178             tt.execute(cb);
179         }
180         catch (RuntimeException rex)
181         {
182             // rethrow
183             throw rex;
184         }
185         catch (Exception e)
186         {
187             throw new JobExecutionException(e);
188         }
189     }
190 }