Debian Codegraph @

src/org/apache/commons/jcs3/engine/control/event/ElementEventQueue.java

raw symbols SCIP
1 package org.apache.commons.jcs3.engine.control.event;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * An event queue is used to propagate ordered cache events to one and only one target listener.
37 */
38 public class ElementEventQueue
39 implements IElementEventQueue
40 {
41 private static final String THREAD_PREFIX = "JCS-ElementEventQueue-";
42
43 /** The logger */
44 private static final Log log = LogManager.getLog( ElementEventQueue.class );
45
46 /** shutdown or not */
47 private final AtomicBoolean destroyed = new AtomicBoolean(false);
48
49 /** The worker thread pool. */
50 private final ExecutorService queueProcessor;
51
52 /**
53 * Constructor for the ElementEventQueue object
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 * Dispose queue
65 */
66 @Override
67 public void dispose()
68 {
69 if (destroyed.compareAndSet(false, true))
70 {
71 // Pool will be shut down by the ThreadPoolManager
72 // queueProcessor.shutdownNow();
73 log.info( "Element event queue destroyed: {0}", this );
74 }
75 }
76
77 /**
78 * Adds an ElementEvent to be handled
79 * @param hand The IElementEventHandler
80 * @param event The IElementEventHandler IElementEvent event
81 * @throws IOException
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 // /////////////////////////// Inner classes /////////////////////////////
101
102 /**
103 * Retries before declaring failure.
104 * @deprecated No longer used
105 */
106 @Deprecated
107 protected abstract class AbstractElementEventRunner
108 implements Runnable
109 {
110 /**
111 * Main processing method for the AbstractElementEvent object
112 */
113 @Override
114 public void run()
115 {
116 try
117 {
118 doRun();
119 // happy and done.
120 }
121 catch ( final IOException e )
122 {
123 // Too bad. The handler has problems.
124 log.warn( "Giving up element event handling {0}", ElementEventQueue.this, e );
125 }
126 }
127
128 /**
129 * This will do the work or trigger the work to be done.
130 * <p>
131 * @throws IOException
132 */
133 protected abstract void doRun()
134 throws IOException;
135 }
136 }