0

I'm trying to work out how to call to an endpoint that accepts a @Body parameter of Flowable<Integer>, to subscribe it to process the incoming stream (using Micronaut and the RxJava framework). I have this controller in Micronaut:

@Post(uri = "inner")
@ExecuteOn(TaskExecutors.IO)
HttpResponse inner(@Body Flowable<Integer> template){

    template.subscribe(new Subscriber<Integer>() {
        Subscription subscription

        @Override
        void onSubscribe(Subscription s) {
            subscription = s
            subscription.request(1l)
        }

        @Override
        void onNext(Integer integer) {
            log.info("inner:onNext called")
            log.info("${integer.toString()} - ${System.currentTimeMillis()}")
            subscription.request(1l)
        }

        @Override
        void onError(Throwable t) {
            log.info("inner:onError called")
            t.printStackTrace()
        }

        @Override
        void onComplete() {
            log.info("inner:onComplete called")
        }
    })
    ok()
}

Which I'm calling from another endpoint called /outer using the code:

rxStreamingHttpClient.exchangeStream(HttpRequest.POST("/inner", Flowable.fromIterable([1,2,3,4,5]))).subscribe()

So what I'm expecting is that I call the /outer endpoint, which in turn calls the /inner endpoint and feeds it the Integer stream to process. This seems to work, but only intermittently - the /inner endpoint always gets called, but it will often get somewhat through processing the stream and then call OnError an exception of com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input. However it does sometimes complete successfully. I can't find an example of how to do this - can anyone advise on what I'm doing wrong?

1 Answers1

0

I managed to get this working by amending the inner endpoint to use blockingSubscribe, so that the outer subscription didn't complete early. Not sure why this scenario isn't gracefully handled, but glad it works now.

template.blockingSubscribe(new Subscriber<Integer>() { ....