Spring云流MessageChannel send()始终返回true

时间:2017-12-06 15:49:45

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

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

有人可以帮忙吗?

使用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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

  

同步

     

制片人是否同步。

     

默认值:false。