View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.routing;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.construct.FlowConstructAware;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.lifecycle.Initialisable;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.lifecycle.Startable;
18  import org.mule.api.lifecycle.Stoppable;
19  import org.mule.api.routing.Aggregator;
20  import org.mule.api.routing.MessageInfoMapping;
21  import org.mule.api.service.Service;
22  import org.mule.processor.AbstractInterceptingMessageProcessor;
23  import org.mule.routing.correlation.EventCorrelator;
24  import org.mule.routing.correlation.EventCorrelatorCallback;
25  
26  /**
27   * <code>AbstractEventAggregator</code> will aggregate a set of messages into a
28   * single message.
29   *
30   * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/Aggregator.html">http://www.eaipatterns.com/Aggregator.html</a>
31   */
32  
33  public abstract class AbstractAggregator extends AbstractInterceptingMessageProcessor
34          implements Initialisable, MuleContextAware, FlowConstructAware, Aggregator, Startable, Stoppable
35  {
36  
37      protected EventCorrelator eventCorrelator;
38      protected MuleContext muleContext;
39      protected FlowConstruct flowConstruct;
40      protected MessageInfoMapping messageInfoMapping;
41  
42      private long timeout = 0;
43      private boolean failOnTimeout = true;
44  
45      public void initialise() throws InitialisationException
46      {
47          if (messageInfoMapping == null)
48          {
49              messageInfoMapping = flowConstruct.getMessageInfoMapping();
50          }
51  
52          eventCorrelator = new EventCorrelator(getCorrelatorCallback(muleContext), next, messageInfoMapping,
53                                                muleContext, flowConstruct.getName());
54  
55          // Inherit failOnTimeout from async-reply if this aggregator is being used for async-reply
56          if (flowConstruct instanceof Service)
57          {
58              Service service = (Service) flowConstruct;
59              if (service.getAsyncReplyMessageSource().getMessageProcessors().contains(this))
60              {
61                  failOnTimeout = service.getAsyncReplyMessageSource().isFailOnTimeout();
62              }
63          }
64  
65          eventCorrelator.setTimeout(timeout);
66          eventCorrelator.setFailOnTimeout(isFailOnTimeout());
67      }
68  
69      public void start() throws MuleException 
70      {
71          if (timeout != 0)
72          {
73              eventCorrelator.start();
74          }
75      }
76  
77      public void stop() throws MuleException
78      {
79          eventCorrelator.stop();
80      }
81  
82      public void setMuleContext(MuleContext context)
83      {
84          this.muleContext = context;
85      }
86  
87      protected abstract EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext);
88  
89      public MuleEvent process(MuleEvent event) throws MuleException
90      {
91          MuleEvent result = eventCorrelator.process(event);
92          if (result == null)
93          {
94              return null;
95          }
96          return processNext(result);
97      }
98  
99      public void expireAggregation(String groupId)
100     {
101         eventCorrelator.forceGroupExpiry(groupId);
102     }
103 
104     public long getTimeout()
105     {
106         return timeout;
107     }
108 
109     public void setTimeout(long timeout)
110     {
111         this.timeout = timeout;
112     }
113 
114     public boolean isFailOnTimeout()
115     {
116         return failOnTimeout;
117     }
118 
119     public void setFailOnTimeout(boolean failOnTimeout)
120     {
121         this.failOnTimeout = failOnTimeout;
122     }
123 
124     public void setFlowConstruct(FlowConstruct flowConstruct)
125     {
126         this.flowConstruct = flowConstruct;
127     }
128 
129     public void setMessageInfoMapping(MessageInfoMapping messageInfoMapping)
130     {
131         this.messageInfoMapping = messageInfoMapping;
132     }
133 }