Reputation: 1101
We are working on a project to handle virtual machine managers from a Java application.
At the moment, we faced a problem where some instructions must be considered as a task because they require more time. Theses taks can also end with errors or be a success, and we can't know the result from the application unless we request the state of the task to the VM hypervisor.
To have a smooth application we wanted to have a CommandManager to handle the differents request to theses hypervisors in separated threads. The problem is that these Command might return specific errors such as that we used to catch in the view to display revelant information to the user but when we implement Runnable
on our Commands interface, we can't throw any exception out of that thread back to reach the view.
What could I do to keep notifying the user of the errors that happened and their nature?
Sorry for my english!
Lets see the code below for a brief example :
First the Command
class ChangeMemorySize extends Command {
private String name;
private int memorySizeMb;
public ChangeMemorySize(Hypervisor hypervisor, String server,
String name, int memory) {
super(hypervisor, server);
this.name = name;
this.memorySizeMb = memory;
}
protected void execute() throws ConnectionException,OperationException{
//Some code here
}
public void run() //CANT THROW ANYTHING HERE :throws VmConfigFault,
try{
execute();
}catch{Exception e){
// I have to catch it all here!
}
This isn't real code this is just an example. This command would then be passed to a manager who would run it in another thread. But I loose all the specifics exception that I used to notify the user of what really happened in there!
Upvotes: 1
Views: 638
Reputation: 15729
While I agree with @John Vint that ExecutorService with an UncaughtExceptionHandler is a good approach, if for some reason you really want to stick with Runnables, I'd suggest that your base class, Command, have some fields/getters for status and Exception. e.g.
Enum Status {FAILED, CANCELLED, SUCCESS};
// protected so subclasses can set them
protected Status status;
protected Exception exception; // null means none
public Status getStatus();
public Exception getException();
You might even want to add a result field to really mimic a Callable.
For simplicity, then add a method that subclasses can call in the catch clause
protected void s***Happenned(Exception e) {
this.exception = e;
status = FAILED;
// any standard cleanup can go here too...
}
Upvotes: 1
Reputation: 40256
Use an ExecutorService and do one of two things.
First you can store all results in a Future and when you want to know if an Exception occurs just invoke Future.get() and handle any exception.
Second you can create the ExecutorService with a ThreadFactory in which you set it a knew UncaughtExceptionHandler like
final UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
// notify error
}
});
Executor executor = Executors.newFixedThreadPool(1, new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setUncaughtExceptionHandler(handler);
return thread;
}
});
Upvotes: 2