View Javadoc

1   /*
2    * $Id: AbstractEventAggregator.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.inbound;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.api.MessagingException;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.routing.EventCorrelator;
19  import org.mule.routing.EventCorrelatorCallback;
20  
21  import javax.resource.spi.work.WorkException;
22  
23  /**
24   * <code>AbstractEventAggregator</code> will aggregate a set of messages into a
25   * single message.
26   */
27  
28  public abstract class AbstractEventAggregator extends SelectiveConsumer
29  {
30      private EventCorrelator eventCorrelator;
31  
32      private int timeout = 0;
33  
34      //@Override
35      public void initialise() throws InitialisationException
36      {
37          eventCorrelator = new EventCorrelator(getCorrelatorCallback(), getMessageInfoMapping(), muleContext);
38          if(timeout != 0)
39          {
40              eventCorrelator.setTimeout(timeout);
41              try
42              {
43                  eventCorrelator.enableTimeoutMonitor();
44              }
45              catch (WorkException e)
46              {
47                  throw new InitialisationException(e, this);
48              }
49          }
50          super.initialise();
51      }
52  
53      protected abstract EventCorrelatorCallback getCorrelatorCallback();
54  
55  
56      //@Override
57      public MuleEvent[] process(MuleEvent event) throws MessagingException
58      {
59          MuleMessage msg = eventCorrelator.process(event);
60          if(msg==null)
61          {
62              return null;
63          }
64          MuleEvent[] result = new MuleEvent[]{new DefaultMuleEvent(msg, event)};
65          return result;
66      }
67      
68      public int getTimeout()
69      {
70          return timeout;
71      }
72  
73      public void setTimeout(int timeout)
74      {
75          this.timeout = timeout;
76      }
77  }