Biff

Java Service lifecycle

Hi,

I'm having a problem understanding how the lifecycle works for running a java application as a service. I have a java program that works fine as a console application, but fails when installed as a service as it shuts down immediately after completing initialisation.

The issue seems to be that the main method thread sets up a whole set of new working threads and then lets itself die, which in turn seems to indicate to the service control panel that the application has died, so the JVM is killed and the server stops.

I have worked around this by putting a Thread.sleep(10000000); call in the initialisation thread which means the server stays up, can deal with requests and generally works fine. However in this case I can't stop the application via the service control panel as the request just times out and I get the 'did not respond in a timely fashion' error.

Love the product apart from this (and the folders in classpath issues i posted a coupl eof hours ago). Easy to use and surprisingly powerful.

cheers, Biff
Cata
Posts: 638
Joined: Thu Apr 10, 2003 7:37 am
Contact: Website

Hi Biff,

Thanks for the prize. Mind if I quote you on our Testimonials page?

A service is stopped by a call to its stop() method. In your main you should have a loop that check a "stopped" variable. In your stop() method, just put the variable to true. The loops sees that and exits.

Of course, the same thing should be done for ALL the threads, not only the main one. The main one should be the last one to exit though.

Check out Java Service Installation Tutorial in our online User Guide.

Cata
Catalin Rotaru - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Biff

Thanks for that. Didn't find the tutorial beforehand and now all is progressing much better!

Still having problems with one thing, though. I need to be able to pass startup arguments to the java main method. I assume this is done with the 'Arguments' field in the 'Service Context' field of the dialog box.

If I use this then I can set the arguments up correctly and they appear in the 'path to executable' field of the service properties once it is installed. However they do not seem to be passed onto the main method of my java program. No matter what I do, the only argument passed seems to be the name of the service.

Is this correct behaviour or have I missd something?

cheers, Biff
Cata
Posts: 638
Joined: Thu Apr 10, 2003 7:37 am
Contact: Website

This works here, I just tested it. Maybe there is a problem in your setup?

Cata
Catalin Rotaru - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Guest

Cata,

Thanks for the reply, but I still can't get it to work. May I just clarify what I'm up to and perhaps you can spot the flaw.

I have created a Java class to run my service which looks like:

Code: Select all

...
	public static void main(String[] argv) throws Exception
	{
		PrintWriter pw = null;
		try
		{
			pw = new PrintWriter(new FileWriter(new File("C:\\outputfile.txt"),false));
			pw.println("Server starting with "+argv.length+" arguments");
			for (int i = 0; i < argv.length; i++)
			{
				pw.println("  Argument "+i+" is "+argv[i]);
			}
		}
		finally
		{
			pw.flush();
			pw.close();
		}

		while (s_running)
		{
...
		}
	}

	public static void stop()
	{
		if (s_running)
		{
			s_running = false;
		}
	}
...
I create the java service as described in the tutorial, except for the 'Arguments' field on the 'ServiceContext' section of the 'Service Properties' is set to [TARGETDIR]. This all seems to work fine as, once the application is installed, the property dialog for it states that it's 'Path to executable' is:
"c:\program files\biff\biffService.exe" c:\program files\biff\
Indicating that the argument is correct. However when I try and start the service the log file 'C:\outputfile.txt' as created by the java main method is contains just one argument of "Biff Service", which is the both the defined name and display name of the service. The parameter 'c:\program files\biff\' is not passed to the java main method at all.

Does this make any sense to you at all?

Many thanks for your help on this.

Biff
Cata
Posts: 638
Joined: Thu Apr 10, 2003 7:37 am
Contact: Website

Hi Biff,

An executable can contain more than one service. The arguments from ServiceInstall are for that executable.

To pass arguments to a service contained in an executable you must set arguments for the service control action defined for that service.

Cata
Catalin Rotaru - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Biff

Cata - brilliant. I understand what the system is doing now (it doesn't really help me solve the problem but it's a start!).

As I understand it the arguments in the ServiceContext box when installing services are purely for the wrapper executable as provided by AdvancedInstaller. If I want to send arguments to the Java program itself, I need to define a Control Operation or manually specify a Start parameter in the box provided in the service control manager properties dialog.

Unfortunately neither of there solutions fixes my problem. I need to install a java service (as defined in the tutorial) and provide a parameter every time it starts as it will be manually stopped and started periodically. I do not want to have to explicitly type in the argument into the Start parameter text field every time the service is to be executed.

Is there any way any way of doing this? Perhaps by defining the argument in the ini file generated by the 'Java Productst' section?

cheers, Biff
Cata
Posts: 638
Joined: Thu Apr 10, 2003 7:37 am
Contact: Website

Got it.

Here's what happens: the service EXE gets two sets of arguments:
- the exe command line args
- the service main function args

Currently we discard the former and pass the later. We will change so that we append the latter to the former and pass both. This fix will be in the next AI version, 2.4.

In the meantime, I can only suggest changing the application so that the parameter is read from a registry entry...

Regards,
Cata
Catalin Rotaru - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”