diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java index 9265287a11..c6f8b1f6a7 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java @@ -487,11 +487,21 @@ public class Constants { */ public static final String REGISTRY_RETRY_PERIOD_KEY = "retry.period"; + /** + * Most retry times + */ + public static final String REGISTRY_RETRY_TIMES_KEY = "retry.times"; + /** * Default value for the period of retry interval in milliseconds: 5000 */ public static final int DEFAULT_REGISTRY_RETRY_PERIOD = 5 * 1000; + /** + * Default value for the times of retry: 3 + */ + public static final int DEFAULT_REGISTRY_RETRY_TIMES = 3; + /** * Reconnection period in milliseconds for register center */ diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/retry/AbstractRetryTask.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/retry/AbstractRetryTask.java index b299b2fd7e..fcbea5fd3c 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/retry/AbstractRetryTask.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/retry/AbstractRetryTask.java @@ -49,12 +49,23 @@ public abstract class AbstractRetryTask implements TimerTask { /** * retry period */ - protected final long retryPeriod; + final long retryPeriod; + + /** + * define the most retry times + */ + private final int retryTimes; /** * task name for this task */ - protected final String taskName; + private final String taskName; + + /** + * times of retry. + * retry task is execute in single thread so that the times is not need volatile. + */ + private int times = 1; private volatile boolean cancel; @@ -67,6 +78,7 @@ public abstract class AbstractRetryTask implements TimerTask { this.taskName = taskName; cancel = false; this.retryPeriod = url.getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD); + this.retryTimes = url.getParameter(Constants.REGISTRY_RETRY_TIMES_KEY, Constants.DEFAULT_REGISTRY_RETRY_TIMES); } public void cancel() { @@ -86,7 +98,7 @@ public abstract class AbstractRetryTask implements TimerTask { if (timer.isStop() || timeout.isCancelled() || isCancel()) { return; } - + times++; timer.newTimeout(timeout.task(), tick, TimeUnit.MILLISECONDS); } @@ -96,6 +108,11 @@ public abstract class AbstractRetryTask implements TimerTask { // other thread cancel this timeout or stop the timer. return; } + if (times > retryTimes) { + // reach the most times of retry. + logger.warn("Final failed to execute task " + taskName + ", url: " + url + ", retry " + retryTimes + " times."); + return; + } if (logger.isInfoEnabled()) { logger.info(taskName + " : " + url); }