Kafka的JavaAPi生產者


package kafkajavaapi;

import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.RecordMetadata;

class DemoCallBack implements Callback {

    private final long startTime;
    private final int key;
    private final String message;

    public DemoCallBack(long startTime, int key, String message) {
        this.startTime = startTime;
        this.key = key;
        this.message = message;
    }

    /**
     * A callback method the user can implement to provide asynchronous handling of request completion. This method will
     * be called when the record sent to the server has been acknowledged. When exception is not null in the callback,
     * metadata will contain the special -1 value for all fields except for topicPartition, which will be valid.
     *
     * @param metadata  The metadata for the record that was sent (i.e. the partition and offset). Null if an error
     *                  occurred.
     * @param exception The exception thrown during processing of this record. Null if no error occurred.
     */
    public void onCompletion(RecordMetadata metadata, Exception exception) {
        long elapsedTime = System.currentTimeMillis() - startTime;
        if (metadata != null) {
            System.out.println(
                    "message(" + key + ", " + message + ") sent to partition(" + metadata.partition() +
                            "), " +
                            "offset(" + metadata.offset() + ") in " + elapsedTime + " ms");
        } else {
            exception.printStackTrace();
        }
    }
}







package kafkajavaapi;

/***
 * kafka常用配置文件
 */
public class KafkaProperties {

    public static final String ZK_List="t1:2181,t2:2181,t3:2181";


    public static final String BROKER_LIST = "t1:9092,t2:9092,t3:9092";


    public static final String TOPIC = "helloworld";
    public static final String KAFKA_SERVER_URL = "localhost";
    public static final int KAFKA_SERVER_PORT = 9092;
    public static final int KAFKA_PRODUCER_BUFFER_SIZE = 64 * 1024;
    public static final int CONNECTION_TIMEOUT = 100000;
    public static final String TOPIC2 = "topic2";
    public static final String TOPIC3 = "topic3";
    public static final String CLIENT_ID = "SimpleConsumerDemoClient";

    private KafkaProperties() {}



}













package kafkajavaapi;


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

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**Kafka 生成者*/
public class MyKafkaProducer extends Thread {
    private final String topic;
    private final Boolean isAsync;

    //用來生產消息的類
private final KafkaProducer<Integer,String> producer;

    public MyKafkaProducer(String topic , Boolean isAsync){
        Properties props = new Properties();

        //ProducerConfig.BOOTSTRAP_SERVERS_CONFIG = bootstrap.servers
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,KafkaProperties.BROKER_LIST);
        //ProducerConfig.CLIENT_ID_CONFIG = client.id
        props.put(ProducerConfig.CLIENT_ID_CONFIG,"TestMyKafkaProducer");
        //key的序列化類
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,IntegerSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        producer = new KafkaProducer<>(props);
        this.topic = topic;
        this.isAsync = isAsync;

    }


    public void run(){
        int messageNo = 1;

        while (true){
            String messageStr = "Message_" + messageNo;

            long startTime = System.currentTimeMillis();

            if(isAsync){ //send asynchronously 異步發送
                producer.send(new ProducerRecord<>(topic,messageNo,messageStr),new DemoCallBack(startTime , messageNo ,messageStr));


            }else{//send synchronously
                try {
                    producer.send(new ProducerRecord<>(topic,messageNo,messageStr)).get();
                    System.out.println("發送------>Sent message: (" + messageNo + ", " + messageStr + ")");
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
            ++messageNo;
        }
    }







}








package kafkajavaapi;

public class TestMykafkaProducer {
    public static void main(String[] args) {

        MyKafkaProducer myKafkaProducer = new MyKafkaProducer(KafkaProperties.TOPIC, false);

        myKafkaProducer.run();


        System.out.println("hello world");
    }


}


測試結果

Message_1
Message_2
Message_3
Message_4
Message_5
Message_6
Message_7
Message_8
Message_10
Message_9
Message_11
Message_12
Message_13
Message_14
Message_15
Message_16
Message_17
Message_18
Message_19
Message_20
Message_21
Message_22
Message_23
Message_24
Message_25
Message_26
Message_27
Message_28
Message_29
Message_30
Message_31
Message_32
Message_33
Message_34
Message_35
Message_36
Message_37
Message_38
Message_39
Message_40
Message_41
Message_42
Message_44
Message_45
Message_46
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章