BAEL-4217: Added Thread Pool Aware MDC (#9554)
* Added Thread Pool Aware MDC * Fixed Thread Pool
This commit is contained in:
parent
2e95961d1b
commit
4671770740
|
@ -1,19 +1,21 @@
|
||||||
package com.baeldung.mdc;
|
package com.baeldung.mdc;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import com.baeldung.mdc.pool.MdcAwareThreadPoolExecutor;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
|
|
||||||
import com.baeldung.mdc.log4j.Log4JRunnable;
|
|
||||||
import com.baeldung.mdc.log4j2.Log4J2Runnable;
|
|
||||||
import com.baeldung.mdc.slf4j.Slf4jRunnable;
|
import com.baeldung.mdc.slf4j.Slf4jRunnable;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
|
||||||
|
|
||||||
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
|
|
||||||
public class TransferDemo {
|
public class TransferDemo {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(3);
|
ExecutorService executor = new MdcAwareThreadPoolExecutor(3, 3, 0, MINUTES,
|
||||||
|
new LinkedBlockingQueue<>(), Thread::new, new AbortPolicy());
|
||||||
|
|
||||||
TransactionFactory transactionFactory = new TransactionFactory();
|
TransactionFactory transactionFactory = new TransactionFactory();
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.mdc.pool;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.ThreadContext;
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.RejectedExecutionHandler;
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class MdcAwareThreadPoolExecutor extends ThreadPoolExecutor {
|
||||||
|
|
||||||
|
public MdcAwareThreadPoolExecutor(int corePoolSize,
|
||||||
|
int maximumPoolSize,
|
||||||
|
long keepAliveTime,
|
||||||
|
TimeUnit unit,
|
||||||
|
BlockingQueue<Runnable> workQueue,
|
||||||
|
ThreadFactory threadFactory,
|
||||||
|
RejectedExecutionHandler handler) {
|
||||||
|
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void afterExecute(Runnable r, Throwable t) {
|
||||||
|
System.out.println("Cleaning the MDC context");
|
||||||
|
MDC.clear();
|
||||||
|
org.apache.log4j.MDC.clear();
|
||||||
|
ThreadContext.clearAll();
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,7 +18,7 @@ public class Slf4jRunnable implements Runnable {
|
||||||
|
|
||||||
new Slf4TransferService().transfer(tx.getAmount());
|
new Slf4TransferService().transfer(tx.getAmount());
|
||||||
|
|
||||||
MDC.clear();
|
// MDC.clear(); We don't need this with MdcAwareThreadPoolExecutor
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue