1
2
3
4
5
6
7
8
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
23
24
25
26
27 public class AsynchronousEventListener implements MuleEventListener
28 {
29
30
31
32 protected static final Log logger = LogFactory.getLog(AsynchronousEventListener.class);
33
34
35
36
37 private final ApplicationListener listener;
38
39
40
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 }