forked from apache/lens
/
AsyncEventListener.java
138 lines (123 loc) · 4.29 KB
/
AsyncEventListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.lens.server.api.events;
import java.util.concurrent.*;
import org.apache.lens.server.api.error.LensException;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* Event listeners should implement this class if they wish to process events asynchronously. This should be used when
* event processing can block, or is computationally intensive.
*
* @param <T> the generic type
*/
@Slf4j
public abstract class AsyncEventListener<T extends LensEvent> implements LensEventListener<T> {
/**
* The processor.
*/
protected final ThreadPoolExecutor processor;
/**
* The event queue.
*/
protected final BlockingQueue<Runnable> eventQueue;
/**
* Name of this Asynchronous Event Listener. Will be used for logging and to name the threads in thread pool that
* allow asynchronous handling of events. If required, Sub Classes can override <code>getName</code> method to
* provide more appropriate name.
*
* Default value is the class Name (Example QueryEndNotifier, ResultFormatter, etc)
*/
@Getter(AccessLevel.PROTECTED)
private final String name = this.getClass().getSimpleName();
/**
* Create a single threaded event listener with an unbounded queue, with daemon threads.
*/
public AsyncEventListener() {
this(1);
}
/**
* Create a event listener with poolSize threads with an unbounded queue and daemon threads.
*
* @param poolSize the pool size
*/
public AsyncEventListener(int poolSize) {
this(poolSize, 10, true);
}
/**
* Create an asynchronous event listener which uses a thread poool to process events.
*
* @param poolSize size of the event processing pool
* @param timeOutSeconds time out in seconds when an idle thread is destroyed
* @param isDaemon if the threads used to process should be daemon threads,
* if false, then implementation should call stop()
* to stop the thread pool
*/
public AsyncEventListener(int poolSize, long timeOutSeconds, final boolean isDaemon) {
eventQueue = new LinkedBlockingQueue<>();
ThreadFactory factory = new BasicThreadFactory.Builder()
.namingPattern(getName()+"_AsyncThread-%d")
.daemon(isDaemon)
.priority(Thread.NORM_PRIORITY)
.build();
// fixed pool with min and max equal to poolSize
processor = new ThreadPoolExecutor(poolSize, poolSize, timeOutSeconds, TimeUnit.SECONDS, eventQueue, factory);
processor.allowCoreThreadTimeOut(true);
}
/**
* Creates a new runnable and calls the process method in it.
*
* @param event the event
* @throws LensException the lens exception
*/
@Override
public void onEvent(final T event) throws LensException {
try {
processor.execute(new Runnable() {
@Override
public void run() {
try {
process(event);
} catch (Throwable e) {
log.error("{} Failed to process event {}", getName(), event, e);
}
}
});
} catch (RejectedExecutionException rejected) {
throw new LensException(rejected);
}
}
/**
* Should implement the actual event handling.
*
* @param event the event
*/
public abstract void process(T event);
/**
* Should be called to stop the event processor thread.
*/
public void stop() {
processor.shutdownNow();
}
public BlockingQueue<Runnable> getEventQueue() {
return eventQueue;
}
}