View Javadoc

1   /*
2    * $Id: AbstractResponseAggregator.java 11567 2008-04-11 13:08:05Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.response;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.api.routing.RoutingException;
17  import org.mule.routing.EventCorrelator;
18  import org.mule.routing.EventCorrelatorCallback;
19  import org.mule.routing.inbound.AbstractEventAggregator;
20  import org.mule.routing.inbound.EventGroup;
21  
22  /**
23   * <code>AbstractResponseAggregator</code> provides a base class for implementing
24   * response aggregator routers. This provides a thread-safe implemenetation and
25   * allows developers to customise how and when events are grouped and collated.
26   * Response Agrregators are used to collect responses that are usually sent to
27   * replyTo endpoints set on outbound routers. When an event is sent out via an
28   * outbound router, the response router will block the response flow on an
29   * Service until the Response Router resolves a reply or times out.
30   */
31  public abstract class AbstractResponseAggregator extends AbstractResponseRouter
32  {
33      private int timeout = -1; // undefined
34  
35      private boolean failOnTimeout = true;
36  
37      private EventCorrelator eventCorrelator;
38  
39      //@Override
40      public void initialise() throws InitialisationException
41      {
42          if (timeout == -1) // undefined
43          {
44              setTimeout(muleContext.getConfiguration().getDefaultSynchronousEventTimeout());
45          }
46          eventCorrelator = new EventCorrelator(getCorrelatorCallback(), messageInfoMapping, muleContext);
47          eventCorrelator.setTimeout(getTimeout());
48          eventCorrelator.setFailOnTimeout(isFailOnTimeout());
49          super.initialise();
50      }
51  
52      protected EventCorrelator getEventCorrelator()
53      {
54          return eventCorrelator;
55      }
56  
57  
58      public void process(MuleEvent event) throws RoutingException
59      {
60          eventCorrelator.addEvent(event);
61      }
62  
63      /**
64       * This method is called by the responding callee thread and should return the
65       * aggregated response message
66       *
67       * @param message
68       * @return
69       * @throws RoutingException
70       */
71      public MuleMessage getResponse(MuleMessage message) throws RoutingException
72      {
73          return eventCorrelator.getResponse(message);
74      }
75  
76  
77      public boolean isFailOnTimeout()
78      {
79          return failOnTimeout;
80      }
81  
82      public void setFailOnTimeout(boolean failOnTimeout)
83      {
84          this.failOnTimeout = failOnTimeout;
85      }
86  
87      public int getTimeout()
88      {
89          return timeout;
90      }
91  
92      public void setTimeout(int timeout)
93      {
94          this.timeout = timeout;
95      }
96  
97      protected abstract EventCorrelatorCallback getCorrelatorCallback();
98  
99      /**
100      * @see AbstractEventAggregator#aggregateEvents(EventGroup)
101      */
102     protected MuleMessage aggregateEvents(EventGroup events) throws RoutingException
103     {
104         return null;
105     }
106 
107 }