0

I have an incoming io.cloudevents.CloudEvent object from HTTP POST, and would like to relay the object as-is to the Kafka topic using Quarkus using this approach. Is there a way to do this? Thanks.


----RESTResource.java-----

    @Inject
    @Broadcast 
    @Channel("mychannel") Emitter<CloudEvent> ceEmitter;

    @POST
    public CompletionStage<Response> sendEvent(CloudEvent object) {
        return CompletableFuture.supplyAsync(() -> {
            Set<ConstraintViolation<CloudEvent>> violations = validator.validate(object);
            ceEmitter.send(object);
            return Response.accepted().build();
        });
    }


-----KafkaProducer.java-----
        @Incoming("mychannel")
        @Outgoing("mytopic")
        public Message<CloudEvent>  generate(CloudEvent ce) {
            return Message.of(ce);
        }

mp.messaging.outgoing.mytopic.value.serializer=??? mp.messaging.incoming.mytopic.value.deserializer=???

  • How do you want to send it? What do you expect in your outgoing record? Should the data be the cloud event (in this case you may need a serializer). Otherwise, just build a record (using Record.of) with the key and value extracted from the incoming cloud event and configure the connector to write cloud events. – Clement May 21 '21 at 12:37
  • @clement - I've added sample code on what I was trying to do. I had issues with de/serialization. What would you set the de/serializer classes to be in the above config to achieve this? Thanks! – codemasorete May 25 '21 at 21:40
  • Do do that you need a CloudEvent serializer (which you will need to provide), or use the built-in support for Cloud Event but in this case, extract the metadata from the incoming cloud event (ce) and rebuild one using CloudEventMetadata. See https://smallrye.io/smallrye-reactive-messaging/smallrye-reactive-messaging/3.3/kafka/kafka.html#_sending_cloud_events. – Clement May 27 '21 at 05:33
  • @clement thanks. And what would you recommend is the best way to ensure that the RESTResource doesn't return a 200 OK, until the CloudEvent has been written into the Kafka Topic, i.e. making it a blocking call for error handling so that the event is not lost before that should the quarkus container go down. – codemasorete May 28 '21 at 12:06
  • If done right (meaning messages are chained, and post-acknowledgement is used) the CompletionStage returned by the emitter.send method is completed when the message is acknowledged, meaning sent to kafka. – Clement May 30 '21 at 07:25

0 Answers0