Introduction
This is a continuation to the post on exception handling on threads. For those interested in the background, please visit this link
This post discusses on the topic "Can I use separate exception handlers for threads performing mission critical tasks vs non trivial tasks?"
Per-Thread Exception Handler
Let's us assume that we built a handler that wants to notify the support team that something has gone wrong in some special thread processing, like the one below (not really notifying anyone but helps correlate the context)
class SupportNotificationExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
notifySupport(t, e);
}
private void notifySupport(Thread t, Throwable e) {
System.out.println("Notified support team on: " + e.getMessage() + " raised from : " + t.getName());
}
}
Let us now apply the above special handler to specific threads in the application. Here we are opting for support team to be notified for any exception in main thread and in one other specific thread.
Refer the snippet of code below,
public class ThreadSpecificHandler {
public static void main(String[] args) {
Thread.currentThread().setUncaughtExceptionHandler(new SupportNotificationExceptionHandler());
Thread.setDefaultUncaughtExceptionHandler(new ThreadExceptionHandler("DEFAULT"));
new Thread(new ExceptionLeakingTask()).start();
Thread t2 = new Thread(new ExceptionLeakingTask());
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
t2.setUncaughtExceptionHandler(new SupportNotificationExceptionHandler());
t2.start();
throw new ArithmeticException("whatever");
}
}
As given in the snippet above, we are using the SupportNotificationExceptionHandler for the main thread and 2nd thread (t2).
Whenever an exception is getting raised in the main and t2, they are notified to the support team, remaining ones are all using the default handler to log to the console and continue.
This illustrates how we can build the handlers specific for the collection of threads based on the business critical threading tasks vs non trivial threading tasks
Below given is the snippet where we have a single handler to be used in many threads
public static void main(String[] args) {
SupportNotificationExceptionHandler supportNotificationExceptionHandler = new SupportNotificationExceptionHandler();
Thread.currentThread().setUncaughtExceptionHandler(supportNotificationExceptionHandler);
Thread.setDefaultUncaughtExceptionHandler(new ThreadExceptionHandler("DEFAULT"));
new Thread(new ExceptionLeakingTask()).start();
Thread t2 = new Thread(new ExceptionLeakingTask());
new Thread(new ExceptionLeakingTask()).start();
new Thread(new ExceptionLeakingTask()).start();
t2.setUncaughtExceptionHandler(supportNotificationExceptionHandler);
t2.start();
throw new ArithmeticException("whatever");
}
When using a spring framework like Spring boot, we can have the handlers autowired as singletons, thereby eliminating the new instance creation in the code base.
Comments
Post a Comment