【问题标题】:Spring cloud stream MessageChannel send() always return trueSpring Cloud Stream MessageChannel send() 总是返回true
【发布时间】:2017-12-06 15:49:44
【问题描述】:

我正在使用 Spring 云流,我想保存消息并在 Kafka 服务器消失时重试在主题上发布它们,但即使 Kafka/Zookeeper 服务器停止,MessageChannel send() 方法也总是返回 true .

有人可以帮忙吗?

更新 application.yml 内容:

spring:
    cloud:
        stream:
            kafka:
                binder:
                    brokers: localhost
                    zk-nodes: localhost
                    mode: raw
                bindings:
                    output:
                        producer:
                            sync: true
            bindings:
                output:
                    destination: topic-notification
                    content-type: application/json

代码:

@Service
public class SendToKafka {
    private Logger log = LoggerFactory.getLogger(SendToKafka.class);

    @Autowired
    Source source;

    @Autowired
    NotificationFileService notificationFileService;

    public void send(NotificationToResendDTO notification){
        try {
            CompletableFuture.supplyAsync(() -> notification)
                .thenAcceptAsync(notif -> {
                    boolean resp = source.output().send(MessageBuilder.withPayload(notif).build());
                    log.info(" ======== kafka server response === " + resp);

                    if (!resp){
                        log.info(" ======== failed to send the notification" + notification);
                        // save failed notification
                        notificationFileService.writeTofile(notification);
                    }
                }).get();
        } catch (InterruptedException | ExecutionException e) {
            log.info(" ======== failed to send the notification with exception" + notification);
            // save failed notification
            notificationFileService.writeTofile(notification);
            e.printStackTrace();
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    Kafka 默认是异步的;您需要将sync 设置为true;见binder producer properties

    同步

    生产者是否同步。

    默认值:假。

    【讨论】:

    • 我试过了,但生产者仍然返回 true。
    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 2017-12-25
    • 1970-01-01
    • 2019-12-22
    • 2019-07-13
    • 2017-04-13
    • 2013-08-06
    • 2017-05-10
    相关资源
    最近更新 更多