1

I have Spring Boot Starter with a REST Controller working perfect:

@RestController
public class SimpleController {
 @GetMapping("/") 
 public String helloWorld() {
 return "Hello world"; 
 }
}

However, when I add other Controller with infinite loop the REST Controller doesn't work:

Error: connect ECONNREFUSED 127.0.0.1:8080

It is a samle code of other Controller.

@Component 
public class HelloWorld {
 @Autowired
 public void hello() 
 {
 while(true) {
 System.out.println("Hello world!");
 Thread.sleep(12000);
 } 
 }
}

So, other Controller (HelloWorld class) is always working, while RestController (SimpleController class) works only if other Controller is disabled. Why so?

asked Jul 3, 2020 at 21:24

2 Answers 2

1

To add to the accepted answer, you can use an implementation of CommandLineRunner or ApplicationRunner like

package com.example.restservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CommandRunner implements CommandLineRunner {
 @Autowired
 private HelloWorld helloWorld;
 @Override
 public void run(String... args) throws Exception {
 while (true) {
 System.out.println("Hello world!" + helloWorld);
 Thread.sleep(12000);
 }
 }
}
answered Jul 3, 2020 at 22:08
Sign up to request clarification or add additional context in comments.

4 Comments

Or you use a @Scheduled method so you don't have an infinite loop code smell?
yeah. That is better option for your usecase
@KavithakaranKanapathippillai Thank you very much for your excellent explanation and code example. I appreciate you have shared your knowledge!
In real world, scheduled jobs are the best way to achieve 'infinite loops'. For sandboxes, you can use worker thread etc...
-1

That's because your application never start properly. While Spring try to create the bean instances, set up dependencies and the whole infrastucrures, it start an infinite loop. It stuck on that, so your server never finish to starts, application context never setup. Note that the startup (mostly) runs on the main thread, and you block it.

answered Jul 3, 2020 at 21:40

4 Comments

Thank you for your response. Indeed, I could see in the logs that Application has not completed the initialization stage. So have I create a parralel thread for the loop to fix the issue?
it depends, what do you want to achive?
I just want that both of this controllers working simuletiously.
if you want to test this behaviour, then yes, you can start it on other thread.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.