1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.spring.events; |
8 | |
|
9 | |
import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService; |
10 | |
import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException; |
11 | |
|
12 | |
import org.apache.commons.logging.Log; |
13 | |
import org.apache.commons.logging.LogFactory; |
14 | |
import org.springframework.context.ApplicationEvent; |
15 | |
import org.springframework.context.ApplicationListener; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public class AsynchronousEventListener implements MuleEventListener |
24 | |
{ |
25 | |
|
26 | |
|
27 | |
|
28 | 0 | protected static final Log logger = LogFactory.getLog(AsynchronousEventListener.class); |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
private final ApplicationListener listener; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
private final ExecutorService threadPool; |
39 | |
|
40 | |
public AsynchronousEventListener(ExecutorService threadPool, ApplicationListener listener) |
41 | 0 | { |
42 | 0 | this.threadPool = threadPool; |
43 | 0 | this.listener = listener; |
44 | 0 | } |
45 | |
|
46 | |
public void onApplicationEvent(ApplicationEvent event) |
47 | |
{ |
48 | |
try |
49 | |
{ |
50 | 0 | threadPool.execute(new Worker(listener, event)); |
51 | |
} |
52 | 0 | catch (RejectedExecutionException e) |
53 | |
{ |
54 | 0 | logger.error("Failed to execute worker for event: " + event.toString(), e); |
55 | 0 | } |
56 | 0 | } |
57 | |
|
58 | |
public ApplicationListener getListener() |
59 | |
{ |
60 | 0 | return listener; |
61 | |
} |
62 | |
|
63 | |
private static class Worker implements Runnable |
64 | |
{ |
65 | |
private final ApplicationListener listener; |
66 | |
private final ApplicationEvent event; |
67 | |
|
68 | |
public Worker(ApplicationListener listener, ApplicationEvent event) |
69 | 0 | { |
70 | 0 | this.listener = listener; |
71 | 0 | this.event = event; |
72 | 0 | } |
73 | |
|
74 | |
public void run() |
75 | |
{ |
76 | |
try |
77 | |
{ |
78 | 0 | listener.onApplicationEvent(event); |
79 | |
} |
80 | 0 | catch (Exception e) |
81 | |
{ |
82 | 0 | logger.error("Failed to forward event: " + event.toString(), e); |
83 | 0 | } |
84 | 0 | } |
85 | |
} |
86 | |
|
87 | |
} |