1 package org.apache.commons.jcs3.engine.control.event;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.atomic.AtomicBoolean;
25
26 import org.apache.commons.jcs3.engine.control.event.behavior.IElementEvent;
27 import org.apache.commons.jcs3.engine.control.event.behavior.IElementEventHandler;
28 import org.apache.commons.jcs3.engine.control.event.behavior.IElementEventQueue;
29 import org.apache.commons.jcs3.log.Log;
30 import org.apache.commons.jcs3.log.LogManager;
31 import org.apache.commons.jcs3.utils.threadpool.PoolConfiguration;
32 import org.apache.commons.jcs3.utils.threadpool.PoolConfiguration.WhenBlockedPolicy;
33 import org.apache.commons.jcs3.utils.threadpool.ThreadPoolManager;
34
35
36
37
38 public class ElementEventQueue
39 implements IElementEventQueue
40 {
41 private static final String THREAD_PREFIX = "JCS-ElementEventQueue-";
42
43
44 private static final Log log = LogManager.getLog( ElementEventQueue.class );
45
46
47 private final AtomicBoolean destroyed = new AtomicBoolean(false);
48
49
50 private final ExecutorService queueProcessor;
51
52
53
54
55 public ElementEventQueue()
56 {
57 queueProcessor = ThreadPoolManager.getInstance().createPool(
58 new PoolConfiguration(false, 0, 1, 1, 0, WhenBlockedPolicy.RUN, 1), THREAD_PREFIX);
59
60 log.debug( "Constructed: {0}", this );
61 }
62
63
64
65
66 @Override
67 public void dispose()
68 {
69 if (destroyed.compareAndSet(false, true))
70 {
71
72
73 log.info( "Element event queue destroyed: {0}", this );
74 }
75 }
76
77
78
79
80
81
82
83 @Override
84 public <T> void addElementEvent( final IElementEventHandler hand, final IElementEvent<T> event )
85 throws IOException
86 {
87
88 log.debug("Adding Event Handler to QUEUE, !destroyed = {0}", !destroyed.get());
89
90 if (destroyed.get())
91 {
92 log.warn("Event submitted to disposed element event queue {0}", event);
93 }
94 else
95 {
96 queueProcessor.execute(() -> hand.handleElementEvent(event));
97 }
98 }
99
100
101
102
103
104
105
106 @Deprecated
107 protected abstract class AbstractElementEventRunner
108 implements Runnable
109 {
110
111
112
113 @Override
114 public void run()
115 {
116 try
117 {
118 doRun();
119
120 }
121 catch ( final IOException e )
122 {
123
124 log.warn( "Giving up element event handling {0}", ElementEventQueue.this, e );
125 }
126 }
127
128
129
130
131
132
133 protected abstract void doRun()
134 throws IOException;
135 }
136 }