View Javadoc

1   /*
2    * $Id: AbstractAggregator.java 22506 2011-07-21 16:30:40Z stephen.fenech $
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.MessagingException;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.construct.FlowConstruct;
18  import org.mule.api.construct.FlowConstructAware;
19  import org.mule.api.context.MuleContextAware;
20  import org.mule.api.lifecycle.Initialisable;
21  import org.mule.api.lifecycle.InitialisationException;
22  import org.mule.api.lifecycle.Startable;
23  import org.mule.api.lifecycle.Stoppable;
24  import org.mule.api.routing.Aggregator;
25  import org.mule.api.routing.MessageInfoMapping;
26  import org.mule.api.service.Service;
27  import org.mule.processor.AbstractInterceptingMessageProcessor;
28  import org.mule.routing.correlation.EventCorrelator;
29  import org.mule.routing.correlation.EventCorrelatorCallback;
30  import org.mule.util.concurrent.ThreadNameHelper;
31  
32  /**
33   * <code>AbstractEventAggregator</code> will aggregate a set of messages into a
34   * single message. <b>EIP Reference:</b> <a
35   * href="http://www.eaipatterns.com/Aggregator.html"
36   * >http://www.eaipatterns.com/Aggregator.html</a>
37   */
38  
39  public abstract class AbstractAggregator extends AbstractInterceptingMessageProcessor
40      implements Initialisable, MuleContextAware, FlowConstructAware, Aggregator, Startable, Stoppable
41  {
42  
43      protected EventCorrelator eventCorrelator;
44      protected MuleContext muleContext;
45      protected FlowConstruct flowConstruct;
46      protected MessageInfoMapping messageInfoMapping;
47  
48      private long timeout = 0;
49      private boolean failOnTimeout = true;
50      protected boolean persistentStores;
51      protected String storePrefix = null;
52  
53      public void initialise() throws InitialisationException
54      {
55          if (messageInfoMapping == null)
56          {
57              messageInfoMapping = flowConstruct.getMessageInfoMapping();
58          }
59          if (storePrefix == null)
60          {
61              storePrefix = String.format("%s%s.%s.", ThreadNameHelper.getPrefix(muleContext),
62                  flowConstruct.getName(), this.getClass().getName());
63          }
64          eventCorrelator = new EventCorrelator(getCorrelatorCallback(muleContext), next, messageInfoMapping,
65              muleContext, flowConstruct.getName(), persistentStores, storePrefix);
66  
67          // Inherit failOnTimeout from async-reply if this aggregator is being used
68          // for async-reply
69          if (flowConstruct instanceof Service)
70          {
71              Service service = (Service) flowConstruct;
72              if (service.getAsyncReplyMessageSource().getMessageProcessors().contains(this))
73              {
74                  failOnTimeout = service.getAsyncReplyMessageSource().isFailOnTimeout();
75              }
76          }
77  
78          eventCorrelator.setTimeout(timeout);
79          eventCorrelator.setFailOnTimeout(isFailOnTimeout());
80      }
81  
82      public void start() throws MuleException
83      {
84          if (timeout != 0)
85          {
86              eventCorrelator.start();
87          }
88      }
89  
90      public void stop() throws MuleException
91      {
92          eventCorrelator.stop();
93      }
94  
95      public void setMuleContext(MuleContext context)
96      {
97          this.muleContext = context;
98      }
99  
100     protected abstract EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext);
101 
102     public MuleEvent process(MuleEvent event) throws MuleException
103     {
104         MuleEvent result = eventCorrelator.process(event);
105         if (result == null)
106         {
107             return null;
108         }
109         return processNext(result);
110     }
111 
112     public void expireAggregation(String groupId) throws MessagingException
113     {
114         eventCorrelator.forceGroupExpiry(groupId);
115     }
116 
117     public long getTimeout()
118     {
119         return timeout;
120     }
121 
122     public void setTimeout(long timeout)
123     {
124         this.timeout = timeout;
125     }
126 
127     public boolean isFailOnTimeout()
128     {
129         return failOnTimeout;
130     }
131 
132     public void setFailOnTimeout(boolean failOnTimeout)
133     {
134         this.failOnTimeout = failOnTimeout;
135     }
136 
137     public void setFlowConstruct(FlowConstruct flowConstruct)
138     {
139         this.flowConstruct = flowConstruct;
140     }
141 
142     public void setMessageInfoMapping(MessageInfoMapping messageInfoMapping)
143     {
144         this.messageInfoMapping = messageInfoMapping;
145     }
146 
147     public boolean isPersistentStores()
148     {
149         return persistentStores;
150     }
151 
152     public void setPersistentStores(boolean persistentStores)
153     {
154         this.persistentStores = persistentStores;
155     }
156 
157     public String getStorePrefix()
158     {
159         return storePrefix;
160     }
161 
162     public void setStorePrefix(String storePrefix)
163     {
164         this.storePrefix = storePrefix;
165     }
166 
167 }