Skip to content

Commit

Permalink
Merge pull request #47 from shellygms/master
Browse files Browse the repository at this point in the history
DocumentDB JavaSDK version 1.7.1 Release
  • Loading branch information
shellygms committed Apr 30, 2016
2 parents 7f01748 + 0d1d3b8 commit 71e5ba6
Show file tree
Hide file tree
Showing 40 changed files with 1,308 additions and 846 deletions.
3 changes: 2 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="test"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand All @@ -17,4 +18,4 @@
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
</classpath>
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To get the binaries of this library as distributed by Microsoft, ready for use w
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
</dependency>

###Option 2: Source Via Git
Expand All @@ -39,6 +39,7 @@ To download a copy of the source code, click "Download ZIP" on the right side of
* Jackson Data Mapper 1.8 (org.codehaus.jackson / jackson-mapper-asl / 1.8.5)
* JSON 20140107 (org.json / json / 20140107)
* JUnit 4.11 (junit / junit / 4.11)
* mockito 1.10.19 (org.mockito / mockito-core / 1.10.19)

Dependencies will be added automatically if Maven is used. Otherwise, please download the jars and add them to your build path.

Expand Down
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Changes in 1.7.1 : ##

- Setting Cookie policy on httpclient to ignoring server cookies as we don't use them.
- Added support to retry on throttled requests.
- Removed e.printstacktrace from source code and replaced with Logger operations as appropriate.
- Changed test code structure and added mocking framework for unit testing.

## Changes in 1.7.0 : ##

- Added support for expiring documents by setting the default time-to-live on collections and time-to-live override on documents.
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java SDK for Microsoft Azure DocumentDB</description>
<url>http://azure.microsoft.com/en-us/services/documentdb/</url>
Expand Down Expand Up @@ -166,6 +166,11 @@
<artifactId>httpcore</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
Expand Down
63 changes: 55 additions & 8 deletions src/com/microsoft/azure/documentdb/BackoffRetryUtility.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,70 @@
package com.microsoft.azure.documentdb;

/**
* A utility class to manage retries for resource throttled errors. It invokes a
* delegate function to execute the target operation. Upon error, it waits a
* period of time as defined in the ResourceThrottleRetryPolicy instance before
* re-issuing the same request. The ResourceThrottleRetryPolicy object decides
* the wait period based on the 'x-ms-retry-after-ms' response header sent by server.
*
*/
class BackoffRetryUtility {

/**
* Executes the code block in the delegate and maybe retries.
* Executes the code block in the delegate and retry if needed.
*
* @param delegate the delegate to execute.
* @param retryPolicy the retry policy.
* @param delegate
* the delegate to execute.
* @param request
* the request parameter for the execution.
* @param maxRetryAttempts
* the max number of retries.
* @throws DocumentClientException
* the original exception if retry is not applicable or if number of retries
* exceeded maxRetryAttempts.
*/
public static void execute(BackoffRetryUtilityDelegate delegate, ResourceThrottleRetryPolicy retryPolicy) {
public static DocumentServiceResponse execute(BackoffRetryUtilityDelegate delegate, DocumentServiceRequest request,
int maxRetryAttempts) throws DocumentClientException {
DocumentServiceResponse response = null;
ResourceThrottleRetryPolicy retryPolicy = new ResourceThrottleRetryPolicy(maxRetryAttempts);
while (true) {
try {
response = delegate.apply(request);
break;
} catch (DocumentClientException e) {
boolean retry = retryPolicy.shouldRetry(e);
if (!retry) {
throw e;
}

BackoffRetryUtility.delayForRetry(retryPolicy);
}
}

return response;
}

/**
* Executes the code block in the delegate and retry if needed.
*
* @param delegate
* the delegate to execute.
* @param maxRetryAttempts
* the max number of retries.
*/
public static void execute(QueryBackoffRetryUtilityDelegate delegate, int maxRetryAttempts) {
ResourceThrottleRetryPolicy retryPolicy = new ResourceThrottleRetryPolicy(maxRetryAttempts);
while (true) {
try {
delegate.apply();
break; // Break from the while loop if no exception happened.
break;
} catch (Exception e) {
boolean retry = retryPolicy.shouldRetry(e);
boolean retry = false;
if (DocumentClientException.class == e.getClass()) {
retry = retryPolicy.shouldRetry((DocumentClientException) e);
}

if (!retry) {
e.printStackTrace();
throw new IllegalStateException("Exception not retriable", e);
}

Expand All @@ -32,7 +80,6 @@ private static void delayForRetry(ResourceThrottleRetryPolicy retryPolicy) {
Thread.sleep(delay);
} catch (InterruptedException e) {
// Ignore the interruption.
e.printStackTrace();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
package com.microsoft.azure.documentdb;

/**
* The delegate class of the BackoffRetryUtility class.
* Define a delegate used by the BackoffRetryUtility to retry throttled requests
* other than query.
*
* @param request
* the request to be issued.
* @throws DocumentClientException
*/
abstract interface BackoffRetryUtilityDelegate {
abstract DocumentServiceResponse apply(DocumentServiceRequest request) throws DocumentClientException;
}

/**
* Define a delegate used by the BackoffRetryUtility to retry throttled requests
* for QueryIterable.
*
* @param request
* the request to be issued.
* @throws DocumentClientException
*/
abstract interface QueryBackoffRetryUtilityDelegate {
abstract void apply() throws Exception;
}
Loading

0 comments on commit 71e5ba6

Please sign in to comment.