import java.util.Properties; import net.sf.json.JSONObject; import kafka.javaapi.producer.Producer; import kafka.javaapi.producer.ProducerData; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; import kafka.serializer.StringEncoder; //利用线程池 调用类的静态函数发送消息 但是发布出去 不知道什么原因 public class SendOid2Kafka { private static Producer producer = createProducer(); public static void sendMessage2CMS(JSONObject message) { producer.send(new KeyedMessage("novel.review.topic", message)); System.out.println("send"); //执行不到这里 消息没发送出去 } private static Producer createProducer() { Properties properties = new Properties(); properties.put("zookeeper.connect","*"); properties.put("serializer.class", StringEncoder.class.getName()); properties.put("metadata.broker.list","*"); properties.put("zookeeper.session.timeout.ms", "15000"); return new Producer(new ProducerConfig(properties)); } }
改为发送String 就发送出去了
JSONObject就发送不出去
想发JSONObject怎么办???
将JSONObject转换成JSON字符串试试。
用阿里的JSONObject转成字符串发送,接收端再人手转成自己要的类