英文:
Run springboot application every 1 minut and with synchronise every controller
问题
我开发了一个包含4个控制器的Spring Boot应用程序,我需要在第一个控制器完成后运行第二个控制器,以执行一些工作,然后是第三个控制器,最后是第四个控制器。我对每个控制器都使用了@Scheduled(fixedRate = 30000)注解,但是使用这种方法,我没有办法确保这4个控制器之间的同步。我需要帮助找到一种解决方案,可以让它们同步运行,或者另一种自动按优先级同时运行所有控制器的解决方案。我附上了代码架构:
```java
@CrossOrigin("*")
@RestController
public class Collector {
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void collector() {
// 方法内容
}
}
@CrossOrigin("*")
@RestController
public class Loader {
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void loader() {
// 方法内容
}
}
@CrossOrigin("*")
@RestController
public class Export {
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void export() {
// 方法内容
}
}
@CrossOrigin("*")
@RestController
public class Send {
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void send() {
// 方法内容
}
}
<details>
<summary>英文:</summary>
I developed spring-boot application with 4 controllers and I need to run the first one after is finished the seconde controller launched to do some work and the there and finally the fourth one, I do @Scheduled(fixedRate = 30000) for every controller, but with this solution, I don't have a synchro between the 4 controllers, I need to help me with a solution to run it with synchro or another solution to automatically run all controller together with priority. and i attached the code architecture
@CrossOrigin("*")
@RestController
public class collector{
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void collector( )
{
// the methodes
}
}
@CrossOrigin("*")
@RestController
public class Loader {
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void loador( )
{
// the methodes
}
}
@CrossOrigin("*")
@RestController
public class export{
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void export( )
{
// the methodes
}
}
@CrossOrigin("*")
@RestController
public class send{
@Autowired
DataSource datasource;
@Scheduled(fixedRate = 40000)
public void send( )
{
// the methodes
}
}
</details>
# 答案1
**得分**: 0
这不是 Spring 中调度器的工作方式。由于您想要在前一个方法完成后立即同步执行每个方法,只需调度触发方法链的方法,并随意在调度方法内部调用它们。
```java
@Scheduled(fixedRate = 40000)
public void collector() {
// 方法调用
this.loader.loador(); // 第二个方法
this.export.export(); // 第三个方法
this.send.send(); // 第四个方法
}
英文:
This is not how schedulers in Spring work. Since you want to execute each method synchronously right after the previous one has finished, schedule just the one that triggers the chain of methods and feel free to call them inside the scheduled method.
@Scheduled(fixedRate = 40000)
public void collector() {
// the methods // body of the 1st one
this.loader.loador(); // 2nd one
this.export.export(); // 3rd one
this.send.send(); // 4th one
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论