1

I'm trying to test streaming a Flux with a delay of 1000 millis for each element.

@Get(value = "/carts/reactive", produces = MediaType.APPLICATION_JSON_STREAM)
public Flux<Cart> getCartsReactive() {
     return Flux.range(1, 10)
            .map(integer -> Cart.builder().name("cart" + integer).build())
            .delayElements(Duration.ofMillis(1000))
            .doOnNext(cart -> log.info("Returning Flux of cart: " +cart));
}

When I hit the service, I don't get a response for 10 seconds and get a blog of response.

curl http://localhost:7070/carts/reactive

{"name":"cart1"}{"name":"cart2"}{"name":"cart3"}{"name":"cart4"}{"name":"cart5"}{"name":"cart6"}{"name":"cart7"}{"name":"cart8"}{"name":"cart9"}{"name":"cart10"}

Is this a bug? Why doesn't it emit 1 cart every second?

Anoop Hallimala
  • 625
  • 1
  • 11
  • 25

1 Answers1

0

There's nothing wrong with your code as you've written it - here's a complete project using micronaut + reactor with exactly that code in a controller:

https://github.com/berry120/micronaut-reactive-demo

It produces the elements one at a time, as expected, with a one second delay:

Micronaut gif

You can compare that project to your own to see what configuration / code you may be missing that's preventing it from working properly. I haven't done anything particularly special with the above project at all - this was purely created from the Micronaut initializr, with the reactor add-on included.

Note that you should configure the micronaut-reactor module (implementation("io.micronaut.reactor:micronaut-reactor")) rather than just including reactor as a third party dependency, otherwise that may cause issues.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • 1
    Thanks for the answer. I download the GitHub project and tried different things. I can conclude that the emission rate is maintained only on Windows. In Mac, the emission rate is not maintained and all the jsons are sent as a blob. Can you try this is a linux/Mac machine? I think its a serious bug. – Anoop Hallimala Mar 10 '21 at 13:26
  • @AnoopHallimala That's really surprising - yup, I'll try on my Mac and let you know. – Michael Berry Mar 10 '21 at 13:29
  • @AnoopHallimala Looks like you're correct - I see the same behaviour on my Mac. I'd create a bug with the micronaut team, link them to that Github repo and mention the different behaviour you're seeing (on windows & Mac.) They should be able to either confirm it as an issue or point you towards what's wrong. – Michael Berry Mar 15 '21 at 10:04
  • Thanks, Michael. Github issue created https://github.com/micronaut-projects/micronaut-core/issues/5102 – Anoop Hallimala Mar 15 '21 at 10:12