serick
Posts: 7
Joined: Wed Feb 14, 2007 9:19 pm
Location: Boston
Contact: Website

Java Service stops after 30 seconds

Hi,

I have successfully created an installer for my Java application. I tested it first as a console app. That worked fine. Then I changed it over to a service, following all of the steps in the tutorial. (Including creating a "New Service Installation" and "New Control Operation"). I installed this, but, when I start up the service, it will run for a bit - say 20-30 seconds, and then it will stop.

I have stderr and stdout being directed to files, but I don't see any error messages in them.

Any suggestions? Just as a note, I have successfully installed this program as a service using the JavaService.exe program, but I would like to use your installer to do it since that requires a batch file.

Regards,

Sean
serick
Posts: 7
Joined: Wed Feb 14, 2007 9:19 pm
Location: Boston
Contact: Website

fixed...

The problem was that my main method exits (after starting some other threads). Apparently this isn't supported? Anyhow, I put an infinite loop in the thread to it persists, and now the service works.

I did it just like this thread:
http://www.advancedinstaller.com/forums ... va+service
(which I just found ...)

Sean
Ferrograph
Posts: 3
Joined: Tue Jan 29, 2013 11:44 am

Re: Java Service stops after 30 seconds

Using an infinite while is ok, but not a great idea as it could consume a lot of CPU time, I'd be surprised if it doesn't.

Here is the method I used below. And a side note is that if you dont put any System.out.print in your main for some reason the AI service.exe wrapper thinks your service stopped - took me a while to figure that out and is probably an AI bug.

Code: Select all

public class MyJavaService
{
    static boolean running = true;
    static MyApp app = null;
    static Object sync = null;
    
    public static void main(String[] args)  throws Exception
    {		
        System.out.println("JavaService Start.");
        
        sync = new Object();
        app  = new MyApp();
        app.start();
		
        synchronized(sync)
        {
            while(running)              
            {
                System.out.println("JavaService  Running.");
                sync.wait();
            }
        }
		
        System.out.println("JavaService  Stopped.");
    }

    public static void stop()
    {
        System.out.println("JavaService  Stopping...");
        
        running = false;
        app.shutdown();
	    
        synchronized(sync) sync.notify();        	    
    }
}
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Java Service stops after 30 seconds

Hello,

I'm not sure why you are encountering this behavior. Can you please run your java main class (without your service) from command line and try to debug it? Also, you can try to add a sleep(1000) or a sleep(10000) method call in your while loop and see if the behavior still persist.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Ferrograph
Posts: 3
Joined: Tue Jan 29, 2013 11:44 am

Re: Java Service stops after 30 seconds

Tried all of that stuff in many combinations and unless the following conditions are met the wrapper reports your service as "started then stopped":

# Main() must not exit until after stop() is called.
# You must do at least one System.out.print during main()

As with the majority of the AI documentation the first one is not discussed properly (poor example in the tutorial) and the second isnt mentioned at all.
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Java Service stops after 30 seconds

Hello,
# You must do at least one System.out.print during main()
We have tested your scenario using version 9.8 of Advanced Installer, but we cannot reproduce the behavior. Can you reproduce the behavior on multiple clean machines? Also, can you tell us what version of Advanced Installer you are using?

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Ferrograph
Posts: 3
Joined: Tue Jan 29, 2013 11:44 am

Re: Java Service stops after 30 seconds

Im strapped for time currently, and since my service is now working...

If I get chance I will put together a clean project to test it, and report back here. Thanks for being so responsive.

The version of AI was 9.8.

Return to “Common Problems”