1
zj
2024-08-07 f6bd6d0ad7b21ed124afca7c30c8fcea93a56319
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.example.kucoinclient.task;
 
import lombok.extern.slf4j.Slf4j;
import org.example.kucoinclient.KucoinClientApplication;
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;
 
/**
 * @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/1 * * * ?")
    public void restart() {
        // 停止当前线程池中的任务
        threadPoolTaskExecutor.shutdown();
        // 创建新的线程池
        threadPoolTaskExecutor.initialize();
 
        Thread restartThread = new Thread(() -> {
            try {
                SpringApplication.exit(context, () -> 0);
                SpringApplication.run(KucoinClientApplication.class);
                log.info("bitgets ws 重启成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("bitgets ws 重启失败");
            }
        });
        restartThread.setDaemon(false);
        restartThread.start();
        log.info("bitgets ws 重启失败");
    }
 
}