0

I'm trying to inject a KafkaTemplate to send a single message. I'm developing a small function that lies outside the reactive approach.

I can only find examples that use @Ingoing and @Outgoing from Smallrye but I don't need a KafkaStream.

I tried with Kafka-CDI but I'm unable to inject the SimpleKafkaProducer.

Any ideas?

For Clement's answer

It seems the right direction, but executing orders.send("hello"); I receive this error:

(vert.x-eventloop-thread-3) Unhandled exception:java.lang.IllegalStateException: Stream not yet connected

I'm consuming from my topic by command line, Kafka is up and running, if I produce manually I can see the consumed messages.

It seems relative to this sentence by the doc:

To use an Emitter for the stream hello, you need a @Incoming("hello") somewhere in your code (or in your configuration).

I have this code in my class:

    @Incoming("orders")
    public CompletionStage<Void> consume(KafkaMessage<String, String> msg) {
        log.info("Received message (topic: {}, partition: {}) with key {}: {}", msg.getTopic(), msg.getPartition(), msg.getKey(), msg.getPayload());
        return msg.ack();
    }

Maybe I've forgotten some configurations?

firegloves
  • 5,581
  • 2
  • 29
  • 50

2 Answers2

4

So, you just need to use an Emitter:

@Inject
@Stream("orders") // Emit on the channel 'orders'
Emitter<String> orders;

// ...
orders.send("hello");

And in your application.properties, declare:

## Orders topic (WRITE)
mp.messaging.outgoing.orders.type=io.smallrye.reactive.messaging.kafka.Kafka
mp.messaging.outgoing.orders.topic=orders
mp.messaging.outgoing.orders.bootstrap.servers=localhost:9092
mp.messaging.outgoing.orders.key.serializer=org.apache.kafka.common.serialization.StringSerializer
mp.messaging.outgoing.orders.value.serializer=org.apache.kafka.common.serialization.StringSerializer
mp.messaging.outgoing.orders.acks=1

To avoid Stream not yet connected exception, as suggested by doc:

To use an Emitter for the stream hello, you need a @Incoming("hello") somewhere in your code (or in your configuration).

Assuming you have something like this in your application.properties:

# Orders topic (READ)
smallrye.messaging.source.orders-r-topic.type=io.smallrye.reactive.messaging.kafka.Kafka
smallrye.messaging.source.orders-r-topic.topic=orders
smallrye.messaging.source.orders-r-topic.bootstrap.servers=0.0.0.0:9092
smallrye.messaging.source.orders-r-topic.key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
smallrye.messaging.source.orders-r-topic.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
smallrye.messaging.source.orders-r-topic.group.id=my-group-id

Add something like this:

@Incoming("orders-r-topic")
public CompletionStage<Void> consume(KafkaMessage<String, String> msg) {
    log.info("Received message (topic: {}, partition: {}) with key {}: {}", msg.getTopic(), msg.getPartition(), msg.getKey(), msg.getPayload());
    return msg.ack();
}
firegloves
  • 5,581
  • 2
  • 29
  • 50
Clement
  • 2,817
  • 1
  • 12
  • 11
3

Since Clement's answer the @Stream annotation has been deprecated. The @Channel annotation must be used instead.

You can use an Emitter provided by the quarkus-smallrye-reactive-messaging-kafka dependency to produce message to a Kafka topic.

A simple Kafka producer implementation:

public class MyKafkaProducer {

    @Inject
    @Channel("my-topic")
    Emitter<String> myEmitter;

    public void produce(String message) {
      myEmitter.send(message);
    }
}

And the following configuration must be added to the application.properties file:

mp.messaging.outgoing.my-topic.connector=smallrye-kafka
mp.messaging.outgoing.my-topic.bootstrap.servers=localhost:9092
mp.messaging.outgoing.my-topic.value.serializer=org.apache.kafka.common.serialization.StringSerializer

This will produce string serialized messages to a kafka topic named my-topic.

Note that by default the name of the channel is also the name of the kafka topic in which the data will be produced. This behavior can be changed through the configuration. The supported configuration attributes are described in the reactive Messaging documentation

Simon C
  • 316
  • 2
  • 5