com.github.danielwegener.logback.kafka.delivery.BlockingDeliveryStrategy Maven / Gradle / Ivy
package com.github.danielwegener.logback.kafka.delivery;
import ch.qos.logback.core.spi.ContextAwareBase;
import org.apache.kafka.clients.producer.BufferExhaustedException;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* DeliveryStrategy that waits on the producer if the output buffer is full.
* The wait timeout is configurable with {@link BlockingDeliveryStrategy#setTimeout(long)}
* @since 0.0.1
* @deprecated Use {@link AsynchronousDeliveryStrategy} instead.
*/
@Deprecated
public class BlockingDeliveryStrategy extends ContextAwareBase implements DeliveryStrategy {
private long timeout = 0L;
@Override
public boolean send(Producer producer, ProducerRecord record, E event, FailedDeliveryCallback failureCallback) {
try {
final Future future = producer.send(record);
if (timeout > 0L) future.get(timeout, TimeUnit.MILLISECONDS);
else if (timeout == 0) future.get();
return true;
}
catch (InterruptedException e) { return false; }
catch (BufferExhaustedException | ExecutionException | CancellationException | TimeoutException e) {
failureCallback.onFailedDelivery(event, e);
}
return false;
}
public long getTimeout() {
return timeout;
}
/**
* Sets the timeout for waits on full consumers.
*
* - {@code timeout > 0}: Wait for {@code timeout} milliseconds
* - {@code timeout == 0}: Wait infinitely
*
* @param timeout a timeout in {@link TimeUnit#MILLISECONDS}.
*/
public void setTimeout(long timeout) {
this.timeout = timeout;
}
}