View Javadoc

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