View Javadoc

1   /*
2    * $Id: AsynchronousProcessingStrategy.java 22864 2011-09-05 17:18:46Z dfeist $
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.processor.strategy;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.ThreadingProfile;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.api.processor.MessageProcessorChainBuilder;
17  import org.mule.api.processor.ProcessingStrategy;
18  import org.mule.config.ChainedThreadingProfile;
19  import org.mule.processor.AsyncInterceptingMessageProcessor;
20  import org.mule.util.concurrent.ThreadNameHelper;
21  
22  import java.util.List;
23  
24  import javax.resource.spi.work.WorkManager;
25  
26  /**
27   * This strategy uses a {@link WorkManager} to schedule the processing of the pipeline of message processors
28   * in a single worker thread.
29   */
30  public class AsynchronousProcessingStrategy implements ProcessingStrategy
31  {
32  
33      protected Integer maxThreads;
34      protected Integer minThreads;
35      protected Integer maxBufferSize;
36      protected Long threadTTL;
37      protected Long threadWaitTimeout;
38      protected Integer poolExhaustedAction;
39      protected ProcessingStrategy synchronousProcessingStrategy = new SynchronousProcessingStrategy();
40  
41      @Override
42      public void configureProcessors(List<MessageProcessor> processors,
43                                      StageNameSource nameSource,
44                                      MessageProcessorChainBuilder chainBuilder,
45                                      MuleContext muleContext)
46      {
47          if (processors.size() > 0)
48          {
49              chainBuilder.chain(createAsyncMessageProcessor(nameSource, muleContext));
50              synchronousProcessingStrategy.configureProcessors(processors, nameSource, chainBuilder,
51                  muleContext);
52          }
53      }
54  
55      protected AsyncInterceptingMessageProcessor createAsyncMessageProcessor(StageNameSource nameSource,
56                                                                              MuleContext muleContext)
57      {
58          return new AsyncInterceptingMessageProcessor(createThreadingProfile(muleContext), getThreadPoolName(
59              nameSource.getName(), muleContext), muleContext.getConfiguration().getShutdownTimeout());
60      }
61  
62      protected ThreadingProfile createThreadingProfile(MuleContext muleContext)
63      {
64          ThreadingProfile threadingProfile = new ChainedThreadingProfile(
65              muleContext.getDefaultThreadingProfile());
66          if (maxThreads != null) threadingProfile.setMaxThreadsActive(maxThreads);
67          if (minThreads != null) threadingProfile.setMaxThreadsIdle(minThreads);
68          if (maxBufferSize != null) threadingProfile.setMaxBufferSize(maxBufferSize);
69          if (threadTTL != null) threadingProfile.setThreadTTL(threadTTL);
70          if (threadWaitTimeout != null) threadingProfile.setThreadWaitTimeout(threadWaitTimeout);
71          if (poolExhaustedAction != null) threadingProfile.setPoolExhaustedAction(poolExhaustedAction);
72          threadingProfile.setMuleContext(muleContext);
73          return threadingProfile;
74      }
75  
76      protected String getThreadPoolName(String stageName, MuleContext muleContext)
77      {
78          return ThreadNameHelper.flow(muleContext, stageName);
79      }
80  
81      public Integer getMaxThreads()
82      {
83          return maxThreads;
84      }
85  
86      public void setMaxThreads(Integer maxThreads)
87      {
88          this.maxThreads = maxThreads;
89      }
90  
91      public Integer getMinThreads()
92      {
93          return minThreads;
94      }
95  
96      public void setMinThreads(Integer minThreads)
97      {
98          this.minThreads = minThreads;
99      }
100 
101     public void setMaxBufferSize(Integer maxBufferSize)
102     {
103         this.maxBufferSize = maxBufferSize;
104     }
105 
106     public void setThreadTTL(Long threadTTL)
107     {
108         this.threadTTL = threadTTL;
109     }
110 
111     public void setThreadWaitTimeout(Long threadWaitTimeout)
112     {
113         this.threadWaitTimeout = threadWaitTimeout;
114     }
115 
116     public void setPoolExhaustedAction(Integer poolExhaustedAction)
117     {
118         this.poolExhaustedAction = poolExhaustedAction;
119     }
120 
121     public Integer getMaxBufferSize()
122     {
123         return maxBufferSize;
124     }
125 
126     public Long getThreadTTL()
127     {
128         return threadTTL;
129     }
130 
131     public Long getThreadWaitTimeout()
132     {
133         return threadWaitTimeout;
134     }
135 
136     public Integer getPoolExhaustedAction()
137     {
138         return poolExhaustedAction;
139     }
140 
141 }