Coverage Report - org.mule.extras.spring.events.AsynchronousEventListener
 
Classes in this File Line Coverage Branch Coverage Complexity
AsynchronousEventListener
82%
9/11
N/A
1.4
AsynchronousEventListener$Worker
78%
7/9
N/A
1.4
 
 1  
 /*
 2  
  * $Id: AsynchronousEventListener.java 7963 2007-08-21 08:53:15Z 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  4
     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  94
     {
 45  94
         this.threadPool = threadPool;
 46  94
         this.listener = listener;
 47  94
     }
 48  
 
 49  
     public void onApplicationEvent(ApplicationEvent event)
 50  
     {
 51  
         try
 52  
         {
 53  298
             threadPool.execute(new Worker(listener, event));
 54  
         }
 55  0
         catch (RejectedExecutionException e)
 56  
         {
 57  0
             logger.error("Failed to execute worker for event: " + event.toString(), e);
 58  298
         }
 59  298
     }
 60  
 
 61  
     public ApplicationListener getListener()
 62  
     {
 63  958
         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  298
         {
 73  298
             this.listener = listener;
 74  298
             this.event = event;
 75  298
         }
 76  
 
 77  
         public void run()
 78  
         {
 79  
             try
 80  
             {
 81  298
                 listener.onApplicationEvent(event);
 82  
             }
 83  0
             catch (Exception e)
 84  
             {
 85  0
                 logger.error("Failed to forward event: " + event.toString(), e);
 86  298
             }
 87  298
         }
 88  
     }
 89  
 
 90  
 }