View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.processor;
8   
9   import org.mule.api.MessagingException;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.api.config.ThreadingProfile;
13  import org.mule.api.context.WorkManagerSource;
14  
15  /**
16   * Implementation of {@link AsyncInterceptingMessageProcessor} which continues
17   * processing in the same thread if the inbound endpoint has an exchange pattern that
18   * has a response or if a transaction is present. Execution of the next message
19   * processor is only passed off to another thread if this is not the case.
20   */
21  public class OptionalAsyncInterceptingMessageProcessor extends AsyncInterceptingMessageProcessor
22  {
23      public OptionalAsyncInterceptingMessageProcessor(WorkManagerSource workManagerSource)
24      {
25          super(workManagerSource);
26      }
27  
28      @Deprecated
29      public OptionalAsyncInterceptingMessageProcessor(WorkManagerSource workManagerSource, boolean doThreading)
30      {
31          super(workManagerSource, doThreading);
32      }
33  
34      public OptionalAsyncInterceptingMessageProcessor(ThreadingProfile threadingProfile,
35                                                       String name,
36                                                       int shutdownTimeout)
37      {
38          super(threadingProfile, name, shutdownTimeout);
39      }
40  
41      @Override
42      protected boolean isProcessAsync(MuleEvent event) throws MessagingException
43      {   
44          Object messageProperty = event.getMessage().getInboundProperty(MuleProperties.MULE_FORCE_SYNC_PROPERTY);
45          boolean forceSync = Boolean.TRUE.equals(messageProperty);
46          
47          boolean hasResponse = event.getEndpoint().getExchangePattern().hasResponse();
48          boolean isTransacted = event.getEndpoint().getTransactionConfig().isTransacted();
49          
50          return !forceSync && doThreading && !hasResponse && !isTransacted;
51      }
52  
53  }