Piotr Sobczyk
Piotr Sobczyk

Reputation: 6583

Command-line application using Spring context never ends

I created mini framework to executing some spring beans from main() method rather than deploying and running full-fledged webapp just to launch those beans. It looks like this:

public abstract class BaseLauncher {

    private static final String APP_CONTEXT_PATH = "com/project/dev/launchers/launchersApplicationContext.xml";
    static ApplicationContext context = new ClassPathXmlApplicationContext(APP_CONTEXT_PATH);

    protected void launch() {
        context.getBean(getClass()).perform();
        //The process never ends so we want to know when we can kill it
        System.out.println("launcher finished");
    }

    @Transactional
    abstract protected void perform();

}

And example launcher looks like this:

@Component
public class ParamLoaderLauncher extends BaseLauncher {
    @Inject
    ParamLoader paramLoader;

    public static void main(String[] args) {
        new ParamLoaderLauncher().launch();
    }

    @Override
    protected void perform() {
        paramLoader.loadParams();
    }
}

It all works great except that when the invoked bean method is finished, application just keep running and we need to kill it manually. I guess it has something to do with using spring app context. Maybe some special spring-related non-deamon thread is launched? If so, is there any way to kill it? Or what other cause of this may be in such simple code?

Upvotes: 0

Views: 2441

Answers (1)

Santosh
Santosh

Reputation: 17923

For standalone applications (not running in any container), shutdownhook needs to be registered for clean shutdown of the spring container when application exits.

Upvotes: 3

Related Questions