如何使用 Spring Boot 在后台运行某些进程?这是我需要的一个例子:
@SpringBootApplication
public class SpringMySqlApplication {
@Autowired
AppUsersRepo appRepo;
public static void main(String[] args) {
SpringApplication.run(SpringMySqlApplication.class, args);
while (true) {
Date date = new Date();
System.out.println(date.toString());
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
最佳答案
您可以使用异步行为。当您调用该方法并且当前线程确实等待它完成时。
像这样创建一个可配置的类。
@Configuration
@EnableAsync
public class AsyncConfiguration {
@Bean(name = "threadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
}
然后在一个方法中使用:
@Async("threadPoolTaskExecutor")
public void someAsyncMethod(...) {}
看看spring documentation欲了解更多信息
关于java - 使用 spring boot 在后台运行进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45281064/