1
2
3
4
5
6
7
8
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
22
23
24
25
26 public class AsynchronousEventListener implements MuleEventListener
27 {
28
29
30
31 protected static final Log logger = LogFactory.getLog(AsynchronousEventListener.class);
32
33
34
35
36 private final ApplicationListener listener;
37
38
39
40
41 private final ExecutorService threadPool;
42
43 public AsynchronousEventListener(ExecutorService threadPool, ApplicationListener listener)
44 {
45 this.threadPool = threadPool;
46 this.listener = listener;
47 }
48
49 public void onApplicationEvent(ApplicationEvent event)
50 {
51 try
52 {
53 threadPool.execute(new Worker(listener, event));
54 }
55 catch (RejectedExecutionException e)
56 {
57 logger.error("Failed to execute worker for event: " + event.toString(), e);
58 }
59 }
60
61 public ApplicationListener getListener()
62 {
63 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 {
73 this.listener = listener;
74 this.event = event;
75 }
76
77 public void run()
78 {
79 try
80 {
81 listener.onApplicationEvent(event);
82 }
83 catch (Exception e)
84 {
85 logger.error("Failed to forward event: " + event.toString(), e);
86 }
87 }
88 }
89
90 }