package org.example.bitgetsclient.task; import lombok.extern.slf4j.Slf4j; import org.example.bitgetsclient.BitgetsClientApplication; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.function.ServerResponse; import javax.servlet.http.HttpServletRequest; /** * @program: demo * @description: * @create: 2024-08-05 10:04 **/ @Component @Slf4j public class RunTask { @Autowired private ConfigurableApplicationContext context; @Autowired @Qualifier("threadPoolTaskExecutor") private ThreadPoolTaskExecutor threadPoolTaskExecutor; // @Scheduled(cron = "0 0/11 * * * ?") public void restart() { // 停止当前线程池中的任务 threadPoolTaskExecutor.shutdown(); // 创建新的线程池 threadPoolTaskExecutor.initialize(); Thread restartThread = new Thread(() -> { try { SpringApplication.exit(context, () -> 0); SpringApplication.run(BitgetsClientApplication.class); log.info("bitgets ws 重启成功"); } catch (Exception e) { e.printStackTrace(); log.error("bitgets ws 重启失败"); } }); restartThread.setDaemon(false); restartThread.start(); log.info("bitgets ws 重启失败"); } }