pl.allegro.tech.hermes.consumers.consumer.batch.ByteBufferMessageBatchFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hermes-consumers Show documentation
Show all versions of hermes-consumers Show documentation
Fast and reliable message broker built on top of Kafka.
package pl.allegro.tech.hermes.consumers.consumer.batch;
import pl.allegro.tech.hermes.api.Subscription;
import pl.allegro.tech.hermes.common.exception.InternalProcessingException;
import pl.allegro.tech.hermes.common.metric.MetricsFacade;
import java.nio.ByteBuffer;
import java.time.Clock;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.UUID.randomUUID;
public class ByteBufferMessageBatchFactory implements MessageBatchFactory {
private final DirectBufferPool bufferPool;
private final Clock clock;
public ByteBufferMessageBatchFactory(int poolableSize, int maxPoolSize, Clock clock, MetricsFacade metrics) {
this.clock = clock;
this.bufferPool = new DirectBufferPool(maxPoolSize, poolableSize, true);
metrics.consumer().registerBatchBufferTotalBytesGauge(bufferPool, DirectBufferPool::totalMemory);
metrics.consumer().registerBatchBufferAvailableBytesGauge(bufferPool, DirectBufferPool::availableMemory);
}
@Override
public MessageBatch createBatch(Subscription subscription) {
try {
ByteBuffer buffer = bufferPool.allocate(subscription.getBatchSubscriptionPolicy().getBatchVolume());
switch (subscription.getContentType()) {
case JSON:
return new JsonMessageBatch(randomUUID().toString(), buffer, subscription, clock);
case AVRO:
default:
throw new UnsupportedOperationException(
"Batching is not supported yet for contentType " + subscription.getContentType());
}
} catch (InterruptedException e) {
throw new InternalProcessingException(e);
}
}
@Override
public void destroyBatch(MessageBatch batch) {
checkNotNull(batch);
bufferPool.deallocate(batch.getContent());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy