kafka复习:(2)客户端发送消息并异步处理返回结果

package com.cisdi.dsp.modules.metaAnalysis.rest.kafka2023;



import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class KafkaTest02 {
    public static void main(String[] args) {
        System.out.println(StringSerializer.class.getName());
        Properties properties= new Properties();

        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"xxx.xxx.xxx.xxx:9092");
        KafkaProducer kafkaProducer=new KafkaProducer(properties);
        ProducerRecord producerRecord=new ProducerRecord<>("paul","hello ,Miss Cloud");
        kafkaProducer.send(producerRecord, (RecordMetadata metadata, Exception exception) -> {
                System.out.println(metadata.offset());
                System.out.println(metadata.topic());
            } );


        //kafkaProducer.close();
        kafkaProducer.close(Duration.ofMillis(5000));
    }
}


你可能感兴趣的:(kafka,kafka,分布式)