|
|
|
@ -17,6 +17,7 @@ package org.thingsboard.server.service.queue.ruleengine; |
|
|
|
|
|
|
|
import lombok.Getter; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.Setter; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
|
|
|
import org.thingsboard.server.gen.transport.TransportProtos; |
|
|
|
@ -24,7 +25,6 @@ import org.thingsboard.server.queue.TbQueueConsumer; |
|
|
|
import org.thingsboard.server.queue.common.TbProtoQueueMsg; |
|
|
|
|
|
|
|
import java.util.Set; |
|
|
|
import java.util.concurrent.CountDownLatch; |
|
|
|
import java.util.concurrent.Future; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@ -37,13 +37,8 @@ public class TbQueueConsumerTask { |
|
|
|
@Getter |
|
|
|
private final TbQueueConsumer<TbProtoQueueMsg<TransportProtos.ToRuleEngineMsg>> consumer; |
|
|
|
|
|
|
|
@Setter |
|
|
|
private Future<?> task; |
|
|
|
private CountDownLatch completionLatch; |
|
|
|
|
|
|
|
public void setTask(Future<?> task) { |
|
|
|
this.completionLatch = new CountDownLatch(1); |
|
|
|
this.task = task; |
|
|
|
} |
|
|
|
|
|
|
|
public void subscribe(Set<TopicPartitionInfo> partitions) { |
|
|
|
log.trace("[{}] Subscribing to partitions: {}", key, partitions); |
|
|
|
@ -53,23 +48,18 @@ public class TbQueueConsumerTask { |
|
|
|
public void initiateStop() { |
|
|
|
log.debug("[{}] Initiating stop", key); |
|
|
|
consumer.stop(); |
|
|
|
if (isRunning()) { |
|
|
|
task.cancel(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void awaitCompletion() { |
|
|
|
log.trace("[{}] Awaiting finish", key); |
|
|
|
if (isRunning()) { |
|
|
|
try { |
|
|
|
if (!completionLatch.await(30, TimeUnit.SECONDS)) { |
|
|
|
task = null; |
|
|
|
throw new IllegalStateException("timeout of 30 seconds expired"); |
|
|
|
} |
|
|
|
task.get(30, TimeUnit.SECONDS); |
|
|
|
log.trace("[{}] Awaited finish", key); |
|
|
|
} catch (Exception e) { |
|
|
|
log.warn("[{}] Failed to await for consumer to stop", key, e); |
|
|
|
} |
|
|
|
task = null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -77,9 +67,4 @@ public class TbQueueConsumerTask { |
|
|
|
return task != null; |
|
|
|
} |
|
|
|
|
|
|
|
public void finished() { |
|
|
|
completionLatch.countDown(); |
|
|
|
task = null; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|