Skip to content

Commit 25794a9

Browse files
timwtglman
authored andcommitted
Add tracing support to scheduled executor services.
1 parent 1b9cbe2 commit 25794a9

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.orientechnologies.common.thread;
2+
3+
import java.util.Objects;
4+
import java.util.concurrent.*;
5+
6+
public class OSourceTraceScheduledExecutorService extends OSourceTraceExecutorService
7+
implements ScheduledExecutorService {
8+
9+
private final ScheduledExecutorService scheduledService;
10+
11+
public OSourceTraceScheduledExecutorService(ScheduledExecutorService service) {
12+
super(service);
13+
Objects.requireNonNull(service);
14+
this.scheduledService = service;
15+
}
16+
17+
@Override
18+
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
19+
final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(command);
20+
return scheduledService.schedule(
21+
() -> {
22+
try {
23+
command.run();
24+
} catch (RuntimeException e) {
25+
throw OTracedExecutionException.trace(trace, e, command);
26+
}
27+
},
28+
delay,
29+
unit);
30+
}
31+
32+
@Override
33+
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
34+
final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(callable);
35+
return scheduledService.schedule(
36+
() -> {
37+
try {
38+
return callable.call();
39+
} catch (RuntimeException e) {
40+
throw OTracedExecutionException.trace(trace, e, callable);
41+
}
42+
},
43+
delay,
44+
unit);
45+
}
46+
47+
@Override
48+
public ScheduledFuture<?> scheduleAtFixedRate(
49+
Runnable command, long initialDelay, long period, TimeUnit unit) {
50+
final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(command);
51+
return scheduledService.scheduleAtFixedRate(
52+
() -> {
53+
try {
54+
command.run();
55+
} catch (RuntimeException e) {
56+
throw OTracedExecutionException.trace(trace, e, command);
57+
}
58+
},
59+
initialDelay,
60+
period,
61+
unit);
62+
}
63+
64+
public ScheduledFuture<?> scheduleWithFixedDelay(
65+
Runnable command, long initialDelay, long delay, TimeUnit unit) {
66+
final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(command);
67+
return scheduledService.scheduleWithFixedDelay(
68+
() -> {
69+
try {
70+
command.run();
71+
} catch (RuntimeException e) {
72+
throw OTracedExecutionException.trace(trace, e, command);
73+
}
74+
},
75+
initialDelay,
76+
delay,
77+
unit);
78+
}
79+
}

core/src/main/java/com/orientechnologies/common/thread/OThreadPoolExecutors.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.orientechnologies.common.thread;
22

3+
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
34
import java.util.concurrent.*;
45

56
public class OThreadPoolExecutors {
@@ -112,7 +113,13 @@ public static ScheduledExecutorService newSingleThreadScheduledPool(String threa
112113

113114
public static ScheduledExecutorService newSingleThreadScheduledPool(
114115
String threadName, ThreadGroup parentThreadGroup) {
115-
return new OScheduledThreadPoolExecutorWithLogging(
116-
1, new SingletonNamedThreadFactory(threadName, parentThreadGroup));
116+
OScheduledThreadPoolExecutorWithLogging exec =
117+
new OScheduledThreadPoolExecutorWithLogging(
118+
1, new SingletonNamedThreadFactory(threadName, parentThreadGroup));
119+
if (OGlobalConfiguration.EXECUTOR_DEBUG_TRACE_SOURCE.getValueAsBoolean()) {
120+
return new OSourceTraceScheduledExecutorService(exec);
121+
} else {
122+
return exec;
123+
}
117124
}
118125
}

0 commit comments

Comments
 (0)