Managing Spring Boot application

Spring Boot is a brand new application framework from Spring. It allows fabulously quick development and rapid prototyping (even including CLI). One of its main features is to work from single "uber jar" file. By "uber jar" I mean that all dependencies, even an application server like Tomcat or Jetty are packed into a single file. In that we can start web application by typing
java -jar application.jar
The only thing we're missing is the managing script. And now I want to dive into that topic.

Of course to do anything more than starting our application we need to know its PID. Spring Boot has a solution named ApplicationPidListener. To use it we need to tell SpringApplication we want to include this listener. And there are to ways to achieve that.


Easiest way it to create file META-INF/spring.factories containing lines:
org.springframework.context.ApplicationListener=\
org.springframework.boot.actuate.system.ApplicationPidListener

Second way allows us to customize listener by specifying own name or location for PID file.
public class Application {
  
  public static void main(String[] args) {
    SpringApplication springApplication = 
        new SpringApplication(Application.class);
    springApplication.addListeners(
        new ApplicationPidListener("app.pid"));
    springApplication.run(args);
  }
}

Now, when we already have our PID file we need bash script providing standard operations like stop, start, restart and status checking. Below you can find simple script solving that challenge. Of course remember to customize highlighted lines :)
#!/bin/sh
JARFile="application.jar"
PIDFile="application.pid"
JVM_OPTS="-Xmx2g"
SPRING_OPTS="--logging.file=application.log"

function check_if_pid_file_exists {
    if [ ! -f $PIDFile ]
    then
 echo "PID file not found: $PIDFile"
        exit 1
    fi
} 

function check_if_process_is_running {
 if ps -p $(print_process) > /dev/null
 then
     return 0
 else
     return 1
 fi
}

function print_process {
    echo $(<"$PIDFile")
}

case "$1" in
  status)
    check_if_pid_file_exists
    if check_if_process_is_running
    then
      echo $(print_process)" is running"
    else
      echo "Process not running: $(print_process)"
    fi
    ;;
  stop)
    check_if_pid_file_exists
    if ! check_if_process_is_running
    then
      echo "Process $(print_process) already stopped"
      exit 0
    fi
    kill -TERM $(print_process)
    echo -ne "Waiting for process to stop"
    NOT_KILLED=1
    for i in {1..20}; do
      if check_if_process_is_running
      then
        echo -ne "."
        sleep 1
      else
        NOT_KILLED=0
      fi
    done
    echo
    if [ $NOT_KILLED = 1 ]
    then
      echo "Cannot kill process $(print_process)"
      exit 1
    fi
    echo "Process stopped"
    ;;
  start)
    if [ -f $PIDFile ] && check_if_process_is_running
    then
      echo "Process $(print_process) already running"
      exit 1
    fi
    nohup java $JVM_OPTS -jar $JARFile $SPRING_OPTS &
    echo "Process started"
    ;;
  restart)
    $0 stop
    if [ $? = 1 ]
    then
      exit 1
    fi
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0
I'm sure that there are a lot of possibilities to tune that script, so comments are welcomed :)

Comments

Marcin Cylke said…
Nice, also you could embed the bash script into the JAR itself. Thou a little bit hackish: http://skife.org/java/unix/2011/06/20/really_executable_jars.html
StackHolder said…
Marcin, that's really cool idea! It needs some changes in Boot but finally it should work
M said…
In Bash shell the PID of the started subprocess is stored in $! variable.

So you can manage java process easily with:

java ... &
echo "$!" > myjavaprogram.pid
kill `cat myjavaprogram.pid`

On Debian you could try use start-stop-daemon command as well. I'm not sure about command portability on another Linux.

Anyway, good contribution to the Spring Boot :-)
Hi, nice script, but shouldn't you put -D options before -jar:
nohup java $SPRING_OPTS -jar $JARFile &
StackHolder said…
In fact no. Spring options are passed as main method attributes, and those are specified ad the end of the command. Before -jar we're passing JVM options.
In fact yes and no. In your script you are using -D option so it not spring option but JVM option. If you change to SPRING_OPTS="--loggin.file=application.log" then script works fine.
StackHolder said…
Yes! You're right! I've fixed it and added JVM_OPTS variable
Unknown said…
Simple startup scripts with Java version checking for Linux and Windows.
https://github.com/tumakha/auth-agent/blob/master/src/assembly/bin-linux/auth-agent
https://github.com/tumakha/auth-agent/blob/master/src/assembly/root/bin/auth-agent.bat
Metricoid said…
I wanted to thank you for this great Information and knowledge, I definitely loved every little bit of it. I have you bookmarked your web site to check out the latest stuff you post. Your blog is eye-catching. I get pleasure from it. Thanks for sharing this beautiful piece of writing with me, get more information at Website Design And Development Company for more Metricoid Technology Solutions related information and knowledge.
3Dclever said…
This is really a good source of information, I will often follow it to know more information and expand my knowledge, I think everyone should know it, thanks Best Rapid prototyping service provider.
seodigiperform said…
This blog post provides a clear and practical guide to managing Spring Boot applications, emphasizing key concepts like monitoring and logging. It's impressive how Spring Boot streamlines application management, similar to how digital tools simplify marketing strategies. For those interested in mastering tech and marketing, understanding frameworks like Spring Boot is essential, just as learning advanced digital marketing techniques can boost a brand's online presence. If you're looking to bridge the gap between tech and marketing, exploring a Digital Marketing Course In Delhi By Digiperform can provide valuable insights into both domains.

Popular posts from this blog

Understanding Spring Web Initialization

Overview of the circuit breaker in Hystrix

Do we really still need a 32-bit JVM?