Running Spring Boot, Part I

Spring Boot offers a lot of functionality out of the box, but one thing that’s lacking is a standard way to run Spring Boot applications. This is not really the fault of Spring Boot, the question is really a larger one of how to run any Java program. Some people want to run it as a program, some as a service. Some want to run on Windows, some on Linux. There are so many deployment options that it would be impossible to generalize a solution for everybody, so this is the point at which people usually just do a little research and deploy and run the program in a way that they are the most comfortable.

This post will step through setting up a startup script for a spring boot application on Linux. This has been done before, but the aforementioned post example left out a couple steps.

One thing that needs to be done is to store the process id in a file, then the file can be tested to see if the application is running, and the id can be extracted to use a native system command to kill it. Spring Boot gives us a nice way to do this, note that the pid file can be set to be deleted on exit. In this case we are just leaving the pid file in the current directory, it may be more appropriate to place it in /var/run.

@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) throws Exception {

        File pid = new File("app.pid");
        pid.deleteOnExit();
        
        SpringApplication app = new SpringApplication(Application.class);
        app.setShowBanner(false);
        app.addListeners(new ApplicationPidListener(pid));
        app.run(args);
    }
}

In the next post we will look at the script to run the application and take advantage of this pid file!

Advertisements

Leave a comment

Filed under Software Engineering

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s