Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#268 start called twice

v3.3.9
open
5
2014-07-13
2009-12-10
Anonymous
No

I'm traying to create a windows service. I have to code some logic so I choose WrapperListener. But when I started the start method is called twice. Do you have more detailed code for doing something like this? Here my code:

public class Robot implements WrapperListener{
private Scheduler scheduler;
java.util.Timer timer;
Logger root = Logger.getRootLogger();

private Robot\(\)
\{

\}
public static void main\(java.lang.String\[\] args\)
\{
    Logger root = Logger.getRootLogger\(\);
    root.addAppender\(new ConsoleAppender\(new PatternLayout\("%-5p \[%t\] %c\{1\}: %m%n"\)\)\);
    root.info\("en main"\);

// Start the application. If the JVM was launched from the native
// Wrapper then the application will wait for the native Wrapper to
// call the application's start method. Otherwise the start method
// will be called immediately.
WrapperManager.start( new Robot(), args );

\}
public void controlEvent\(int event\)
\{

if ( ( event == WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT )
&& ( WrapperManager.isLaunchedAsService() || WrapperManager.isIgnoreUserLogoffs() ) )
{
// Ignore
}
else
{
WrapperManager.stop( 0 );
// Will not get here.
}
}
public int stop(int exitCode)
{
scheduler.stopService();

    return exitCode;
\}
public java.lang.Integer start\(java.lang.String\[\] args\)
\{
    synchronized\(root\)
    \{
        root.addAppender\(new ConsoleAppender\(new PatternLayout\("%-5p \[%t\] %c\{1\}: %m%n"\)\)\);
        root.info\("Start now" + \(\(args \!= null && args.length > 0\) ? args\[0\] : ""\)\);

        scheduler = new Scheduler\(\);
        //WrapperManager.signalStarting\( 30000 \);
        scheduler.start\(\);
        timer = new java.util.Timer\(\);
        timer.scheduleAtFixedRate\(scheduler, 0, 10000\);
    \}

    return null;
\}
public void run\(\)
\{

\}

}

and the running log

STATUS | wrapper | 2009/12/10 01:23:00 | --> Wrapper Started as Console
STATUS | wrapper | 2009/12/10 01:23:00 | Java Service Wrapper Community Edition 32-bit 3.3.9
STATUS | wrapper | 2009/12/10 01:23:00 | Copyright (C) 1999-2009 Tanuki Software, Ltd. All Rights Reserved.
STATUS | wrapper | 2009/12/10 01:23:00 | http://wrapper.tanukisoftware.org
STATUS | wrapper | 2009/12/10 01:23:00 |
STATUS | wrapper | 2009/12/10 01:23:00 | Launching a JVM...
INFO | jvm 1 | 2009/12/10 01:23:00 | INFO [main] root: en main
INFO | jvm 1 | 2009/12/10 01:23:00 | WrapperManager: Initializing...
INFO | jvm 1 | 2009/12/10 01:23:01 | INFO [WrapperListener_start_runner] root: Start now
INFO | jvm 1 | 2009/12/10 01:23:01 | INFO [WrapperListener_start_runner] root: Start now
INFO | jvm 1 | 2009/12/10 01:23:01 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:01 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:01 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:11 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:11 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:11 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:11 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:20 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:20 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:20 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:20 | INFO [Timer-0] root: run con timer
INFO | jvm 1 | 2009/12/10 01:23:20 | INFO [Timer-0] root: run con timer
STATUS | wrapper | 2009/12/10 01:23:26 | CTRL-C trapped. Shutting down.
STATUS | wrapper | 2009/12/10 01:23:26 | <-- Wrapper Stopped

the line INFO | jvm 1 | 2009/12/10 01:23:01 | INFO [Timer-0] root: run con timer appears 3 times just at startup as is called 3 times then waits the period I set and again 4 calls. Here is the other class code:

public class Scheduler extends TimerTask {
public void start()
{

\}
public void stopService\(\)
\{

\}
public void run\(\)
\{
    Logger root = Logger.getRootLogger\(\);
    root.addAppender\(new ConsoleAppender\(new PatternLayout\("%-5p \[%t\] %c\{1\}: %m%n"\)\)\);
    root.info\("run con timer"\);
\}

}

Thanks.

Discussion

  • Leif Mortenson
    Leif Mortenson
    2009-12-10

    • milestone: --> v3.3.9
    • assigned_to: nobody --> mortenson
     
  • Leif Mortenson
    Leif Mortenson
    2009-12-10

    The code you wrote looks correct. My first guess is that you are using one of the Wrapper's helper classes to launch your code. When you implement your own WrapperListener, you will be using Integration Method #3.
    http://wrapper.tanukisoftware.org/doc/english/integrate.html#method3

    If you were to use Method #1 for example, then the WrapperSimpleApp helper class would initialize the Wrapper once before launching your application. I will run some tests on our end as well.

    Please set the wrapper.debug=true property and attache the wrapper.log and wrapper.conf files to this issue.

    You can attache files by clicking on the text "Add a file" below.
    Cheers,
    Leif

     
  • Leif Mortenson
    Leif Mortenson
    2009-12-10

    I ran some tests calling a WrapperListener from a WrapperSimpleApp helper class as I described, and the Wrapper actually handles that case correctly by reporting the following error:

    ---
    WrapperSimpleApp:
    WrapperSimpleApp: Encountered an error running main:
    WrapperSimpleApp: java.lang.IllegalStateException: WrapperManager has already been started with a WrapperListener.
    WrapperSimpleApp: at org.tanukisoftware.wrapper.WrapperManager.privilegedStart(WrapperManager.java:2374)
    WrapperSimpleApp: at org.tanukisoftware.wrapper.WrapperManager.access$2500(WrapperManager.java:96)
    WrapperSimpleApp: at org.tanukisoftware.wrapper.WrapperManager$5.run(WrapperManager.java:2318)
    WrapperSimpleApp: at java.security.AccessController.doPrivileged(Native Method)
    WrapperSimpleApp: at org.tanukisoftware.wrapper.WrapperManager.start(WrapperManager.java:2315)
    WrapperSimpleApp: at org.tanukisoftware.wrapper.test.NestedWrapperListener.main(NestedWrapperListener.java:66)
    WrapperSimpleApp: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    WrapperSimpleApp: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    WrapperSimpleApp: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    WrapperSimpleApp: at java.lang.reflect.Method.invoke(Method.java:597)
    WrapperSimpleApp: at org.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:244)
    WrapperSimpleApp: at java.lang.Thread.run(Thread.java:619)
    ---

    I am not seeing the problem when reviewing the code. The test that I created here works as expected.

    I should be able to see the problem by looking at your wrapper.conf and wrapper.log with debug output enabled.

    Cheers,
    Leif

     
  • Leif Mortenson
    Leif Mortenson
    2009-12-10

    Oh!. I see the problem. If you look carefully, you have added two ConsoleAppenders to the Root logger. One in the main method, and then again in the start method.

    Because of this, when you log something, anything, you will get two lines of output.

    I have not tested this, but it looks like the problem. Try removing the addAppender method from the start method.

    Let me know how this works for you.

    Cheers,
    Leif