View Javadoc

1   /*
2    * $Id: AbstractAggregator.java 19941 2010-10-15 15:47:52Z 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.routing;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.construct.FlowConstruct;
17  import org.mule.api.construct.FlowConstructAware;
18  import org.mule.api.context.MuleContextAware;
19  import org.mule.api.lifecycle.Initialisable;
20  import org.mule.api.lifecycle.InitialisationException;
21  import org.mule.api.routing.Aggregator;
22  import org.mule.api.routing.MessageInfoMapping;
23  import org.mule.api.service.Service;
24  import org.mule.processor.AbstractInterceptingMessageProcessor;
25  import org.mule.routing.correlation.EventCorrelator;
26  import org.mule.routing.correlation.EventCorrelatorCallback;
27  
28  import javax.resource.spi.work.WorkException;
29  
30  /**
31   * <code>AbstractEventAggregator</code> will aggregate a set of messages into a
32   * single message.
33   * 
34   * <b>EIP Reference:</b> <a href="http://www.eaipatterns.com/Aggregator.html">http://www.eaipatterns.com/Aggregator.html</a>
35   */
36  
37  public abstract class AbstractAggregator extends AbstractInterceptingMessageProcessor
38      implements Initialisable, MuleContextAware, FlowConstructAware, Aggregator
39  {
40  
41      protected EventCorrelator eventCorrelator;
42      protected MuleContext muleContext;
43      protected FlowConstruct flowConstruct;
44      protected MessageInfoMapping messageInfoMapping;
45  
46      private long timeout = 0;
47      private boolean failOnTimeout = true;
48  
49      public void initialise() throws InitialisationException
50      {
51          if (messageInfoMapping == null)
52          {
53              messageInfoMapping = flowConstruct.getMessageInfoMapping();
54          }
55  
56          eventCorrelator = new EventCorrelator(getCorrelatorCallback(muleContext), next, messageInfoMapping,
57              muleContext);
58  
59          // Inherit failOnTimeout from async-reply if this aggregator is being used for async-reply
60          if (flowConstruct instanceof Service)
61          {
62              Service service = (Service) flowConstruct;
63              if (service.getAsyncReplyMessageSource().getMessageProcessors().contains(this))
64              {
65                  failOnTimeout = service.getAsyncReplyMessageSource().isFailOnTimeout();
66              }
67          }
68          
69          eventCorrelator.setTimeout(timeout);
70          eventCorrelator.setFailOnTimeout(isFailOnTimeout());
71          if (timeout != 0)
72          {
73              try
74              {
75                  eventCorrelator.enableTimeoutMonitor();
76              }
77              catch (WorkException e)
78              {
79                  throw new InitialisationException(e, this);
80              }
81          }
82      }
83  
84      public void setMuleContext(MuleContext context)
85      {
86          this.muleContext = context;
87      }
88  
89      protected abstract EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext);
90  
91      public MuleEvent process(MuleEvent event) throws MuleException
92      {
93          MuleEvent result = eventCorrelator.process(event);
94          if (result == null)
95          {
96              return null;
97          }
98          return processNext(result);
99      }
100 
101     public void expireAggregation(String groupId)
102     {
103         eventCorrelator.forceGroupExpiry(groupId);
104     }
105 
106     public long getTimeout()
107     {
108         return timeout;
109     }
110 
111     public void setTimeout(long timeout)
112     {
113         this.timeout = timeout;
114     }
115 
116     public boolean isFailOnTimeout()
117     {
118         return failOnTimeout;
119     }
120 
121     public void setFailOnTimeout(boolean failOnTimeout)
122     {
123         this.failOnTimeout = failOnTimeout;
124     }
125 
126     public void setFlowConstruct(FlowConstruct flowConstruct)
127     {
128         this.flowConstruct = flowConstruct;
129     }
130 
131     public void setMessageInfoMapping(MessageInfoMapping messageInfoMapping)
132     {
133         this.messageInfoMapping = messageInfoMapping;
134     }
135 }