Skip to content

Commit 0f14b81

Browse files
liyanzhang505huiguangjun
authored andcommitted
add retryStrategy to ClientConfiguration.
1 parent dcf2c6c commit 0f14b81

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/main/java/com/aliyun/oss/ClientConfiguration.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.aliyun.oss.common.auth.RequestSigner;
3333
import com.aliyun.oss.common.comm.IdleConnectionReaper;
3434
import com.aliyun.oss.common.comm.Protocol;
35+
import com.aliyun.oss.common.comm.RetryStrategy;
3536
import com.aliyun.oss.common.comm.SignVersion;
3637
import com.aliyun.oss.common.utils.ResourceManager;
3738
import com.aliyun.oss.common.utils.VersionInfoUtils;
@@ -102,6 +103,8 @@ public class ClientConfiguration {
102103

103104
protected long tickOffset = 0;
104105

106+
private RetryStrategy retryStrategy;
107+
105108
public ClientConfiguration() {
106109
super();
107110
AppendDefaultExcludeList(this.cnameExcludeList);
@@ -699,4 +702,26 @@ public void setTickOffset(long epochTicks) {
699702
long localTime = new Date().getTime();
700703
this.tickOffset = epochTicks - localTime;
701704
}
705+
706+
/**
707+
* Gets the retry strategy
708+
*
709+
* @return {@link RetryStrategy} object.
710+
*/
711+
public RetryStrategy getRetryStrategy() {
712+
return retryStrategy;
713+
}
714+
715+
/**
716+
* Sets the retry strategy.
717+
*
718+
* @param retryStrategy
719+
* The retryStrategy is used to check whether to retry request or not.
720+
* If it has been specified, the client will prefer this retryStrategy
721+
* than its private strategy {@link DefaultRetryStrategy}.
722+
*/
723+
public void setRetryStrategy(RetryStrategy retryStrategy) {
724+
this.retryStrategy = retryStrategy;
725+
}
726+
702727
}

src/main/java/com/aliyun/oss/internal/OSSOperation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ protected ExecutionContext createDefaultContext(HttpMethod method, String bucket
169169
if (method == HttpMethod.POST) {
170170
context.setRetryStrategy(noRetryStrategy);
171171
}
172+
173+
if (client.getClientConfiguration().getRetryStrategy() != null) {
174+
context.setRetryStrategy(client.getClientConfiguration().getRetryStrategy());
175+
}
176+
172177
context.setCredentials(credentials);
173178
return context;
174179
}

src/test/java/com/aliyun/oss/OSSClientConfigurationTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import com.aliyun.oss.common.auth.RequestSigner;
2323
import com.aliyun.oss.common.comm.RequestMessage;
24+
import com.aliyun.oss.common.comm.ResponseMessage;
25+
import com.aliyun.oss.common.comm.RetryStrategy;
2426
import junit.framework.Assert;
2527
import org.junit.Test;
2628

@@ -66,4 +68,24 @@ public void testTimeoutClientEnable() {
6668
Assert.assertTrue(configuration.isRequestTimeoutEnabled());
6769
}
6870

71+
@Test
72+
public void testUserRetryStrategy() {
73+
ClientConfiguration configuration = new ClientConfiguration();
74+
Assert.assertNull(configuration.getRetryStrategy());
75+
76+
configuration.setRetryStrategy(new TestRetryStrategy());
77+
Assert.assertNotNull(configuration.getRetryStrategy());
78+
Assert.assertTrue(configuration.getRetryStrategy().shouldRetry(null, null, null, 1));
79+
Assert.assertFalse(configuration.getRetryStrategy().shouldRetry(null, null, null, 2));
80+
}
81+
6982
}
83+
84+
85+
class TestRetryStrategy extends RetryStrategy {
86+
87+
@Override
88+
public boolean shouldRetry(Exception ex, RequestMessage request, ResponseMessage response, int retries) {
89+
return retries < 2 ? true : false;
90+
}
91+
}

0 commit comments

Comments
 (0)