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