我想每4个小时安排一个作业,每4个小时重新启动spring批处理应用程序。我正在使用下面的代码,但这仅是计划我的Jobs。当应用程序启动时,我运行的方法很少。这些方法是没有安排
@EnableScheduling
public class App {
private static ConfigurableApplicationContext context;
private static final Logger logger = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
BasicConfigurator.configure();
context = SpringApplication.run(App.class, args);
}
@Scheduled(cron = "0 0 0/4 * * * ")
public static void restart() {
ApplicationArguments args = context.getBean(ApplicationArguments.class);
logger.info("##### Scheduler Started #####"+System.currentTimeMillis());
Thread thread = new Thread(() -> {
context.close();
logger.info("***Context Closed***"+System.currentTimeMillis());
context = SpringApplication.run(App.class, args.getSourceArgs());
logger.info("###### Context Restarted "+
System.currentTimeMillis());
});
thread.setDaemon(false);
thread.start();
}
}
由于该方法在Constructor中被调用,因此在初始化bean时将被调用。因此,我必须每次都重新启动应用程序