com.solace.spring.cloud.stream.binder.inbound.BatchCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-cloud-stream-binder-solace Show documentation
Show all versions of spring-cloud-stream-binder-solace Show documentation
A Spring Cloud Stream Binder implementation using the Solace Java API (JCSMP)
package com.solace.spring.cloud.stream.binder.inbound;
import com.solace.spring.cloud.stream.binder.properties.SolaceConsumerProperties;
import com.solace.spring.cloud.stream.binder.util.MessageContainer;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
/**
* Collector which batches message.
* Message batches can be retrieved from this collector only when batching requirements have been met.
*/
@Slf4j
public class BatchCollector {
private final SolaceConsumerProperties consumerProperties;
private final List batchedMessages;
private long timeSentLastBatch = System.currentTimeMillis();
private UUID currentFlowReceiverReferenceId;
public BatchCollector(SolaceConsumerProperties consumerProperties) {
this.consumerProperties = consumerProperties;
this.batchedMessages = new ArrayList<>(consumerProperties.getBatchMaxSize());
}
/**
* Add message to batch
*
* @param messageContainer message container
*/
public void addToBatch(MessageContainer messageContainer) {
if (messageContainer == null) {
return;
}
batchedMessages.add(messageContainer);
UUID flowReceiverReferenceId = messageContainer.getFlowReceiverReferenceId();
if (currentFlowReceiverReferenceId != null && !currentFlowReceiverReferenceId.equals(flowReceiverReferenceId)) {
if (log.isTraceEnabled()) {
log.trace(String.format("Added a message to batch, but its flow receiver reference ID was %s, " +
"expected %s. Pruning stale messages from batch...",
flowReceiverReferenceId, currentFlowReceiverReferenceId));
}
pruneStaleMessages();
}
currentFlowReceiverReferenceId = flowReceiverReferenceId;
}
/**
* Checks if batch is eligible to be collected.
*
* @return true if available (batch may be empty).
*/
public boolean isBatchAvailable() {
return isBatchAvailableInternal() && (!pruneStaleMessages() || isBatchAvailableInternal());
}
public boolean isBatchAvailableInternal() {
if (batchedMessages.size() < consumerProperties.getBatchMaxSize()) {
long batchTimeDiff = System.currentTimeMillis() - timeSentLastBatch;
if (consumerProperties.getBatchTimeout() == 0 || batchTimeDiff < consumerProperties.getBatchTimeout()) {
if (log.isTraceEnabled()) {
log.trace(String.format("Collecting batch... Size: %s, Time since last batch: %s ms",
batchedMessages.size(), batchTimeDiff));
}
return false;
} else if (log.isTraceEnabled()) {
log.trace(String.format(
"Batch timeout reached