简体   繁体   中英

How to process NonBlocking with Smallrye messaging and reactive http client?

I have the following code.

@Incoming("my-topic")
void process(String someEvent) {
     String someResponse = assuminglyRealFastReactiveClientCall();
}

The above code throws a blocking thread exception. Which is corrected with @Blocking .

@Incoming("my-topic")
@Blocking
void process(String someEvent) {
     String someResponse = assuminglyRealFastReactiveClientCall();
}

If I switch String assuminglyRealFastReactiveClientCall() to Uni<String> assuminglyRealFastReactiveClientCall()

I'm guessing the consumer method has to switch to manual ack strategy and the message needs to be acked/nacked based on the result of the subscribe, so?

@Incoming("my-topic")
void process(Message<String> someEvent) {
     assuminglyRealFastReactiveClientCall()
                .subscribe().with(s -> {
                    System.out.println("Response: " + s);
                    event.ack();
                }, t -> event.nack(t));

}
    @Incoming("my-topic")
    Uni<Void> process(Message<String> someEvent) {
        return assuminglyRealFastReactiveClientCall()
                .invoke(this::handleResponse)
                .chain(response -> Uni.createFrom().completionStage(someEvent.ack()));

    }

    private void handleResponse(String response) {
        // Do something with the response
    }

The paragraph Consuming Messages in the Smallrye Reactive messaging documentation has many more examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM