使用 Kafka 生成消息
import java.util.Date;
import java.util.Properties;
import kafka.common.FailedToSendMessageException;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class KafkaProducer {
private static Producer<String, String> producer;
public KafkaProducer()
{
Properties props = new Properties();
props.put("metadata.broker.list","localhost:2181");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("request.required.acks","1");
ProducerConfig config = new ProducerConfig(props);
producer = new Producer<String,String>(config);
}
public static void main(String[] args)
{
if(args.length<2)
{
System.err.println("Usage: KafkaProducer TopicName MessageCount");
System.exit(0);
}
String topic = args[0];
int messageCount = Integer.parseInt(args[1]);
KafkaProducer kafka = new KafkaProducer();
kafka.publishMessage(topic,messageCount);
}
private void publishMessage(String topic, int messageCount)
{
for(int mcount=0;mcount<messageCount;mcount++)
{
String runtime = new Date().toString();
String msg = "Message Published Time -" + runtime;
System.out.println(msg);
KeyedMessage<String,String> data = new KeyedMessage<String,String>(topic,msg);
producer.send(data);
}
producer.close();
}
}
在使用 eclipse 运行这个程序时,我得到了以下异常:
log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Message Published Time -Fri Jul 10 13:05:20 IST 2015
Exception in thread "main" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90)
at kafka.producer.Producer.send(Producer.scala:77)
at kafka.javaapi.producer.Producer.send(Producer.scala:33)
at KafkaProducer.publishMessage(KafkaProducer.java:52)
at KafkaProducer.main(KafkaProducer.java:39)
Zookeeper 服务启动,Broker 启动,Topic 创建。消费者也准备好了。
有人可以帮我解决这个问题吗?