Rémy Schumm blog ingénieur

Kafka-Emitter on Quarkus

publiziert am 11. 07. 2019 um 06:07

This is going to be a very short post: Based on the the Quarkus Kafka-Guide, I wanted to write my first little messages to a Kafka-Topic without using the reactive stream model. This info was hard to find in the dokus…

…so I decided to write this little post:

in application.properties add just another Kafka-Config, like:

## nachricht-bus topic (WRITE)
mp.messaging.outgoing.nachricht-bus.connector=smallrye-kafka
mp.messaging.outgoing.nachricht-bus.topic=nachricht-bus
#mp.messaging.outgoing.nachricht-bus.bootstrap.servers=localhost:9092
mp.messaging.outgoing.nachricht-bus.key.serializer=org.apache.kafka.common.serialization.StringSerializer
mp.messaging.outgoing.nachricht-bus.value.serializer=org.apache.kafka.common.serialization.StringSerializer
#mp.messaging.outgoing.nachricht-bus.acks=1

…to prepare a topic nachricht-bus, where you will write to.
Then, the Java Code gets very easy:

package ch.schumm.halloquarkus;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.smallrye.reactive.messaging.annotations.Emitter;
import io.smallrye.reactive.messaging.annotations.Stream;

@ApplicationScoped
public class HalloService {

    @Inject
    @Stream("nachricht-bus") // Emit on the channel 'nachricht-bus'
    Emitter<String> nachrichtEmitter;

    public String sagHallo(String name) {
        nachrichtEmitter.send(name); 
        return "neuste Nachricht: " + name;
    }
}

The Quarkus Kafka modules will make the rest for you. You might have to configure the property e.g. kafka.bootstrap.servers=your-kafka-boostrap-server:9092 to point to the Kafka Boostrap Servers.

Hinweis: dieser Blog wiederspiegelt meine persönliche Meinung und hat nichts mit meiner Anstellung als Dozent der zhaw noch mit anderen Anstellungen zu tun.


zurück zum Seitenanfang