View Javadoc

1   /*
2    * $Id: AsynchronousEventListener.java 7976 2007-08-21 14:26:13Z 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.extras.spring.events;
12  
13  import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
14  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.springframework.context.ApplicationEvent;
18  import org.springframework.context.ApplicationListener;
19  
20  /**
21   * <code>AsynchronousEventListener</code> will proces a received Event in a
22   * separate Thread. The thread pool passed in the constructor will determine how many
23   * threads can be executed at any time.
24   */
25  
26  public class AsynchronousEventListener implements MuleEventListener
27  {
28      /**
29       * logger used by this class
30       */
31      protected static final Log logger = LogFactory.getLog(AsynchronousEventListener.class);
32  
33      /**
34       * The listener to delegate to
35       */
36      private final ApplicationListener listener;
37  
38      /**
39       * the pool that manages the threads of execution
40       */
41      private final ExecutorService threadPool;
42  
43      public AsynchronousEventListener(ExecutorService threadPool, ApplicationListener listener)
44      {
45          this.threadPool = threadPool;
46          this.listener = listener;
47      }
48  
49      public void onApplicationEvent(ApplicationEvent event)
50      {
51          try
52          {
53              threadPool.execute(new Worker(listener, event));
54          }
55          catch (RejectedExecutionException e)
56          {
57              logger.error("Failed to execute worker for event: " + event.toString(), e);
58          }
59      }
60  
61      public ApplicationListener getListener()
62      {
63          return listener;
64      }
65  
66      private static class Worker implements Runnable
67      {
68          private final ApplicationListener listener;
69          private final ApplicationEvent event;
70  
71          public Worker(ApplicationListener listener, ApplicationEvent event)
72          {
73              this.listener = listener;
74              this.event = event;
75          }
76  
77          public void run()
78          {
79              try
80              {
81                  listener.onApplicationEvent(event);
82              }
83              catch (Exception e)
84              {
85                  logger.error("Failed to forward event: " + event.toString(), e);
86              }
87          }
88      }
89  
90  }