Java Spring-BOOT: 主循环旁边的HTTP处理程序

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

Java Spring-BOOT: main-loop beside HTTP-handler

问题

  1. 我从Spring BOOT开始
  2. public class App
  3. {
  4. public static void main(String[] args) throws Exception
  5. {
  6. System.out.println("--------------------");
  7. SpringApplication.run(App.class, args);
  8. for (;;) // 我的主循环
  9. {
  10. // 永久执行某些操作
  11. Thread.sleep(10000);
  12. }
  13. }
  14. }
  15. 除了HTTP处理组件我还想有一个主循环例如用于在HTTP之外执行任务)。
  16. * 这种设计中的循环是实现此目的的最佳方式吗
  17. 我正在使用配置绑定@Value@ConfigurationProperties)。
  18. * 如何在我的主循环中获取那些信息我不能使用静态变量来做到这一点
英文:

I am starting with Spring BOOT.

  1. public class App
  2. {
  3. public static void main (String [] args) throws Exception
  4. {
  5. System.out.println ("--------------------");
  6. SpringApplication.run (App.class, args);
  7. for (;;) // my main loop
  8. {
  9. // do something permanently
  10. Thread.sleep (10000);
  11. }
  12. }
  13. }

Beside the HTTP-handler-conponents I have I like to have a main-loop (e.g. for doing tasks beside HTTP).

  • Is this design with the loop the best way to do that?

I am using configuration-binding (@Value and @ConfigurationProperties too).

  • How can I get that information in my main-loop? I can not do it with static variables.

答案1

得分: 3

不要在您的应用程序类中执行类似的操作。

只需定义一个Spring Bean,并注释一个包含您想要定期执行的任何逻辑的函数,并使用@Scheduled进行注释。

欲了解更多信息,请参阅以下内容:https://www.baeldung.com/spring-scheduled-tasks

英文:

Dont do anything like that in your Application class.

Just define a spring bean and annotate a function that contains whatever logic you want to have performed regularly and annotate is with an @Scheduled.

Have a look at the following for more information: https://www.baeldung.com/spring-scheduled-tasks

答案2

得分: 3

参见scheduler spring文档。简要地:

在启动器/配置中添加@EnableScheduling:

  1. @SpringBootApplication
  2. @EnableScheduling
  3. public class SchedulingTasksApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SchedulingTasksApplication.class);
  6. }
  7. }

示例调度器组件:

  1. @Component
  2. public class ScheduledTasks {
  3. @Scheduled(fixedRate = 10000)
  4. public void reportCurrentTime() {
  5. // do anything
  6. }
  7. }
英文:

See scheduler spring doc. Briefly:

Add @EnableScheduling to starter/config:

  1. @SpringBootApplication
  2. @EnableScheduling
  3. public class SchedulingTasksApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SchedulingTasksApplication.class);
  6. }
  7. }

example scheduler component:

  1. @Component
  2. public class ScheduledTasks {
  3. @Scheduled(fixedRate = 10000)
  4. public void reportCurrentTime() {
  5. // do anything
  6. }
  7. }

答案3

得分: 1

我认为这不会是更好的设计。
如果你想要执行非HTTP任务,我建议创建一个新的Bean,然后使用 @Scheduled 注解,并可以指定你想要执行任务的时间。

  1. @Component
  2. public class YourTask {
  3. @Scheduled(fixedRate = 5000)
  4. public void reportCurrentTime() {
  5. // 你的任务
  6. }
  7. }
英文:

I dont think this would be the better design.
If you wan to do the non HTTTP task then then i would recommend creating new bean and using @Scheduled and can specify the time when you want to do it.

  1. @Component
  2. public class YourTask{
  3. @Scheduled(fixedRate = 5000)
  4. public void reportCurrentTime() {
  5. //your task
  6. }
  7. }

huangapple
  • 本文由 发表于 2020年3月4日 05:07:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/60515522.html
匿名

发表评论

匿名网友

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

确定