简体   繁体   中英

How to implement logic on stop and start of Java windows service wrapper YAJSW?

I'm leatning to work with YAJSW for wrapping java apps into windows services. It works fine with starting application but I want to know how to implement logic in the java application so that when I stop a service it would perform that logic inside the java application.

Something like implementing onStart() and onStop() methods in my java application so I can call these when I start or stop a java service.

You can call your onStart() method/logic right inside your main method which is the entry point for your application. In order to implement onStop(), this is how we have done it ...

step-1) Create following class ...

public class ShutdownHandler implements Runnable {
    public ShutdownHandler() {
        super();
    }
    @Override
    public void run() {
        // call to your onStop() or code right here;
    }
}

step-2) Inside the main method ...

Thread shutdownThread = new Thread(new ShutdownHandler());
Runtime.getRuntime().addShutdownHook(shutdownThread);

This will invoke the ShutdownHandler's run method when your java application terminates. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM