Skip to content

Commit e184e7c

Browse files
committed
FELIX-6784 Changed the WorkerQueue to be based on the Java's standard ScheduledThreadPoolExecutor. This already implements keeping track of tasks, and allows for the configuring a keep alive time.
1 parent 60bd13b commit e184e7c

File tree

2 files changed

+21
-56
lines changed

2 files changed

+21
-56
lines changed

configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public Bundle addingBundle(final Bundle bundle, final BundleEvent event) {
105105
if ( active &&
106106
(state == Bundle.ACTIVE || state == Bundle.STARTING) ) {
107107
SystemLogger.debug("Adding bundle " + getBundleIdentity(bundle) + " : " + getBundleState(state));
108-
queue.enqueue(new Runnable() {
108+
queue.submit(new Runnable() {
109109

110110
@Override
111111
public void run() {
@@ -128,7 +128,7 @@ public void removedBundle(final Bundle bundle, final BundleEvent event, final Bu
128128
final int state = bundle.getState();
129129
if ( active && state == Bundle.UNINSTALLED ) {
130130
SystemLogger.debug("Removing bundle " + getBundleIdentity(bundle) + " : " + getBundleState(state));
131-
queue.enqueue(new Runnable() {
131+
queue.submit(new Runnable() {
132132

133133
@Override
134134
public void run() {
@@ -148,7 +148,7 @@ public void run() {
148148
}
149149

150150
public void configAdminAdded() {
151-
queue.enqueue(new Runnable() {
151+
queue.submit(new Runnable() {
152152

153153
@Override
154154
public void run() {
@@ -182,7 +182,7 @@ private String getBundleState(int state) {
182182
*/
183183
public void shutdown() {
184184
this.active = false;
185-
this.queue.stop();
185+
this.queue.shutdownNow();
186186
this.tracker.close();
187187
}
188188

configurator/src/main/java/org/apache/felix/configurator/impl/WorkerQueue.java

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,32 @@
1818
*/
1919
package org.apache.felix.configurator.impl;
2020

21-
import java.util.ArrayList;
22-
import java.util.List;
23-
import java.util.concurrent.Executors;
21+
import java.util.concurrent.ScheduledThreadPoolExecutor;
2422
import java.util.concurrent.ThreadFactory;
23+
import java.util.concurrent.TimeUnit;
2524

2625
import org.apache.felix.configurator.impl.logger.SystemLogger;
2726

28-
public class WorkerQueue implements Runnable {
29-
30-
private final ThreadFactory threadFactory;
31-
32-
private final List<Runnable> tasks = new ArrayList<>();
33-
34-
private volatile Thread backgroundThread;
35-
36-
private volatile boolean stopped = false;
27+
public class WorkerQueue extends ScheduledThreadPoolExecutor {
3728

3829
public WorkerQueue() {
39-
this.threadFactory = Executors.defaultThreadFactory();
40-
}
41-
42-
public void stop() {
43-
synchronized ( this.tasks ) {
44-
this.stopped = true;
45-
}
46-
}
47-
48-
public void enqueue(final Runnable r) {
49-
synchronized ( this.tasks ) {
50-
if ( !this.stopped ) {
51-
this.tasks.add(r);
52-
if ( this.backgroundThread == null ) {
53-
this.backgroundThread = this.threadFactory.newThread(this);
54-
this.backgroundThread.setDaemon(true);
55-
this.backgroundThread.setName("Apache Felix Configurator Worker Thread");
56-
this.backgroundThread.start();
57-
}
30+
super(1, new ThreadFactory() {
31+
@Override
32+
public Thread newThread(Runnable runnable) {
33+
Thread thread = new Thread(runnable);
34+
thread.setDaemon(true);
35+
thread.setName("Apache Felix Configurator Worker Thread");
36+
return thread;
5837
}
59-
}
38+
});
39+
setKeepAliveTime(5, TimeUnit.SECONDS);
40+
allowCoreThreadTimeOut(true);
6041
}
6142

6243
@Override
63-
public void run() {
64-
Runnable r;
65-
do {
66-
r = null;
67-
synchronized ( this.tasks ) {
68-
if ( !this.stopped && !this.tasks.isEmpty() ) {
69-
r = this.tasks.remove(0);
70-
} else {
71-
this.backgroundThread = null;
72-
}
73-
}
74-
if ( r != null ) {
75-
try {
76-
r.run();
77-
} catch ( final Throwable t) {
78-
// just to be sure our loop never dies
79-
SystemLogger.error("Error processing task" + t.getMessage(), t);
80-
}
81-
}
82-
} while ( r != null );
44+
protected void afterExecute(Runnable runnable, Throwable throwable) {
45+
if (throwable != null) {
46+
SystemLogger.error("Error processing task" + throwable.getMessage(), throwable);
47+
}
8348
}
8449
}

0 commit comments

Comments
 (0)