Could somebody help me to do the following:
@PostContruct public void func() {
webclient.get()...subscribe();
}
webclient call will terminate after func() returns. Most likely it will happen before the first request comes in, but no guarantees. The other option is to block(), which defeats the purpose of being reactive.
What would be the right way to make reactive calls in @PostConstruct methods?
Thank you.
I created a simple bean.
Synchronous update:
@Component
public class BeanTest {
private String postConstructValue;
@PostConstruct
public void init(){
try {
Thread.sleep(5000);
this.postConstructValue = "Construction done";
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Scheduled(fixedRate = 500)
public void print(){
System.out.println(
this.postConstructValue
);
}
}
Asynchronous update:
@Component
public class BeanTest {
private String postConstructValue;
@PostConstruct
public void init(){
Flux.just("Construction done")
.delayElements(Duration.ofSeconds(5))
.subscribe(s -> this.postConstructValue = s);
}
@Scheduled(fixedRate = 500)
public void print(){
System.out.println(
this.postConstructValue
);
}
}
null
for few seconds. Then it started printing "Construction done". It does not terminate the Flux postConstruct value update. It happened asynchronously. Reactive approach is good when you want a non-blocking behavior and getting something done asynchronously. If you think that your component creation should wait for proper construction, you have to block! Otherwise, you can go with second approach.