运行Spring Boot应用程序,每1分钟执行一次,并确保每个控制器都同步。

huangapple go评论118阅读模式
英文:

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&#39;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
}

huangapple
  • 本文由 发表于 2020年4月10日 21:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定