View Javadoc

1   /*
2    * $Id: QueuedThreadPerProcessorProcessingStrategy.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.processor.MessageProcessor;
15  import org.mule.api.processor.MessageProcessorBuilder;
16  import org.mule.api.processor.MessageProcessorChainBuilder;
17  import org.mule.util.queue.QueueManager;
18  
19  import java.util.List;
20  
21  import javax.resource.spi.work.WorkManager;
22  
23  /**
24   * This strategy uses the {@link QueueManager} to decouple the processing of each message processor. Each
25   * queue is polled and a {@link WorkManager} is used to schedule processing of the message processors in a new
26   * worker thread.
27   */
28  public class QueuedThreadPerProcessorProcessingStrategy extends QueuedAsynchronousProcessingStrategy
29  {
30  
31      @Override
32      public void configureProcessors(List<MessageProcessor> processors,
33                                      StageNameSource nameSource,
34                                      MessageProcessorChainBuilder builder,
35                                      MuleContext muleContext)
36      {
37          for (int i = 0; i < processors.size(); i++)
38          {
39              MessageProcessor processor = processors.get(i);
40  
41              builder.chain(createAsyncMessageProcessor(nameSource, muleContext));
42  
43              if (processor instanceof MessageProcessor)
44              {
45                  builder.chain(processor);
46              }
47              else if (processor instanceof MessageProcessorBuilder)
48              {
49                  builder.chain((MessageProcessorBuilder) processor);
50              }
51              else
52              {
53                  throw new IllegalArgumentException(
54                      "MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
55              }
56          }
57      }
58  }