1 10 11 package org.mule.extras.spring.events; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.springframework.context.ApplicationEvent; 16 import org.springframework.context.ApplicationListener; 17 18 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService; 19 import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException; 20 21 26 27 public class AsynchronousEventListener implements MuleEventListener 28 { 29 32 protected static Log logger = LogFactory.getLog(AsynchronousEventListener.class); 33 34 37 private final ApplicationListener listener; 38 39 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(event)); 55 } 56 catch (RejectedExecutionException e) 57 { 58 logger.error("Failed to process event: " + event.toString(), e); 59 } 60 } 61 62 private class Worker extends Thread 63 { 64 private ApplicationEvent event; 65 66 public Worker(ApplicationEvent event) 67 { 68 this.event = event; 69 } 70 71 public void run() 72 { 73 listener.onApplicationEvent(event); 74 } 75 } 76 77 public ApplicationListener getListener() 78 { 79 return listener; 80 } 81 } 82 | Popular Tags |