|
@@ -0,0 +1,88 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2015 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.util;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.DMDircMBassador;
|
|
26
|
+import com.dmdirc.events.AppErrorEvent;
|
|
27
|
+import com.dmdirc.logger.ErrorLevel;
|
|
28
|
+
|
|
29
|
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
30
|
+
|
|
31
|
+import java.util.concurrent.ExecutionException;
|
|
32
|
+import java.util.concurrent.Future;
|
|
33
|
+import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
34
|
+import java.util.function.BiConsumer;
|
|
35
|
+
|
|
36
|
+/**
|
|
37
|
+ * An scheduled executor service that takes logs failed executions either via eventbus errors or a
|
|
38
|
+ * custom error function.
|
|
39
|
+ */
|
|
40
|
+public class LoggingScheduledExecutorService extends ScheduledThreadPoolExecutor {
|
|
41
|
+
|
|
42
|
+ private final BiConsumer<Runnable, Throwable> afterExecute;
|
|
43
|
+
|
|
44
|
+ /**
|
|
45
|
+ * Creates a new instance of this executor service.
|
|
46
|
+ *
|
|
47
|
+ * @param coreSize The number of threads to keep in the pool, even if they are idle, unless
|
|
48
|
+ * {@code allowCoreThreadTimeOut} is set
|
|
49
|
+ * @param eventBus The event bus to raise errors on
|
|
50
|
+ * @param poolName The naming format to use when naming threads
|
|
51
|
+ */
|
|
52
|
+ public LoggingScheduledExecutorService(final int coreSize,
|
|
53
|
+ final DMDircMBassador eventBus, final String poolName) {
|
|
54
|
+ this(coreSize, (r, t) -> eventBus
|
|
55
|
+ .publishAsync(new AppErrorEvent(ErrorLevel.HIGH, t, t.getMessage(), "")), poolName);
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ /**
|
|
59
|
+ * Creates a new instance of this executor service.
|
|
60
|
+ *
|
|
61
|
+ * @param coreSize The number of threads to keep in the pool, even if they are idle,
|
|
62
|
+ * unless {@code allowCoreThreadTimeOut} is set
|
|
63
|
+ * @param afterExecute The function to call when an exception occurs
|
|
64
|
+ * @param poolName The naming format to use when naming threads
|
|
65
|
+ */
|
|
66
|
+ public LoggingScheduledExecutorService(final int coreSize,
|
|
67
|
+ final BiConsumer<Runnable, Throwable> afterExecute, final String poolName) {
|
|
68
|
+ super(coreSize, new ThreadFactoryBuilder().setNameFormat(poolName + "-%d").build());
|
|
69
|
+ this.afterExecute = afterExecute;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ @Override
|
|
73
|
+ protected void afterExecute(final Runnable r, final Throwable t) {
|
|
74
|
+ super.afterExecute(r, t);
|
|
75
|
+ if (t == null && r instanceof Future<?>) {
|
|
76
|
+ try {
|
|
77
|
+ ((Future<?>) r).get();
|
|
78
|
+ } catch (ExecutionException ex) {
|
|
79
|
+ afterExecute.accept(r, ex);
|
|
80
|
+ } catch (InterruptedException ex) {
|
|
81
|
+ //Ignore
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+ if (t != null) {
|
|
85
|
+ afterExecute.accept(r, t);
|
|
86
|
+ }
|
|
87
|
+ }
|
|
88
|
+}
|