Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Moved ThreadPollExecutorLogged to shared
Browse files Browse the repository at this point in the history
I need it for the wrapper
  • Loading branch information
weeryan17 committed Jan 10, 2019
1 parent f85f1ba commit aafc0e6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.cascadebot</groupId>
<artifactId>shared</artifactId>
<version>1.8</version>
<version>1.8.1</version>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
Expand All @@ -24,5 +24,13 @@
</plugins>
</build>

<dependencies>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>

</project>
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);
}

}

0 comments on commit aafc0e6

Please sign in to comment.