How to startup and shutdown jars in Windows

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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值