Quarkus Kafka流/响应消息反序列化异常

时间:2020-07-17 19:53:10

标签: java apache-kafka apache-kafka-streams quarkus smallrye-reactive-messaging

嘿,所以我正在尝试使用Kafka Streams和MP Reactive Messaging来读取Kafka主题,然后再产生回去。

Kafka Streams错误-

org.apache.kafka.streams.errors.StreamsException: Deserialization exception handler is set to fail upon a deserialization error. If you would rather have the streaming pipeline continue after a deserialization error, please set the default.deserialization.exception.handler appropriately.

Reactive Messaging错误是相似的,但是基本上,将消息反序列化的POJO看起来像这样-

    public class FinancialMessage {
    
    public String user_id;
    public String stock_symbol;
    public String exchange_id;
    public String trade_type;
    public String date_created;
    public String date_submitted;
    public int quantity;
    public double stock_price;
    public double total_cost;
    public int institution_id;
    public int country_id;
    public boolean compliance_services;
    public boolean technical_validation;
    public boolean schema_validation;
    public boolean business_validation;
    public boolean trade_enrichment;
   }

请注意,有一个默认的空构造函数和一个包含所有字段的构造函数。

import java.time.Instant;

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

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.GlobalKTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.Produced;
import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
import org.apache.kafka.streams.state.Stores;

import io.quarkus.kafka.client.serialization.JsonbSerde;
import io.quarkus.kafka.client.serialization.JsonbSerializer;


@ApplicationScoped
public class ComplianceTopology {

    private static final String KTABLE_TOPIC = "kstreams-ktable-topic";
    private static final String INCOMING_TOPIC = "kstreams-incoming-test";
    private static final String OUTGOING_TOPIC = "kstreams-outgoing-test";


    @Produces
    public Topology buildTopology() {

        StreamsBuilder builder = new StreamsBuilder();

        JsonbSerde<FinancialMessage> financialMessageSerde = new JsonbSerde<>(FinancialMessage.class);

        builder.stream(
            INCOMING_TOPIC,
            Consumed.with(Serdes.Integer(), financialMessageSerde)
        )
        .filter(
            (key, message) -> checkCompliance(message)
        )
        .mapValues (
            checkedMessage -> performComplianceCheck(checkedMessage)
        )
        .to (
            INCOMING_TOPIC,
            Produced.with(Serdes.Integer(), financialMessageSerde)
        );  
        
        return builder.build();
    }

    public boolean checkCompliance (FinancialMessage rawMessage) {
        return (rawMessage.compliance_services);
    }

    public FinancialMessage performComplianceCheck(FinancialMessage checkedMessage) {
        checkedMessage.compliance_services = false;

        return checkedMessage;
    }

}

但是,我想它被称为“毒药”,但是MQ产生的一条带有“ Aloha”有效载荷的消息会破坏它,而我无法将其反序列化。我猜这是因为'Aloha'不被识别为字符串,因为它是单引号。由于数据是通过MQ发送的,因此我无法访问该数据的发送方式。有没有一种方法可以跳过处理此不可反序列化的消息,而仅继续执行该主题的处理?

1 个答案:

答案 0 :(得分:0)

如错误消息中所示

please set the default.deserialization.exception.handler appropriately

您可以配置其他反序列化异常处理程序,以跳过无法反序列化的消息。

查看文档以获取更多详细信息:https://kafka.apache.org/documentation/streams/developer-guide/config-streams.html#default-deserialization-exception-handler