Emitting JAX-RS Messages into MicroProfile Reactive Messaging Channels

Reactive Messaging for MicroProfile API allows mapping of method's parameters and return values to virtual channels:


import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;

public class HelloListener {

    @Incoming("hello")
    @Outgoing("confirmed")
    public String onHello(String hello) {
        return "confirmed: " + hello;
    }
}    

Messages consumed by Jakarta RESTful Web Services / Java API for RESTful Web Services can be ingested into the system with the injected Emitter qualified with the @Channel annotation.

After sending the String to the Emitter, the message becomes available in the channel and can be consumed as method parameter or sent to Kafka topic:


import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.reactive.messaging.Channel;
import org.eclipse.microprofile.reactive.messaging.Emitter;

@Path("/hello")
public class HelloResource {

    @Inject
    @Channel("hello")
    Emitter<String> emitter;

    @POST
    @Consumes(MediaType.TEXT_PLAIN)
    public void forward(String message) {
        emitter.send(message);
    }
}    

See it in action, and from scratch in 7 minutes:

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License