kafka producer发送消息send报错?

幽影√空竹~ 发表于: 2018-01-16   最后更新时间: 2018-01-17 10:32:34   4,836 游览

idea写的producer发送消息给linux中的消费者,zookeeper.kafka.消费者都以启动,但是send报错:

代码信息:

public class ProducerKafka extends Thread{
    private final Producer<Integer, String> producer;
    private final String topic;
    private final Properties prop = new Properties();

    public ProducerKafka(String topic) {
//        prop.put("zookeeper.connect", "192.168.111.128:2181");//声明zk
        prop.put("serializer.class", StringEncoder.class.getName());
        prop.put("metadata.broker.list", "192.168.111.128:9092");// 声明kafka broker
        producer = new Producer<Integer, String>(new ProducerConfig(prop));
        System.out.println(111);
        this.topic = topic;
    }
    public void run(){
        int messageNo = 1;
        while (true){
            String messageStr = new String("Message_"+messageNo);
            System.out.println("Send:"+messageStr);
            KeyedMessage<Integer, String> keyedMessage = new KeyedMessage<Integer, String>(topic,messageStr);
            producer.send(keyedMessage);
            messageNo++;
            try{
                sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ProducerKafka producerKafka = new ProducerKafka("test");
        producerKafka.start();
    }
}

报错信息:

Exception in thread "Thread-0" java.util.EmptyStackException

    at ProducerKafka.run(ProducerKafka.java:27)
    Process finished with exit code 0
发表于 2018-01-16
添加评论

打个断点,跟踪下27行,空了

12345 -> 半兽人 6年前

为什么会这样?

你的答案

查看kafka相关的其他问题或提一个您自己的问题