diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/aggregator/AbstractMessageBarrierHandler.java b/org.springframework.integration/src/main/java/org/springframework/integration/aggregator/AbstractMessageBarrierHandler.java index 2302026766..15076f0357 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/aggregator/AbstractMessageBarrierHandler.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/aggregator/AbstractMessageBarrierHandler.java @@ -103,12 +103,14 @@ public abstract class AbstractMessageBarrierHandler> private volatile boolean autoStartup = true; - private volatile boolean initialized; - private volatile TaskScheduler taskScheduler; private volatile ScheduledFuture reaperFutureTask; + private volatile boolean initialized; + + private final Object lifecycleMonitor = new Object(); + public AbstractMessageBarrierHandler() { this.channelTemplate.setSendTimeout(DEFAULT_SEND_TIMEOUT); @@ -179,30 +181,40 @@ public abstract class AbstractMessageBarrierHandler> } } - public void afterPropertiesSet() { - this.trackedCorrelationIds = new ArrayBlockingQueue(this.trackedCorrelationIdCapacity); - if (this.autoStartup) { - this.start(); + public final void afterPropertiesSet() { + synchronized (this.lifecycleMonitor) { + if (!this.initialized) { + this.trackedCorrelationIds = new ArrayBlockingQueue(this.trackedCorrelationIdCapacity); + if (this.autoStartup) { + this.start(); + } + this.initialized = true; + } } - this.initialized = true; } public boolean isRunning() { - return this.reaperFutureTask != null; + synchronized (this.lifecycleMonitor) { + return this.reaperFutureTask != null; + } } public void start() { - if (this.isRunning()) { - return; + synchronized (this.lifecycleMonitor) { + if (this.isRunning()) { + return; + } + Assert.state(this.taskScheduler != null, "TaskScheduler must not be null"); + this.reaperFutureTask = this.taskScheduler.schedule(new PrunerTask(), + new IntervalTrigger(this.reaperInterval, TimeUnit.MILLISECONDS)); } - Assert.state(this.taskScheduler != null, "TaskScheduler must not be null"); - this.reaperFutureTask = this.taskScheduler.schedule(new PrunerTask(), new IntervalTrigger(this.reaperInterval, - TimeUnit.MILLISECONDS)); } public void stop() { - if (this.isRunning()) { - this.reaperFutureTask.cancel(true); + synchronized (this.lifecycleMonitor) { + if (this.isRunning()) { + this.reaperFutureTask.cancel(true); + } } }