1. startup project with jar
后台启动
@echo off
echo starting...
START javaw -jar springboot-20220403-0.0.1-SNAPSHOT.jar
pause
指定JDK版本启动
"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" -server -jar design-pattern-1.0-SNAPSHOT.jar
2. shutdown project with jar
(1) 使用taskkill PID
@echo off
set port=8080
echo port : %port%
for /f "usebackq tokens=1-5" %%a in (`netstat -ano ^| findstr %port%`) do (
if [%%d] EQU [LISTENING] (
set pid=%%e
)
)
for /f "usebackq tokens=1-5" %%a in (`tasklist ^| findstr %pid%`) do (
set image_name=%%a
)
taskkill /f /pid %pid%
pause
(2) SpringBoot 2.3 + actuator优雅退出
- 1.1 添加pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 1.2 修改配置
server:
shutdown: GRACEFUL
# 最大等待时间
spring:
lifecycle:
timeout-per-shutdown-phase: 30s
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: shutdown
- 1.3 访问/actuator/shutdown即可(POST)
@echo off
curl -X POST http://localhost:8080/actuator/shutdown
pause
(3) 关闭ApplicationContext实现
@RestController
@Slf4j
public class ShutDownController implements ApplicationContextAware {
private ApplicationContext applicationContext;
@GetMapping("/shutdown")
public void shutDownContext() {
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext;
log.warn("Context is shutdown...");
ctx.close();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}