public KafkaPusher(String brokers, String topic) { this.closer = Closer.create(); this.topic = topic; Properties props = new Properties(); props.put("metadata.broker.list", brokers); props.put("serializer.class", "kafka.serializer.DefaultEncoder"); props.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(props); this.producer = createProducer(config); }
@Override public void close() throws IOException { super.close(); }
@Test public void test() throws IOException { KafkaPusher pusher = new KafkaPusher("localhost:" + kafkaPort, TOPIC); String msg1 = "msg1"; String msg2 = "msg2"; pusher.pushMessages(Lists.newArrayList(msg1.getBytes(), msg2.getBytes())); try { Thread.sleep(1000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } assert(iterator.hasNext()); Assert.assertEquals(new String(iterator.next().message()), msg1); assert(iterator.hasNext()); Assert.assertEquals(new String(iterator.next().message()), msg2); pusher.close(); }
public KafkaPusher(String brokers, String topic) { this.closer = Closer.create(); this.topic = topic; Properties props = new Properties(); props.put("metadata.broker.list", brokers); props.put("serializer.class", "kafka.serializer.DefaultEncoder"); props.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(props); this.producer = createProducer(config); }