This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved ThreadPollExecutorLogged to shared
I need it for the wrapper
- Loading branch information
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/cascadebot/shared/utils/ThreadPoolExecutorLogged.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2018 CascadeBot. All rights reserved. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package com.cascadebot.shared.utils; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.SynchronousQueue; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ThreadPoolExecutorLogged extends ThreadPoolExecutor { | ||
|
||
private Logger logger; | ||
public ThreadPoolExecutorLogged(int corePoolSize, int maximumPoolSize, long keepAliveTime, | ||
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory factory, Logger logger) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, factory); | ||
this.logger = logger; | ||
} | ||
|
||
@Override | ||
public void afterExecute(Runnable r, Throwable t) { | ||
super.afterExecute(r, t); | ||
if (t == null && r instanceof Future<?>) { | ||
try { | ||
Object result = ((Future<?>) r).get(); | ||
} catch (CancellationException e) { | ||
t = e; | ||
} catch (ExecutionException e) { | ||
t = e.getCause(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
if (t != null) { | ||
logger.error("Unhandled exception in thread: " + Thread.currentThread().getName() + "!", t); | ||
} | ||
} | ||
|
||
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory, Logger logger) { | ||
return new ThreadPoolExecutorLogged(0, Integer.MAX_VALUE, | ||
60L, TimeUnit.SECONDS, | ||
new SynchronousQueue<>(), | ||
threadFactory, logger); | ||
} | ||
|
||
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory, Logger logger) { | ||
return new ThreadPoolExecutorLogged(nThreads, nThreads, | ||
0L, TimeUnit.MILLISECONDS, | ||
new LinkedBlockingQueue<>(), | ||
threadFactory, logger); | ||
} | ||
|
||
} |