Skip to content

Commit 2366667

Browse files
Merge pull request #6 from PraneethChandraThota/retry-logic
LU-47 Java Retry Logic
2 parents dd034e1 + fc25912 commit 2366667

File tree

13 files changed

+415
-44
lines changed

13 files changed

+415
-44
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ repositories {
5454

5555
You can just drop the jar file in.
5656

57-
[injectionApi-1.1.1.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.1.1/injectionApi-1.1.1.jar)
57+
[injectionApi-1.2.1.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.2.1/injectionApi-1.2.1.jar)
5858

59-
[injectionApi-1.1.1-sources.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.1.1/injectionApi-1.1.1-sources.jar)
59+
[injectionApi-1.2.1-sources.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.2.1/injectionApi-1.2.1-sources.jar)
6060

61-
[injectionApi-1.1.1-javadoc.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.1.1/injectionApi-1.1.1-javadoc.jar)
61+
[injectionApi-1.2.1-javadoc.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.2.1/injectionApi-1.2.1-javadoc.jar)
6262

6363
Alternately, you can simply [clone this repository](https://github.com/socketlabs/socketlabs-java.git) directly to include the source code in your project.
6464

@@ -191,6 +191,9 @@ This example demonstrates how to embed an image in your message.
191191
### [Basic send with a web proxy](https://github.com/socketlabs/socketlabs-java/tree/master/examples/src/main/java/examples/basic/BasicSendWithProxy.java)
192192
This example demonstrates how to use a proxy with your HTTP client.
193193

194+
### [Basic send with retry enabled](https://github.com/socketlabs/socketlabs-java/tree/master/examples/src/main/java/examples/basic/BasicSendWithRetry.java)
195+
This example demonstrates how to use the retry logic with your HTTP client.
196+
194197
### [Basic send with invalid file attachment](https://github.com/socketlabs/socketlabs-java/tree/master/examples/src/main/java/examples/basic/invalid/BasicSendWithInvalidAttachment.java)
195198
This example demonstrates the results of attempting to do a send with an invalid attachment.
196199

@@ -229,6 +232,7 @@ For more information about AMP please see [AMP Project](https://amp.dev/document
229232

230233
<a name="version"></a>
231234
# Version
235+
* 1.2.1 - Adding optional retry logic for Http requests. If configured, the request will retry when certain 500 errors occur (500, 502, 503, 504)
232236
* 1.1.1 - Adding request timeout value on the client for Http requests
233237
* 1.1.0 - Adds Amp Html Support
234238
* 1.0.0 - Initial Release

examples/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ dependencies {
1616
testCompile group: 'junit', name: 'junit', version: '4.12'
1717
compile 'com.fasterxml.jackson.core:jackson-core:2.11.0'
1818
compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
19-
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.1.1'
19+
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.2.1 '
2020
}

examples/src/main/java/examples/Main.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,27 @@ private static void DisplayTheMenu() {
5656
System.out.println(" 7: Basic Send With Embedded Image ");
5757
System.out.println(" 8: Basic Send With Proxy ");
5858
System.out.println(" 9: Basic Async Send ");
59-
System.out.println(" 10: Basic Send Complex Example ");
59+
System.out.println(" 10: Basic Async With Retry");
60+
System.out.println(" 11: Basic Send With Retry ");
61+
System.out.println(" 12: Basic Send Complex Example ");
6062
System.out.println();
6163
System.out.println(" Validation Error Handling Examples: ");
62-
System.out.println(" 11: Basic Send With Invalid Attachment");
63-
System.out.println(" 12: Basic Send With Invalid From ");
64-
System.out.println(" 13: Basic Send With Invalid Recipients ");
64+
System.out.println(" 13: Basic Send With Invalid Attachment");
65+
System.out.println(" 14: Basic Send With Invalid From ");
66+
System.out.println(" 15: Basic Send With Invalid Recipients ");
6567
System.out.println();
6668
System.out.println(" Bulk Send Examples: ");
67-
System.out.println(" 14: Bulk Send ");
68-
System.out.println(" 15: Bulk Send With MergeData ");
69-
System.out.println(" 16: Bulk Send With ASCII Charset And Merge Data ");
70-
System.out.println(" 17: Bulk Send From DataSource With Merge Data ");
71-
System.out.println(" 18: Bulk Send Complex Example ");
69+
System.out.println(" 16: Bulk Send ");
70+
System.out.println(" 17: Bulk Send With MergeData ");
71+
System.out.println(" 18: Bulk Send With ASCII Charset And Merge Data ");
72+
System.out.println(" 19: Bulk Send From DataSource With Merge Data ");
73+
System.out.println(" 20: Bulk Send Complex Example ");
7274
System.out.println();
7375
System.out.println(" AMP Html Examples: ");
74-
System.out.println(" 19: Basic Send With Amp Body Example ");
75-
System.out.println(" 20: Bulk Send With Amp Body Example ");
76+
System.out.println(" 21: Basic Send With Amp Body Example ");
77+
System.out.println(" 22: Bulk Send With Amp Body Example ");
78+
79+
7680
System.out.println();
7781
System.out.println("-------------------------------------------------------------------------");
7882
}
@@ -97,17 +101,20 @@ private static String GetExampleName(String selection) {
97101
case 7: return "examples.basic.BasicSendWithEmbeddedImage";
98102
case 8: return "examples.basic.BasicSendWithProxy";
99103
case 9: return "examples.basic.BasicAsync";
100-
case 10: return "examples.basic.BasicSendComplexExample";
101-
case 11: return "examples.basic.invalid.BasicSendWithInvalidAttachment";
102-
case 12: return "examples.basic.invalid.BasicSendWithInvalidFrom";
103-
case 13: return "examples.basic.invalid.BasicSendWithInvalidRecipients";
104-
case 14: return "examples.bulk.BulkSend";
105-
case 15: return "examples.bulk.BulkSendWithMergeData";
106-
case 16: return "examples.bulk.BulkSendWithASCIICharsetMergeData";
107-
case 17: return "examples.bulk.BulkSendFromDataSourceWithMerge";
108-
case 18: return "examples.bulk.BulkSendComplexExample";
109-
case 19: return "examples.basic.BasicSendWithAmpBodyExample";
110-
case 20: return "examples.bulk.BulkSendWithAmpBodyExample";
104+
case 10: return "examples.basic.BasicAsyncWithRetry";
105+
case 11: return "examples.basic.BasicSendWithRetry";
106+
case 12: return "examples.basic.BasicSendComplexExample";
107+
case 13: return "examples.basic.invalid.BasicSendWithInvalidAttachment";
108+
case 14: return "examples.basic.invalid.BasicSendWithInvalidFrom";
109+
case 15: return "examples.basic.invalid.BasicSendWithInvalidRecipients";
110+
111+
case 16: return "examples.bulk.BulkSend";
112+
case 17: return "examples.bulk.BulkSendWithMergeData";
113+
case 18: return "examples.bulk.BulkSendWithASCIICharsetMergeData";
114+
case 19: return "examples.bulk.BulkSendFromDataSourceWithMerge";
115+
case 20: return "examples.bulk.BulkSendComplexExample";
116+
case 21: return "examples.basic.BasicSendWithAmpBodyExample";
117+
case 22: return "examples.bulk.BulkSendWithAmpBodyExample";
111118
default:
112119
System.out.println("Invalid Input (Out of Range)");
113120
return null;

examples/src/main/java/examples/basic/BasicAsync.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.socketLabs.injectionApi.message.BasicMessage;
99
import com.socketLabs.injectionApi.message.EmailAddress;
1010
import examples.*;
11+
import okhttp3.Call;
1112

1213
import java.io.IOException;
1314

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package examples.basic;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.SerializationFeature;
5+
import com.socketLabs.injectionApi.SendResponse;
6+
import com.socketLabs.injectionApi.SocketLabsClient;
7+
import com.socketLabs.injectionApi.core.SendAsyncCallback;
8+
import com.socketLabs.injectionApi.message.BasicMessage;
9+
import com.socketLabs.injectionApi.message.EmailAddress;
10+
import examples.Example;
11+
import examples.ExampleConfig;
12+
13+
import java.io.IOException;
14+
import java.net.InetSocketAddress;
15+
import java.net.Proxy;
16+
17+
public class BasicAsyncWithRetry implements Example {
18+
19+
@Override
20+
public SendResponse RunExample() throws Exception {
21+
22+
BasicMessage message = new BasicMessage();
23+
24+
message.setSubject("Sending A Test Message (Basic Send Async)");
25+
message.setHtmlBody("<html><body><h1>Sending A Test Message</h1><p>This is the Html Body of my message.</p></body></html>");
26+
message.setPlainTextBody("This is the Plain Text Body of my message.");
27+
28+
message.setFrom(new EmailAddress("[email protected]"));
29+
message.addToEmailAddress("[email protected]");
30+
31+
// create the proxy
32+
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 4433));
33+
34+
// create the client
35+
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey, proxy);
36+
37+
client.setRequestTimeout(5);
38+
client.setNumberOfRetries(2);
39+
40+
// send the message
41+
client.sendAsync(message, new SendAsyncCallback() {
42+
43+
@Override
44+
public void onError(Exception ex) {
45+
// Handle Exception here
46+
ex.printStackTrace();
47+
return;
48+
}
49+
50+
@Override
51+
public void onResponse(SendResponse response) throws IOException {
52+
// Handle SendResponse here
53+
54+
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
55+
56+
System.out.println("Response body : ");
57+
System.out.println(mapper.writeValueAsString(response));
58+
System.out.println();
59+
System.out.println();
60+
System.out.println("Enter a number (or QUIT to exit):");
61+
62+
return;
63+
64+
}
65+
});
66+
67+
System.out.println("Waiting for response...");
68+
return null;
69+
}
70+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package examples.basic;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.SerializationFeature;
5+
import com.socketLabs.injectionApi.SendResponse;
6+
import com.socketLabs.injectionApi.SocketLabsClient;
7+
import com.socketLabs.injectionApi.core.SendAsyncCallback;
8+
import com.socketLabs.injectionApi.message.BasicMessage;
9+
import com.socketLabs.injectionApi.message.EmailAddress;
10+
import examples.Example;
11+
import examples.ExampleConfig;
12+
13+
import java.io.IOException;
14+
import java.net.InetSocketAddress;
15+
import java.net.Proxy;
16+
17+
18+
public class BasicSendWithRetry implements Example {
19+
public SendResponse RunExample () throws Exception {
20+
21+
BasicMessage message = new BasicMessage();
22+
23+
message.setSubject("Sending An Email Through A Proxy");
24+
message.setHtmlBody("<html><body><h1>Sending An Email Through A Proxy</h1><p>This is the Html Body of my message.</p></body></html>");
25+
message.setPlainTextBody("This is the Plain Text Body of my message.");
26+
27+
message.setFrom(new EmailAddress("[email protected]"));
28+
message.addToEmailAddress("[email protected]");
29+
30+
// create the proxy
31+
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 4433));
32+
33+
// create the client
34+
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey, proxy);
35+
client.setNumberOfRetries(2);
36+
37+
// send the message
38+
SendResponse response = client.send(message);
39+
40+
return response;
41+
}
42+
}

injectionApi/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
def baseGroupId = "com.socketlabs"
88
def baseArtifactId = 'injectionApi'
99
def computeVersion() {
10-
def baseVersion = "1.1.1"
10+
def baseVersion = "1.2.1"
1111
def release = true
1212
if (release)
1313
return "${baseVersion}"

injectionApi/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.socketlabs</groupId>
66
<artifactId>injectionApi</artifactId>
7-
<version>1.1.1</version>
7+
<version>1.2.1</version>
88
<name>socketlabs-java</name>
99
<description>SocketLabs Email Delivery Java library</description>
1010
<url>https://github.com/socketlabs/socketlabs-java/</url>
@@ -23,15 +23,15 @@
2323
<id>david-schrenker</id>
2424
<name>David Schrenker</name>
2525
<email>[email protected]</email>
26-
<organization>org.apache.maven.model.Organization@15ba5d1c</organization>
26+
<organization>org.apache.maven.model.Organization@4d9a0b1c</organization>
2727
<roles>
2828
<role>Developer</role>
2929
</roles>
3030
</developer>
3131
<developer>
3232
<id>rbrazuk</id>
3333
<name>Ross Brazuk</name>
34-
<organization>org.apache.maven.model.Organization@3cf2a6e7</organization>
34+
<organization>org.apache.maven.model.Organization@201c6b36</organization>
3535
<roles>
3636
<role>Developer</role>
3737
</roles>
@@ -40,7 +40,7 @@
4040
<contributors>
4141
<contributor>
4242
<name>Ryan Lydzinski</name>
43-
<organization>org.apache.maven.model.Organization@2e482819</organization>
43+
<organization>org.apache.maven.model.Organization@6c83948b</organization>
4444
<roles>
4545
<role>Intern</role>
4646
</roles>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.socketLabs.injectionApi;
2+
3+
4+
import java.time.Duration;
5+
import java.util.Random;
6+
7+
public class RetrySettings {
8+
private final int defaultNumberOfRetries = 0;
9+
private final int maximumAllowedNumberOfRetries = 5;
10+
private Duration minimumRetryTIme = Duration.ofSeconds(1);
11+
private Duration maximumRetryTime = Duration.ofSeconds(10);
12+
private int maximumNumberOfRetries = defaultNumberOfRetries;
13+
14+
/// <summary>
15+
/// Creates a new instance of the <c>RetrySettings</c>.
16+
/// </summary>
17+
/// <param name="maximumRetries"></param>
18+
public RetrySettings(int maximumRetries){
19+
if (maximumRetries < 0)
20+
throw new IllegalArgumentException("maximumNumberOfRetries must be greater than 0");
21+
if (maximumRetries > 5)
22+
throw new IllegalArgumentException("The maximum number of allowed retries is " + maximumAllowedNumberOfRetries);
23+
maximumNumberOfRetries = maximumRetries;
24+
}
25+
26+
/// <summary>
27+
/// The maximum number of retries when sending an Injection API Request before throwing an exception. Default: 0, no retries, you must explicitly enable retry settings
28+
/// </summary>
29+
public int getMaximumNumberOfRetries(){
30+
return maximumNumberOfRetries;
31+
}
32+
33+
34+
/// <summary>
35+
/// Get the time period to wait before next call
36+
/// </summary>
37+
/// <param name="numberOfAttempts"></param>
38+
/// <returns></returns>
39+
public Duration getNextWaitInterval(int numberOfAttempts){
40+
41+
long interval = Math.min(
42+
minimumRetryTIme.toMillis() + getRetryDelta(numberOfAttempts),
43+
maximumRetryTime.toMillis());
44+
return Duration.ofMillis(interval);
45+
46+
}
47+
48+
public int getRetryDelta(int numberOfAttempts){
49+
50+
Random random = new Random();
51+
52+
int min = (int) (Duration.ofSeconds(1).toMillis() * 0.8);
53+
int max = (int) (Duration.ofSeconds(1).toMillis() * 1.2);
54+
int randomNumber = random.nextInt(max - min) + min;
55+
56+
return (int) ((Math.pow(2.0, numberOfAttempts) - 1.0) * randomNumber);
57+
58+
}
59+
60+
}

0 commit comments

Comments
 (0)