View Javadoc

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