我的大数据之旅-JAVA代码向KAFKA发送消息,带回调

maven pom配置查看上一篇文章

我的大数据之旅-JAVA代码向KAFKA发送消息

package com.fengling;


import org.apache.kafka.clients.producer.*;
import org.junit.Before;
import org.junit.Test;

import java.util.Properties;

public class KafkaProducerTestWithCallBack {

    private Properties props;
    private Producer producer;


    @Before
    public void init() {
        props = new Properties();
        props.put("bootstrap.servers", "hadoop129:9092,hadoop130:9092,hadoop131:9092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    }

    @Test
    public void produce() {
        System.out.println("begin produce");
        connectKafka();
        sendMsg();
        System.out.println("finish produce");
    }

    private void connectKafka() {
        System.out.println("create a connection!");
        producer = new KafkaProducer(props);
    }

    private void sendMsg() {
        for (int i = 1000; i < 5000; i++) {
            String msg = "西藏318,2020我们一起走!!! 消息序号:" + i;
            producer.send(new ProducerRecord("we318", msg), new Callback() {
                @Override
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    System.out.println("回调信息 --> offset = " + metadata.offset() + " partition = " + metadata.partition());
                }
            });

            System.out.println("send one msg = " + msg);
            try {
                Thread.sleep(1000 * 2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}

主要是加了一个回调方法:打印了offset、partition

运行效果如下:

我的大数据之旅-JAVA代码向KAFKA发送消息,带回调_第1张图片

你可能感兴趣的:(大数据)