|
From: Leif M. <le...@ta...> - 2004-11-25 04:37:55
|
Christophe,
The problem here is that your WrapperListener.start method is not
returning.
It is described in the javadocs, but that method must return. The
Wrapper is timing
out waiting for it.
From your source code, I don't see anything that you are doing that
warrants you
going to the extra work of using integration method #3. I would
suggest using method
#1. It will work out of the box and requires no special coding.
If you do want to keep using this integration method, you should be
able to simply
remove the loop in your start method. My guess is that your
application was quitting
because "All non-daemon threads had completed." You can fix that
original problem
by making it your mailing thread into a non-daemon. Try the following:
private Mailing m_mailing;
public Integer start(String[] arg0) {
m_mailing = new Mailing();
m_mailing.start();
return null;
}
public int stop(int exitCode) {
m_mailing.stop();
return exitCode;
}
I changed your Mailing class from a Thread(??) to an object that can be
started and
stopped. Then made stop method stop your class cleanly. Internally,
the Mailing.start()
method should create a runner thread that is NOT A DAEMON. Then start
it. It should
look something like the following. This is based on working code that
I took from one
of my base classes.
public class Mailing implements Runnable {
private Thread m_runner;
private boolean m_runnerStop = false;
public void start() throws Exception {
m_runner = new Thread( this, "Mailing_runner" );
m_runner.start();
}
public void stop() throws Exception {
Thread runner = m_runner;
m_runnerStop = true;
if ( runner != null ) {
runner.interrupt();
}
// Give the user code a change to stop cleanly.
try {
stopRunner();
} catch ( Throwable t ) {
System.out.println( "Encountered a problem while stopping
the component." );
t.printStackTrace();
}
// Waiting for runner thread to stop.
synchronized ( this ) {
while ( m_runner != null ) {
try {
// Wait to be notified that the thread has exited.
this.wait();
} catch ( InterruptedException e ) {
// Ignore
}
}
}
}
public void run() {
if ( Thread.currentThread() != m_runner ) {
throw new IllegalStateException( "Private method." );
}
try {
try {
runner();
} catch ( Throwable t ) {
System.out.println( "The runner method threw an uncaught
exception, runner is terminating," );
t.printStackTrace();
}
} finally {
synchronized ( this ) {
m_runner = null;
// Wake up the stop method if it is waiting for the
runner to stop.
this.notify();
}
}
}
protected void stopRunner() throws Exception
{
// Do anything you need to do to stop cleanly.
}
protected void runner() {
while ( !isStopping() ) {
try {
// ******
// Your user code would go here for each cycle of the
main loop.
// ******
} catch ( Throwable t ) {
System.out.println( "Unexpected error in runner." );
t.printStackTrace();
// Avoid thrashing.
if ( !isStopping() ) {
try {
Thread.sleep( 5000 );
} catch ( InterruptedException e ) {
// Ignore
}
}
}
}
}
public boolean isStopping() {
return m_runnerStop;
}
}
Your code also did this:
catch(Exception e){
System.exit(1);
}
That is BAD BAD BAD. You will will regret that programming style at
some point as your
application will just suddenly exit for no reason. ALWAYS do something
with a caught
exception. If you "know" it will never happen anyway then it will no
hurt to print the
stack trace.
Cheers,
Leif
Christophe Leroux wrote:
> Dear all,
>
> First of all, I would like to say that sourceforge is a very huge
> knowledge source for me. I like your work and hope your success will
> continue growing.
>
> Ohterwise, I'm really disapointed about the way to execute my app as a
> service with the wrapper.
>
> Here is the content of my wrapper.conf :
>
> Starting the SendLogByMail service...
> --> Wrapper Started as Service
> Using system timer.
> server listening on port 32000.
> Launching a JVM...
> command: "C:\J2EE\SDK1.4\jdk\bin\java.exe"
> -Djava.library.path="classes/lib" -classpath "classes/"
> -Dwrapper.key="Rfv8sZ_PcoRLht4W" -Dwrapper.port=32000
> -Dwrapper.debug="TRUE" -Dwrapper.use_system_time="TRUE"
> -Dwrapper.version="3.1.2" -Dwrapper.native_library="wrapper"
> -Dwrapper.service="TRUE" -Dwrapper.cpu.timeout="10" -Dwrapper.jvmid=1
> com.cleroux.Main
> JVM started (PID=3392)
> WrapperManager class initialized by thread: main Using classloader:
> sun.misc.Launcher$AppClassLoader@e80a59
> Wrapper Manager: JVM #1
> Wrapper Manager: Registering shutdown hook
> Wrapper Manager: Using wrapper
> Loaded native library: wrapper.dll
> Calling native initialization method.
> Initializing WrapperManager native library.
> Java Executable: C:\J2EE\SDK1.4\jdk\bin\java.exe
> Windows version: 5.1.2600
> Java Version : 1.4.2_04-b04 Java HotSpot(TM) Client VM
> Java VM Vendor : Sun Microsystems Inc.
>
> Wrapper (Version 3.1.2) http://wrapper.tanukisoftware.org
>
> WrapperManager.start(com.cleroux.Main@14b7453, args[]) called by
> thread: main
> Open socket to wrapper...
> Opened Socket
> Send a packet KEY : Rfv8sZ_PcoRLht4W
> handleSocket(Socket[addr=/127.0.0.1,port=32000,localport=2313])
> accepted a socket from 127.0.0.1 on port 2313
> read a packet KEY : Rfv8sZ_PcoRLht4W
> Got key from JVM: Rfv8sZ_PcoRLht4W
> send a packet LOW_LOG_LEVEL : 1
> send a packet PING_TIMEOUT : 30
> Start Application.
> send a packet START : start
> Received a packet LOW_LOG_LEVEL : 1
> Wrapper Manager: LowLogLevel from Wrapper is 1
> Received a packet PING_TIMEOUT : 30
> Wrapper Manager: PingTimeout from Wrapper is 30000
> Received a packet START : start
> calling listener.start()
> Waiting to start...
> Waiting to start...
> Waiting to start...
> Waiting to start...
> Waiting to start...
> Startup failed: Timed out waiting for signal from JVM.
>
> As you can see, the wrapper try 5 times to launch the service
> unsuccessfully.
>
> I really ask my self why it does not !
>
> If any one had an idea, I would be really happy !
>
> Here is an extract of my main class :
>
> public class Main implements WrapperListener{
>
> public Integer start(String[] arg0) {
> boolean flag=true;
>
> try{
> Mailing mailing = new Mailing();
> mailing.setDaemon(true);
> mailing.start();
> while (mailing.isAlive()){
> System.out.println("Thread encore vivant !");
> Thread.sleep(6000);
> }
> }
> catch(Exception e){
> System.exit(1);
> }
> return null;
> }
>
> public static void main(String[] args) {
> WrapperManager.start(new Main(), args);
> }
> }
>
> Thanks for your help !
>
> Bye,
>
> Christophe (please excuse me for the quality of my english !)
|