I have one example of a GET method that generates a Randon INT and sends back to the client
@GetMapping
public Flux<String> search() {
return Flux.create(fluxSink -> {
Random r = new Random();
int n;
for (int i = 0; i < 10; i++) {
n= r.nextInt(1000);
System.out.println("Creating:"+n);
fluxSink.next(String.valueOf(n));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
fluxSink.complete();
});
}
Curl:
curl -v -H "Accept: text/event-stream" -X GET 'http://localhost:8080/stream
With this code using a CURL command I can only see the numbers in my client when the fluxSink.complete happens.
Now If I change my code to:
@GetMapping
public Flux<String> search() {
return Flux.create(fluxSink -> {
Thread t = new Thread(() -> {
Random r = new Random();
int n;
for (int i = 0; i < 10; i++) {
n= r.nextInt(1000);
System.out.println("Creating:"+n);
fluxSink.next(String.valueOf(n));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
fluxSink.complete();
});
t.start();
});
}
Wrapping up the process into a Thread it works fine. I can see the data being transferred fine when the fluxSink.next happens.
Can anyone explain this effect? How can I see the data flowing without explicitly using a Thread?
Thank you!