View Javadoc

1   /*
2    * $Id: QueuedAsynchronousProcessingStrategy.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.MuleProperties;
15  import org.mule.api.config.ThreadingProfile;
16  import org.mule.api.store.ListableObjectStore;
17  import org.mule.config.QueueProfile;
18  import org.mule.management.stats.QueueStatistics;
19  import org.mule.management.stats.QueueStatisticsAware;
20  import org.mule.processor.AsyncInterceptingMessageProcessor;
21  import org.mule.processor.SedaStageInterceptingMessageProcessor;
22  import org.mule.util.concurrent.ThreadNameHelper;
23  import org.mule.util.queue.QueueManager;
24  
25  import java.io.Serializable;
26  
27  import javax.resource.spi.work.WorkManager;
28  
29  /**
30   * This strategy uses a {@link QueueManager} to decouple receipt and processing of messages. The queue is
31   * polled and a {@link WorkManager} is used to schedule processing of the pipeline of message processors in a
32   * single worker thread.
33   */
34  public class QueuedAsynchronousProcessingStrategy extends AsynchronousProcessingStrategy
35      implements QueueStatisticsAware
36  {
37  
38      protected Integer queueTimeout;
39      protected Integer maxQueueSize = 0;
40      protected ListableObjectStore<Serializable> queueStore = null;
41      protected QueueStatistics queueStatistics;
42  
43      @Override
44      protected AsyncInterceptingMessageProcessor createAsyncMessageProcessor(StageNameSource nameSource,
45                                                                              MuleContext muleContext)
46      {
47          Integer timeout = queueTimeout != null ? queueTimeout : muleContext.getConfiguration()
48              .getDefaultQueueTimeout();
49  
50          initQueueStore(muleContext);
51  
52          QueueProfile queueProfile = new QueueProfile(maxQueueSize, queueStore);
53          ThreadingProfile threadingProfile = createThreadingProfile(muleContext);
54          String stageName = nameSource.getName();
55          return new SedaStageInterceptingMessageProcessor(ThreadNameHelper.flow(muleContext, stageName),
56              stageName, queueProfile, timeout, threadingProfile, queueStatistics, muleContext);
57      }
58  
59      protected void initQueueStore(MuleContext muleContext)
60      {
61          queueStore = muleContext.getRegistry().lookupObject(
62              MuleProperties.OBJECT_STORE_DEFAULT_IN_MEMORY_NAME);
63      }
64  
65      public Integer getQueueTimeout()
66      {
67          return queueTimeout;
68      }
69  
70      public void setQueueTimeout(Integer queueTimeout)
71      {
72          this.queueTimeout = queueTimeout;
73      }
74  
75      public Integer getMaxQueueSize()
76      {
77          return maxQueueSize;
78      }
79  
80      public void setMaxQueueSize(Integer maxQueueSize)
81      {
82          this.maxQueueSize = maxQueueSize;
83      }
84  
85      public ListableObjectStore<Serializable> getQueueStore()
86      {
87          return queueStore;
88      }
89  
90      public void setQueueStore(ListableObjectStore<Serializable> queueStore)
91      {
92          this.queueStore = queueStore;
93      }
94  
95      public QueueStatistics getQueueStatistics()
96      {
97          return queueStatistics;
98      }
99  
100     @Override
101     public void setQueueStatistics(QueueStatistics queueStatistics)
102     {
103         this.queueStatistics = queueStatistics;
104     }
105 }