View Javadoc

1   /*
2    * $Id: AsyncMessageProcessorsFactoryBean.java 20531 2010-12-08 21:34:21Z dzapata $
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.config.spring.factories;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.NamedObject;
15  import org.mule.api.config.MuleConfiguration;
16  import org.mule.api.config.ThreadingProfile;
17  import org.mule.api.context.MuleContextAware;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.processor.MessageProcessorBuilder;
20  import org.mule.processor.AsyncInterceptingMessageProcessor;
21  import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
22  import org.mule.util.concurrent.ThreadNameHelper;
23  
24  import java.util.List;
25  
26  import org.springframework.beans.factory.FactoryBean;
27  
28  public class AsyncMessageProcessorsFactoryBean implements FactoryBean, MuleContextAware, NamedObject
29  {
30  
31      protected MuleContext muleContext;
32  
33      protected List messageProcessors;
34      protected ThreadingProfile threadingProfile;
35      protected String name;
36  
37      public Class getObjectType()
38      {
39          return MessageProcessor.class;
40      }
41  
42      public void setThreadingProfile(ThreadingProfile threadingProfile)
43      {
44          this.threadingProfile = threadingProfile;
45      }
46  
47      public void setMessageProcessors(List messageProcessors)
48      {
49          this.messageProcessors = messageProcessors;
50      }
51  
52      public Object getObject() throws Exception
53      {
54          if (threadingProfile == null)
55          {
56              threadingProfile = muleContext.getDefaultThreadingProfile();
57          }
58  
59          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
60          final MuleConfiguration config = muleContext.getConfiguration();
61          final String threadPrefix = ThreadNameHelper.asyncProcessor(muleContext, name);
62  
63          AsyncInterceptingMessageProcessor asyncProcessor = new AsyncInterceptingMessageProcessor(threadingProfile,
64                                                                                                   threadPrefix,
65                                                                                                   config.getShutdownTimeout());
66          builder.chain(asyncProcessor);
67          for (Object processor : messageProcessors)
68          {
69              if (processor instanceof MessageProcessor)
70              {
71                  builder.chain((MessageProcessor) processor);
72              }
73              else if (processor instanceof MessageProcessorBuilder)
74              {
75                  builder.chain((MessageProcessorBuilder) processor);
76              }
77              else
78              {
79                  throw new IllegalArgumentException(
80                      "MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
81              }
82          }
83          return builder.build();
84      }
85  
86      public boolean isSingleton()
87      {
88          return false;
89      }
90  
91      public void setMuleContext(MuleContext context)
92      {
93          this.muleContext = context;
94      }
95  
96      public String getName()
97      {
98          return name;
99      }
100 
101     public void setName(String name)
102     {
103         this.name = name;
104     }
105 
106 }