2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.common.wait;
11 import java.util.concurrent.Callable;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
16 * Created by mirehak on 4/28/15.
18 public class SimpleTaskRetryLooper {
20 private static final Logger LOG = LoggerFactory.getLogger(SimpleTaskRetryLooper.class);
22 private final long tick;
23 private final int maxRetries;
26 * It retries the task passed to its method loopUntilNoException a number of
30 * sleep between steps in miliseconds
34 public SimpleTaskRetryLooper(long tick, int maxRetries) {
36 this.maxRetries = maxRetries;
39 @SuppressWarnings("checkstyle:IllegalCatch")
40 public <T> T loopUntilNoException(Callable<T> task) throws Exception {
43 Exception taskException = null;
44 for (int i = 0; i < maxRetries; i++) {
49 } catch (Exception exception) {
50 LOG.debug("looper step failed: {}", exception.getMessage());
51 taskException = exception;
56 } catch (InterruptedException e) {
57 LOG.debug("interrupted: {}", e.getMessage(), e);
58 Thread.currentThread().interrupt();
62 if (taskException != null) {
66 LOG.debug("looper step succeeded: {}", output);