Improved shutdown for BufferedRateExecutor

This commit is contained in:
Andrew Shvayka 2018-10-26 17:54:58 +03:00
parent 34c149de81
commit 18662201f1

View File

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2018 The Thingsboard Authors * Copyright © 2016-2018 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -26,6 +26,7 @@ import java.util.UUID;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -45,6 +46,7 @@ public abstract class AbstractBufferedRateExecutor<T extends AsyncTask, F extend
private final ExecutorService callbackExecutor; private final ExecutorService callbackExecutor;
private final ScheduledExecutorService timeoutExecutor; private final ScheduledExecutorService timeoutExecutor;
private final int concurrencyLimit; private final int concurrencyLimit;
private volatile boolean stopped;
protected final AtomicInteger concurrencyLevel = new AtomicInteger(); protected final AtomicInteger concurrencyLevel = new AtomicInteger();
protected final AtomicInteger totalAdded = new AtomicInteger(); protected final AtomicInteger totalAdded = new AtomicInteger();
@ -106,7 +108,10 @@ public abstract class AbstractBufferedRateExecutor<T extends AsyncTask, F extend
AsyncTaskContext<T, V> taskCtx = null; AsyncTaskContext<T, V> taskCtx = null;
try { try {
if (curLvl <= concurrencyLimit) { if (curLvl <= concurrencyLimit) {
taskCtx = queue.take(); taskCtx = queue.poll(1, TimeUnit.SECONDS);
if (taskCtx == null) {
continue;
}
final AsyncTaskContext<T, V> finalTaskCtx = taskCtx; final AsyncTaskContext<T, V> finalTaskCtx = taskCtx;
logTask("Processing", finalTaskCtx); logTask("Processing", finalTaskCtx);
concurrencyLevel.incrementAndGet(); concurrencyLevel.incrementAndGet();