aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java')
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java26
1 files changed, 3 insertions, 23 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java
index 872c61e480..acdd1ae5aa 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifier.java
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.openecomp.sdcrests.item.rest.services.catalog.notification;
import java.util.Collection;
@@ -38,21 +37,17 @@ import org.openecomp.sdcrests.item.types.ItemAction;
public class AsyncNotifier implements Notifier {
private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(1);
-
private static final int DEFAULT_NUM_OF_RETRIES = 2;
private static final long DEFAULT_INTERVAL = 5000;
-
private final BiFunction<Collection<String>, ItemAction, Callable<NextAction>> taskProducer;
private final int numberOfRetries;
private final long retryInterval;
-
AsyncNotifier(BiFunction<Collection<String>, ItemAction, Callable<NextAction>> taskProducer) {
this(taskProducer, DEFAULT_NUM_OF_RETRIES, DEFAULT_INTERVAL);
}
- AsyncNotifier(BiFunction<Collection<String>, ItemAction, Callable<NextAction>> taskProducer, int numOfRetries,
- long retryInterval) {
+ AsyncNotifier(BiFunction<Collection<String>, ItemAction, Callable<NextAction>> taskProducer, int numOfRetries, long retryInterval) {
this.taskProducer = taskProducer;
this.numberOfRetries = numOfRetries;
this.retryInterval = retryInterval;
@@ -60,30 +55,22 @@ public class AsyncNotifier implements Notifier {
@Override
public void execute(Collection<String> itemIds, ItemAction action) {
-
Callable<AsyncNotifier.NextAction> worker = taskProducer.apply(itemIds, action);
-
RetryingTask retryingTask = new RetryingTask(worker, numberOfRetries, retryInterval, EXECUTOR_SERVICE);
-
EXECUTOR_SERVICE.submit(LoggingContext.copyToCallable(retryingTask));
}
- public enum NextAction {
- RETRY, DONE
- }
+ public enum NextAction {RETRY, DONE}
static class RetryingTask implements Callable<Void> {
private static final Logger LOGGER = LoggerFactory.getLogger(RetryingTask.class);
-
private final Callable<AsyncNotifier.NextAction> worker;
private final long delay;
private final ScheduledExecutorService scheduler;
private final AtomicInteger retries;
- RetryingTask(Callable<AsyncNotifier.NextAction> worker, int numOfRetries, long delay,
- ScheduledExecutorService scheduler) {
-
+ RetryingTask(Callable<AsyncNotifier.NextAction> worker, int numOfRetries, long delay, ScheduledExecutorService scheduler) {
this.worker = Objects.requireNonNull(worker);
this.retries = new AtomicInteger(requirePositiveRetries(numOfRetries));
this.delay = requirePositiveDelay(delay);
@@ -91,38 +78,31 @@ public class AsyncNotifier implements Notifier {
}
private int requirePositiveRetries(int number) {
-
if (number < 1) {
throw new IllegalArgumentException("Number of retries must be positive");
}
-
return number;
}
private long requirePositiveDelay(long number) {
-
if (number < 1) {
throw new IllegalArgumentException("Delay must be positive");
}
-
return number;
}
@Override
public Void call() throws Exception {
-
NextAction next = worker.call();
if (next == NextAction.DONE) {
LOGGER.debug("Task successful: {}. Not going to retry", worker);
return null;
}
-
int attempts = retries.decrementAndGet();
if (attempts < 1) {
LOGGER.warn("Exhausted number of retries for task {}, exiting", worker);
return null;
}
-
scheduler.schedule(this, delay, TimeUnit.MILLISECONDS);
return null;
}