AMQP-451: Support Flushing Batch

JIRA: https://jira.spring.io/browse/AMQP-451

- Add `flush()` to support flushing any partial batch.
- Implement `Lifecycle` and invoke `flush()` when stopped.
This commit is contained in:
Gary Russell
2014-12-12 11:19:44 -05:00
parent 2f22bb6e42
commit 882e94f6f4

View File

@@ -23,6 +23,7 @@ import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.support.BatchingStrategy;
import org.springframework.amqp.rabbit.core.support.MessageBatch;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.context.Lifecycle;
import org.springframework.scheduling.TaskScheduler;
/**
@@ -38,7 +39,7 @@ import org.springframework.scheduling.TaskScheduler;
* @since 1.4.1
*
*/
public class BatchingRabbitTemplate extends RabbitTemplate {
public class BatchingRabbitTemplate extends RabbitTemplate implements Lifecycle {
private final BatchingStrategy batchingStrategy;
@@ -85,6 +86,13 @@ public class BatchingRabbitTemplate extends RabbitTemplate {
}
}
/**
* Flush any partial in-progress batches.
*/
public void flush() {
releaseBatches();
}
private synchronized void releaseBatches() {
MessageBatch batch;
while ((batch = this.batchingStrategy.releaseBatch()) != null) {
@@ -92,4 +100,18 @@ public class BatchingRabbitTemplate extends RabbitTemplate {
}
}
@Override
public void start() {
}
@Override
public void stop() {
flush();
}
@Override
public boolean isRunning() {
return true;
}
}