简体   繁体   English

动态主题名称/Quarkus SmallRye Reactive Messaging Kafka

[英]Dynamic Topic Name / Quarkus SmallRye Reactive Messaging Kafka

I want to use this extension: [Quarkus Smallrye Reactive Messaging Kafka]我想使用这个扩展:[Quarkus Smallrye Reactive Messaging Kafka]

But in my application the name of the topics is not known in advance, it is specified according to the message received from the user at runtime.但是在我的应用程序中,主题的名称是事先不知道的,它是根据运行时从用户那里收到的消息指定的。 How can I specify the topic name and settings related to the topic without annotations and programmatically?如何在没有注释的情况下以编程方式指定主题名称和与主题相关的设置? (Only for send a message to Kafka -> Produce) (仅用于向 Kafka -> Produce 发送消息)

@ApplicationScoped
public class PriceGenerator {

    private Random random = new Random();

    // Don't want to use this 
    // "generated-price" not known at build time
    @Outgoing("generated-price")                       
    public Multi<Integer> generate() {                  
        return Multi.createFrom().ticks().every(Duration.ofSeconds(5))
                .onOverflow().drop()
                .map(tick -> random.nextInt(100));
    }

}

or these configs should set at runtime and programmatically或者这些配置应该在运行时以编程方式设置

mp.messaging.outgoing.generated-price.connector=smallrye-kafka
mp.messaging.outgoing.generated-price.topic=prices
mp.messaging.outgoing.generated-price.value.serializer=org.apache.kafka.common.serialization.IntegerSerializer

Because I did not know the way, I used the native Kafka driver因为不识路,所以用的是原生的kafka驱动

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-kafka-client</artifactId>
    </dependency>
Properties props = new Properties();
props.put("bootstrap.servers", "85.93.89.115:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("linger.ms", 1);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
producer.send(new ProducerRecord<String, String>(topicName.toString(), messageFactory.MessageToString(message)));

You're able to override the value of topic dynamically at startup or at any time you need, but here is a snippet of code to indicates how to override the predefined value of topic:您可以在启动时或任何需要的时候动态地覆盖主题的值,但这里有一段代码指示如何覆盖主题的预定义值:

@ApplicationScoped
public class AppLifecycleBean {

    void onStart(@Observes StartupEvent ev) {               
        System.setProperty("mp.messaging.outgoing.generated-price.topic", "example");
    }

    void onStop(@Observes ShutdownEvent ev) {               
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Quarkus/SmallRye Reactive Messaging/Kafka 连接多个集群 - Quarkus/SmallRye Reactive Messaging/Kafka connect with multiple clusters Quarkus Kafka Streams/Reactive Messaging 反序列化异常 - Quarkus Kafka Streams/Reactive Messaging Deserializing Exception Quarkus + Kafka + Smallrye 异常处理 - Quarkus + Kafka + Smallrye exception handling ServerInterceptor gRPC 在 Quarkus 中没有捕捉到 SmallRye Mutiny Reactive 的异常 - ServerInterceptor gRPC not catching exceptions with SmallRye Mutiny Reactive in Quarkus 如何将反应流连接到 quarkus / smallrye 中的 AMQP 代理 - How to connect a reactive stream to an AMQP broker in quarkus / smallrye 如何在使用 Quarkus 的 Microprofile Reactive Messaging 启动应用程序时删除所有 Kafka 消息? - How can I drop all Kafka messages while starting an app using Microprofile Reactive Messaging with Quarkus? 使用 SmallRye 反应式消息动态发布/订阅 MQTT - Publish / Subscribe MQTT using SmallRye reactive messaging dynamically Quarkus Kafka Reactive - 反序列化子类(错误) - Quarkus Kafka Reactive - Deserializing SubClass (ERROR) 反应式消息传递 - ClassCastException 无法转换为 class io.smallrye.mutiny.Multi - reactive messaging - ClassCastException cannot be cast to class io.smallrye.mutiny.Multi 使用响应式消息传递时未在 Quarkus 上传播 Opentracing 上下文 - Opentracing context not propagated on Quarkus when using reactive messaging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM