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 | 0 | 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 | 0 | { |
46 | 0 | this.threadPool = threadPool; |
47 | 0 | this.listener = listener; |
48 | 0 | } |
49 | |
|
50 | |
public void onApplicationEvent(ApplicationEvent event) |
51 | |
{ |
52 | |
try |
53 | |
{ |
54 | 0 | 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 | 0 | } |
60 | 0 | } |
61 | |
|
62 | |
public ApplicationListener getListener() |
63 | |
{ |
64 | 0 | 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 | 0 | { |
74 | 0 | this.listener = listener; |
75 | 0 | this.event = event; |
76 | 0 | } |
77 | |
|
78 | |
public void run() |
79 | |
{ |
80 | |
try |
81 | |
{ |
82 | 0 | listener.onApplicationEvent(event); |
83 | |
} |
84 | 0 | catch (Exception e) |
85 | |
{ |
86 | 0 | logger.error("Failed to forward event: " + event.toString(), e); |
87 | 0 | } |
88 | 0 | } |
89 | |
} |
90 | |
|
91 | |
} |