While working with multiple spring boot application, It’s difficult to identify spring boot application PID so spring boot provide ways to write application PID to file while application will startup. Here is example of write Spring boot application process id to separate file using ApplicationPidFileWriter.

SpringBootConfig

org.springframework.boot.system.ApplicationPidFileWriter is listener which is responsible to write PID to specified configuration.

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.system.ApplicationPidFileWriter;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */

@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication springApplication = new SpringApplication(SpringBootConfig.class);
        springApplication.addListeners(new ApplicationPidFileWriter());     // register PID write to spring boot. It will write PID to file
        springApplication.run(args);
    }
}

application.properties

When spring boot application will start, It will write process id to specified file at spring.pid.file configuration. spring.pid.file is optional, If this properties is not not specified then spring boot will create application.pid file at root location of spring boot application jar.

spring.pid.file=e:\\spring-boot-pid\\pid.txt #Location of the PID file to write

Output:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2017-09-23 13:35:55.387  INFO 74672 --- [           main] com.javadeveloperzone.SpringBootConfig   : Starting SpringBootConfig on Mahesh with PID 74672 (F:\extrawork\spring-boot\spring-boot-pid-example\target\classes started by Lenovo in F:\extrawork\spring-boot\spring-boot-pid-example)
2017-09-23 13:35:55.391  INFO 74672 --- [           main] com.javadeveloperzone.SpringBootConfig   : No active profile set, falling back to default profiles: default
2017-09-23 13:35:55.479  INFO 74672 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]37883b97: startup date [Sat Sep 23 13:35:55 IST 2017]; root of context hierarchy
2017-09-23 13:35:58.768  INFO 74672 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-23 13:35:58.789  INFO 74672 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-09-23 13:35:58.792  INFO 74672 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-09-23 13:35:58.921  INFO 74672 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-09-23 13:35:58.922  INFO 74672 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3448 ms
2017-09-23 13:35:59.135  INFO 74672 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-23 13:35:59.140  INFO 74672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-23 13:35:59.141  INFO 74672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-23 13:35:59.141  INFO 74672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-23 13:35:59.141  INFO 74672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-23 13:35:59.546  INFO 74672 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]37883b97: startup date [Sat Sep 23 13:35:55 IST 2017]; root of context hierarchy
2017-09-23 13:35:59.634  INFO 74672 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[]}" onto public java.lang.String com.javadeveloperzone.controller.SpringBootExampleController.hello()
2017-09-23 13:35:59.642  INFO 74672 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-09-23 13:35:59.642  INFO 74672 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-09-23 13:35:59.698  INFO 74672 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-23 13:35:59.698  INFO 74672 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-23 13:35:59.754  INFO 74672 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-23 13:36:00.082  INFO 74672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-09-23 13:36:00.206  INFO 74672 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-09-23 13:36:00.214  INFO 74672 --- [           main] com.javadeveloperzone.SpringBootConfig   : Started SpringBootConfig in 5.317 seconds (JVM running for 5.979)

It will create file and write application pid to file, We can refer this PID for future references:

If spring.pid.file is specified:

 

Spring boot ApplicationPidFileWriter Example - Process ID writer example

Spring boot ApplicationPidFileWriter Example – Process ID writer example

If spring.pid.file is not specified:

 

Spring boot ApplicationPidFileWriter Example application.pid

Spring boot ApplicationPidFileWriter Example application.pid

References :

Spring Boot Document – Process monitor

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *