DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Managing Spring Boot Application

Managing Spring Boot Application

Jakub Kubrynski user avatar by
Jakub Kubrynski
·
May. 08, 14 · Interview
Like (2)
Save
Tweet
Share
42.55K Views

Join the DZone community and get the full member experience.

Join For Free
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"
SPRING_OPTS="-DLOG_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 $SPRING_OPTS -jar $JARFile &
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 :)
Spring Framework Spring Boot Web application Application framework

Published at DZone with permission of Jakub Kubrynski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Common Firewall Misconfigurations and How to Address Them
  • DeveloperWeek 2023: The Enterprise Community Sharing Security Best Practices
  • How To Set Up and Run Cypress Test Cases in CI/CD TeamCity
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: