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

chris01 :

I am starting with Spring BOOT.

public class App 
{
    public static void main (String [] args) throws Exception
    {
        System.out.println ("--------------------");

        SpringApplication.run (App.class, args);

        for (;;)   // my main loop
        {
            // do something permanently
            Thread.sleep (10000);
        }    
    }  
}

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.
User9123 :

See scheduler spring doc. Briefly:

Add @EnableScheduling to starter/config:

@SpringBootApplication
@EnableScheduling
public class SchedulingTasksApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchedulingTasksApplication.class);
    }
}

example scheduler component:

@Component
public class ScheduledTasks {
    @Scheduled(fixedRate = 10000)
    public void reportCurrentTime() {
        // do anything
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23197&siteId=1