@Override public void close() throws IOException { // ~ wait for all pending futures to finish if (LOG.isDebugEnabled()) { final int nItems = fs.size(); long start = System.nanoTime(); waitPendingConfirms(); long end = System.nanoTime(); LOG.debug("Finished waiting for confirmation of {} items in {}ms", nItems, TimeUnit.NANOSECONDS.toMillis(end - start)); } else { waitPendingConfirms(); } }
@Override public Writer<Pair<byte[], byte[]>> openWriter(int partitionId) { String cacheKey = brokers; Producer<byte[], byte[]> producer = PRODUCERS.get(cacheKey); if (producer == null) { // ~ ok, let's create a new producer (this may take some time) final Producer<byte[], byte[]> p = KafkaUtils.newProducer(brokers, config); // ~ now, let's try to store it in our global cache final Producer<byte[], byte[]> p1 = PRODUCERS.putIfAbsent(cacheKey, p); if (p1 == null) { producer = p; } else { // ~ looks like somebody managed to create concurrently a new // producer in between and store it quicker into the global cache producer = p1; // ~ must close the created one to avoid leaking resources! p.close(); } } final List<PartitionInfo> partitions = producer.partitionsFor(topic); return new ProducerWriter(producer, topic, partitionId % partitions.size()); }