In a current project, I am attempting to load a set of plugins; The problem is that plugin could do anything, include throwing exceptions, or just hanging forever. I am trying to accomplish the following:
try {
// load class by package name
Class<?> pluginClass = Class.forName(plginClassName);
// create a new instance of the object
// this call could throw an exception or never return
GenericPlugin plugin = (GenericPlugin) pluginClass.newInstance();
state = PluginState.INITIALIZED;
...
} catch ( InterruptedException ie ) { // <-- Compile Error Here
// Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body
state = PluginState.TIMEOUT;
} catch ( Exception ex ) {
state = PluginState.FAILED;
}
I would like it such that in my calling class of the loader thread I can do:
LoaderThread t = new LoaderThread( pluginClassName );
// start loading the plugin
t.start();
// wait 5 seconds for startup
t.join( 5000 );
// interrupt if not complete
t.interrupt();
// get my plugin state
pluginState = t.getPluginState();
If i am going at this wrong, please advise, but
How can I timeout a call to
class.newInstance()
?