In my software I use @Async to call a certain method from a Bean in the background, on Tomcat startup, and this works as expected.
But, under some defined circumstances, what the method does is failing, and the method myUtil.connect() calls itself then to try again.
The basic code looks as follows.
The bean definition:
import org.springframework.context.annotation.Bean;
import package.MyService;
@Bean(initMethod = "init", destroyMethod = "destroy")
MyService myService() {
MyService bean = new MyService();
return bean;
}
And the MyService bean:
import javax.inject.Inject;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class MyService {
@Inject
private MyUtil myUtil;
@Async
public void init() {
myUtil.connect();
}
}
I have tried annotating the method connect() also with @Async believing, since it calls itself, @Async would make it run in background as well, with no success.
Is there a way to assure subsequent calls to connect() "remain" in the background ?
Edit:
Would it be working to make connect() a wrapper around a function which does the real work? This way connect() would just be called once, and the wrapped function would call itself in case of failure.
init()?@ASyncis probably using a proxy and internal call are not proxied.connect()call itself? What do you expect to happen? What actually happens?