@OnStart/OnStop vs ModuleInstall and also force exit during starup
Jean-Marc Borer <[email protected]>
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <CADAUzbnC-aUiTNMrTVLMbLhYZ1U4ig8nE+08fdKvKCCA1C7y_Q@mail.gmail.com> |
Hello guys,
I have several questions today and need you opinion.
Firstable, I want to initialize and create an instance at the startup that
will run in its own thread. During shutdown it must be stopped properly.
Secondly, if an exception occurs during initialization, the application
must exit silently.
With ModuleInstall it would look like:
public class Installer extends ModuleInstall {
private static final Logger LOGGER = LogUtils.getLogger();
private MyContainer container;
private Thread containerThread;
@Override
public void restored() {
try {
File f = new File("some path");
container = new NbNimbusContainer(f, "some title", 15555, 0);
containerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
container.run();
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Exception during
container run. Exiting...", ex);
LifecycleManager.getDefault().exit(-1);
}
}
});
containerThread.start();
} catch (Exception ex) {
// must be posted on another thread otherwise deadlock
Runnable r = new Runnable() {
@Override
public void run() {
LifecycleManager.getDefault().exit(-1);
}
};
SwingUtilities.invokeLater(r);
}
}
@Override
public void close() {
if (container != null) {
container.requestShutdown();
}
// wait for container to exit
if (containerThread != null) {
try {
containerThread.join(30000);
} catch (InterruptedException ex) {
LOGGER.log(Level.WARNING, "Container join interrupted.",
ex);
}
}
}
}
This works. Now I would like to replace Module install with OnStart/OnStop.
The problem is that these annotations only work on classes implementing
Runnable and empty constructor.
I need to share the container and containerThread variables. Unfortunately
you cannot annotate non-static internal classes. I could use static
variables, but this is not very nice.
Another option would be to create a Starter and Stopper service
implementing Runnable, which would call Runnable instances you cou have
submitted to them in your own OnStart. The problem is that OnStart is
multithreaded and you would have no garantie of the invocation order...
Finally one could look for the thread by name and then stop it. Not sure it
is nice.
So the question is: how would you solve this problem with OnStart OnStop?
Cheers,
JMB