📄 wrappersimpleapp.java
字号:
*-------------------------------------------------------------*/
/**
* Used to launch the application in a separate thread.
*/
public void run()
{
// Notify the start method that the thread has been started by the JVM.
synchronized( this )
{
m_mainStarted = true;
notifyAll();
}
Throwable t = null;
try
{
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: invoking main method" );
}
m_mainMethod.invoke( null, new Object[] { m_appArgs } );
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: main method completed" );
}
synchronized(this)
{
// Let the start() method know that the main method returned, in case it is
// still waiting.
m_mainComplete = true;
this.notifyAll();
}
return;
}
catch ( IllegalAccessException e )
{
t = e;
}
catch ( IllegalArgumentException e )
{
t = e;
}
catch ( InvocationTargetException e )
{
t = e.getTargetException();
if ( t == null )
{
t = e;
}
}
// If we get here, then an error was thrown. If this happened quickly
// enough, the start method should be allowed to shut things down.
System.out.println();
System.out.println( "WrapperSimpleApp: Encountered an error running main: " + t );
// We should print a stack trace here, because in the case of an
// InvocationTargetException, the user needs to know what exception
// their app threw.
t.printStackTrace();
synchronized(this)
{
if ( m_startComplete )
{
// Shut down here.
WrapperManager.stop( 1 );
return; // Will not get here.
}
else
{
// Let start method handle shutdown.
m_mainComplete = true;
m_mainExitCode = new Integer( 1 );
this.notifyAll();
return;
}
}
}
/*---------------------------------------------------------------
* WrapperListener Methods
*-------------------------------------------------------------*/
/**
* The start method is called when the WrapperManager is signalled by the
* native wrapper code that it can start its application. This
* method call is expected to return, so a new thread should be launched
* if necessary.
* If there are any problems, then an Integer should be returned, set to
* the desired exit code. If the application should continue,
* return null.
*/
public Integer start( String[] args )
{
// Decide whether or not to wait for the start main method to complete before returning.
boolean waitForStartMain = WrapperSystemPropertyUtil.getBooleanProperty(
WrapperSimpleApp.class.getName() + ".waitForStartMain", false );
int maxStartMainWait = WrapperSystemPropertyUtil.getIntProperty(
WrapperSimpleApp.class.getName() + ".maxStartMainWait", 2 );
maxStartMainWait = Math.max( 1, maxStartMainWait );
// Decide the maximum number of times to loop waiting for the main start method.
int maxLoops;
if ( waitForStartMain )
{
maxLoops = Integer.MAX_VALUE;
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: start(args) Will wait indefinitely "
+ "for the main method to complete." );
}
}
else
{
maxLoops = maxStartMainWait; // 1s loops.
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: start(args) Will wait up to " + maxLoops
+ " seconds for the main method to complete." );
}
}
Thread mainThread = new Thread( this, "WrapperSimpleAppMain" );
synchronized(this)
{
m_appArgs = args;
mainThread.start();
// To avoid problems with the main thread starting slowly on heavily loaded systems,
// do not continue until the thread has actually started.
while ( !m_mainStarted )
{
try
{
this.wait( 1000 );
}
catch ( InterruptedException e )
{
// Continue.
}
}
// Wait for startup main method to complete.
int loops = 0;
while ( ( loops < maxLoops ) && ( !m_mainComplete ) )
{
try
{
this.wait( 1000 );
}
catch ( InterruptedException e )
{
// Continue.
}
if ( !m_mainComplete )
{
// If maxLoops is large then this could take a while. Notify the
// WrapperManager that we are still starting so it doesn't give up.
WrapperManager.signalStarting( 5000 );
}
loops++;
}
// Always set the flag stating that the start method completed. This is needed
// so the run method can decide whether or not it needs to be responsible for
// shutting down the JVM in the event of an exception thrown by the start main
// method.
m_startComplete = true;
// The main exit code will be null unless an error was thrown by the start
// main method.
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: start(args) end. Main Completed="
+ m_mainComplete + ", exitCode=" + m_mainExitCode );
}
return m_mainExitCode;
}
}
/**
* Called when the application is shutting down.
*/
public int stop( int exitCode )
{
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: stop(" + exitCode + ")" );
}
// Normally an application will be asked to shutdown here. Standard Java applications do
// not have shutdown hooks, so do nothing here. It will be as if the user hit CTRL-C to
// kill the application.
return exitCode;
}
/**
* Called whenever the native wrapper code traps a system control signal
* against the Java process. It is up to the callback to take any actions
* necessary. Possible values are: WrapperManager.WRAPPER_CTRL_C_EVENT,
* WRAPPER_CTRL_CLOSE_EVENT, WRAPPER_CTRL_LOGOFF_EVENT, or
* WRAPPER_CTRL_SHUTDOWN_EVENT
*/
public void controlEvent( int event )
{
if ( ( event == WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT )
&& WrapperManager.isLaunchedAsService() )
{
// Ignore
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: controlEvent(" + event + ") Ignored" );
}
}
else
{
if ( WrapperManager.isDebugEnabled() )
{
System.out.println( "WrapperSimpleApp: controlEvent(" + event + ") Stopping" );
}
WrapperManager.stop( 0 );
// Will not get here.
}
}
/*---------------------------------------------------------------
* Methods
*-------------------------------------------------------------*/
/**
* Displays application usage
*/
protected void showUsage()
{
System.out.println();
System.out.println(
"WrapperSimpleApp Usage:" );
System.out.println(
" java org.tanukisoftware.wrapper.WrapperSimpleApp {app_class} [app_parameters]" );
System.out.println();
System.out.println(
"Where:" );
System.out.println(
" app_class: The fully qualified class name of the application to run." );
System.out.println(
" app_parameters: The parameters that would normally be passed to the" );
System.out.println(
" application." );
}
/*---------------------------------------------------------------
* Main Method
*-------------------------------------------------------------*/
/**
* Used to Wrapper enable a standard Java application. This main
* expects the first argument to be the class name of the application
* to launch. All remaining arguments will be wrapped into a new
* argument list and passed to the main method of the specified
* application.
*/
public static void main( String args[] )
{
new WrapperSimpleApp( args );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -