Coverage Report - org.mule.module.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 10789 2008-02-12 20:04:43Z dfeist $
 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.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  2
     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  94
     {
 46  94
         this.threadPool = threadPool;
 47  94
         this.listener = listener;
 48  94
     }
 49  
 
 50  
     public void onApplicationEvent(ApplicationEvent event)
 51  
     {
 52  
         try
 53  
         {
 54  298
             threadPool.execute(new Worker(listener, event));
 55  
         }
 56  0
         catch (RejectedExecutionException e)
 57  
         {
 58  0
             logger.error("Failed to execute worker for event: " + event.toString(), e);
 59  298
         }
 60  298
     }
 61  
 
 62  
     public ApplicationListener getListener()
 63  
     {
 64  958
         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  298
         {
 74  298
             this.listener = listener;
 75  298
             this.event = event;
 76  298
         }
 77  
 
 78  
         public void run()
 79  
         {
 80  
             try
 81  
             {
 82  298
                 listener.onApplicationEvent(event);
 83  
             }
 84  0
             catch (Exception e)
 85  
             {
 86  0
                 logger.error("Failed to forward event: " + event.toString(), e);
 87  298
             }
 88  298
         }
 89  
     }
 90  
 
 91  
 }