例子如下:

 @Autowired
    KafkaTemplate<String, String> kafkaTemplate;

    public void testKafkaTemplate () {
        ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("topic", "{\"key\": \"value\"}");

        CompletableFuture<SendResult<String, String>> completable = future.completable();
        completable.whenCompleteAsync((n, e) -> {
            if (null != e) {
                System.out.println("发送报错了");
            } else {
                System.out.println("发送成功了!");
            }
        });
    }

    public void testKafkaTemplate1() {
        ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("topic", "{\"key\": \"value\"}");
        future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
            @Override
            public void onFailure(Throwable throwable) {
                //
            }

            @Override
            public void onSuccess(SendResult<String, String> stringStringSendResult) {
                //
            }
        });
    }
Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐