A better way to handle the infinite loop

In my previous post, I wrote about running infinite loop in Java and also I mention that it was not recommended way of doing and here is the alternative

Instead of we implementing the infinite loop, you can offload that to the framework if you are using Spring-boot

Ref : https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling

@Service
public class LoopHelper{

    //Executes in every 1 sec
    @Scheduled(fixedRate=1000)
    public void checkforMessage() {
        //Check message and handle
    }
}

and Enable the scheduling for your application

@SpringBootApplication
@EnableScheduling
public class Application {

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

Also published on Medium.

Default image
Vignesh A Sathiyanantham
AWS Certified developer with 8 years of extensive experience working for Cloud and Datacenter solutions and leading a small team in a lean startup based on Bangalore, India
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.