无法从春季云流kafka捕获RecordTooLargeException

时间:2019-08-29 02:59:00

标签: spring-boot apache-kafka spring-cloud

我正在使用Spring cloud stream kafka作为绑定器。当我的消息太大时,我得到错误

  ERROR o.s.k.s.LoggingProducerListener - Exception thrown when sending a message with key='null' and payload='{123, 34, 105, 100, 34, 58, 34, 115, 105, 110, 103, 97, 112, 111, 114, 101, 104, 101, 114, 97, 108, ...' to topic page:
    org.apache.kafka.common.errors.RecordTooLargeException: The message is 4711755 bytes when serialized which is larger than the maximum request size you have configured with the max.request.size configuration.

这是我下面用于发送消息的springboot代码

 private BinderAwareChannelResolver resolver;

    boolean isSent = this.resolver.resolveDestination(this.topic)
                    .send(message);

由于出现错误,我应该能够在springboot代码中捕获RecordTooLargeException。但是,它没有被捕获,并且代码继续。 isSent也返回为“ true”。它不应该返回假吗?我如何捕获此错误并进行处理?谢谢

1 个答案:

答案 0 :(得分:0)

如果错误是内部错误,并且是由不受应用程序代码直接控制的任何线程引发的,则可能要使用UncaughtExceptionHandler

Thread.setDefaultUncaughtExceptionHandler((whichThread, whatException) -> {
            if (whatException.getClass()
                    .equals(org.apache.kafka.common.errors.RecordTooLargeException.class) ||
                    whatException.getCause().getClass()
                            .equals(org.apache.kafka.common.errors.RecordTooLargeException.class)) {
                // do something
            }
            else if (or) else {
            // others
            }
        });

但是上面的代码只是让您了解该异常。 如果收到的记录大于大小,则应在代理和主题上进行更改。

max.message.bytes (topic)
message.max.bytes (broker)

PS:您可能想同时使用getClass().equals()instanceof来代替isAssignableFrom()

更新

我在KafkaProducer中遇到了相同的错误,并且在Callback中返回了异常

producer.send(new ProducerRecord<>(topic, key, value), (recordMetadata, exception) -> {
    if (exception instanceof RecordTooLargeException) {
          // handling code
    }
});

您还可以看到实现Spring kafka asynchronous callback

相关问题