Source:
public void syncPostBatch(final String topicId, final List<BatchItem> batch) throws EventPublishingException { batch.forEach(item -> Preconditions.checkNotNull( item.getPartition(), "BatchItem partition can't be null at the moment of publishing!")); final Producer<String, String> producer = kafkaFactory.takeProducer(); final Map<BatchItem, CompletableFuture<Exception>> sendFutures = new HashMap<>(); try { for (final BatchItem item : batch) { item.setStep(EventPublishingStep.PUBLISHING); sendFutures.put(item, publishItem(producer, topicId, item)); } final CompletableFuture<Void> multiFuture = CompletableFuture.allOf( sendFutures.values().toArray(new CompletableFuture<?>[sendFutures.size()])); multiFuture.get(createSendTimeout(), TimeUnit.MILLISECONDS); final boolean needReset = sendFutures.entrySet().stream().filter( entry -> isExceptionShouldLeadToReset(entry.getValue().getNow(null))) .findAny().isPresent(); if (needReset) { kafkaFactory.terminateProducer(producer); } } catch (final TimeoutException ex) { failUnpublished(batch, "timed out"); throw new EventPublishingException("Error publishing message to kafka", ex); } catch (final ExecutionException ex) { failUnpublished(batch, "internal error"); throw new EventPublishingException("Error publishing message to kafka", ex); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); failUnpublished(batch, "interrupted"); throw new EventPublishingException("Error publishing message to kafka", ex); } finally { kafkaFactory.releaseProducer(producer); } final boolean atLeastOneFailed = batch.stream() .anyMatch(item -> item.getResponse().getPublishingStatus() == EventPublishingStatus.FAILED); if (atLeastOneFailed) { failUnpublished(batch, "internal error"); throw new EventPublishingException("Error publishing message to kafka"); } }

Target:
public void syncPostBatch(final String topicId, final List<BatchItem> batch) throws EventPublishingException { batch.forEach(item -> Preconditions.checkNotNull( item.getPartition(), "BatchItem partition can't be null at the moment of publishing!")); final Producer<String, String> producer = kafkaFactory.takeProducer(); final Map<BatchItem, CompletableFuture<Exception>> sendFutures = new HashMap<>(); try { for (final BatchItem item : batch) { item.setStep(EventPublishingStep.PUBLISHING); sendFutures.put(item, publishItem(producer, topicId, item)); } final CompletableFuture<Void> multiFuture = CompletableFuture.allOf( sendFutures.values().toArray(new CompletableFuture<?>[sendFutures.size()])); multiFuture.get(createSendTimeout(), TimeUnit.MILLISECONDS); final Optional<Exception> needReset = sendFutures.entrySet().stream() .filter(entry -> isExceptionShouldLeadToReset(entry.getValue().getNow(null))) .map(entry -> entry.getValue().getNow(null)) .findAny(); if (needReset.isPresent()) { LOG.info("Terminating producer while publishing to topic " + topicId + " because of unrecoverable exception", needReset.get()); kafkaFactory.terminateProducer(producer); } } catch (final TimeoutException ex) { failUnpublished(batch, "timed out"); throw new EventPublishingException("Error publishing message to kafka", ex); } catch (final ExecutionException ex) { failUnpublished(batch, "internal error"); throw new EventPublishingException("Error publishing message to kafka", ex); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); failUnpublished(batch, "interrupted"); throw new EventPublishingException("Error publishing message to kafka", ex); } finally { kafkaFactory.releaseProducer(producer); } final boolean atLeastOneFailed = batch.stream() .anyMatch(item -> item.getResponse().getPublishingStatus() == EventPublishingStatus.FAILED); if (atLeastOneFailed) { failUnpublished(batch, "internal error"); throw new EventPublishingException("Error publishing message to kafka"); } }