Monday, February 2, 2009

How to - Detect system availability on startup


The best way to ensure that the system is available and ready is to leverage the ApplicationManager class with its inStartup method along with using a system listener and its powerUp method. Sample code is included below as an example on how you can detect when the system is available and ready.
Code:
public static void main( String[] args ) {
// Perform additional work as part of main method if necessary.
if( ApplicationManager.getApplicationManager().inStartup() ) {
// Add a system listener to detect when system is ready and available.
applicationInstance.addSystemListener( applicationInstance );
} else {
// System is already ready and available so perform your start up work
now.
// Note that this work must be completed using invokeLater because the
// application has not yet entered the event dispatcher.
applicationInstance.doStartupWorkLater();
}
// Enter the event dispatcher.
applicationInstance.enterEventDispatcher();
}
/**
* Implementation of the powerUp method for the SystemListener interface
*
*/
public void powerUp()
{
applicationInstance.removeSystemListener( this );
doStartupWork();
}
/**
* Perform the start up work on a new Runnable using the
* invokeLater construct to ensure that it is executed
* after the event thread has been created.
*
*/
private void doStartupWorkLater()
{
applicationInstance.invokeLater( new Runnable() {
public void run() {
doStartupWork();
}
} );
}
/**
* Your private method for performing your startup work.
*
*/
private void doStartupWork()
{
// Perform your start up activities here!
}

No comments:

Post a Comment

Place your comments here...