Skip to content

Commit f35fcbc

Browse files
Merge pull request #11 from socketlabs/feature/http-client-config
addin configurable HttpClients to handle connection pooling.
2 parents f70901b + 9c16668 commit f35fcbc

File tree

19 files changed

+707
-322
lines changed

19 files changed

+707
-322
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ proguard/
2929
.gradletasknamecache
3030
.gradle
3131
.gradle/
32+
gradle.properties
33+
3234
build/
3335
bin/
34-
gradle.properties
3536
/.vs/VSWorkspaceState.json
3637
/.vs/socketlabs-java/v17/TestStore/0/testlog.manifest
3738
/.vs/socketlabs-java/v17/TestStore/0/000.testlog

examples/build.gradle

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ version '1.0-SNAPSHOT'
88
sourceCompatibility = 1.8
99

1010
repositories {
11-
mavenLocal()
1211
mavenCentral()
12+
maven {
13+
url = uri("https://central.sonatype.com/repository/maven-snapshots/")
14+
}
15+
mavenLocal()
1316
}
1417

1518
dependencies {
16-
testCompile group: 'junit', name: 'junit', version: '4.9.2'
1719
compile 'com.fasterxml.jackson.core:jackson-core:2.14.0'
1820
compile 'com.fasterxml.jackson.core:jackson-databind:2.14.0'
19-
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.4.2'
21+
compile 'org.apache.httpcomponents:httpclient:4.5.14'
22+
compile 'org.apache.httpcomponents:httpasyncclient:4.1.5'
23+
compile group: 'com.socketlabs', name: 'injectionApi', version: '2.0.1-SNAPSHOT'
2024
}
1.3 KB
Binary file not shown.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Thu Jun 18 12:46:49 EDT 2020
2-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip
31
distributionBase=GRADLE_USER_HOME
42
distributionPath=wrapper/dists
5-
zipStorePath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip
64
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

examples/gradlew

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ APP_NAME="Gradle"
2828
APP_BASE_NAME=`basename "$0"`
2929

3030
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31-
DEFAULT_JVM_OPTS=""
31+
DEFAULT_JVM_OPTS='"-Xmx64m"'
3232

3333
# Use the maximum available, or set MAX_FD != -1 to use that value.
3434
MAX_FD="maximum"

examples/gradlew.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
1414
set APP_HOME=%DIRNAME%
1515

1616
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17-
set DEFAULT_JVM_OPTS=
17+
set DEFAULT_JVM_OPTS="-Xmx64m"
1818

1919
@rem Find java.exe
2020
if defined JAVA_HOME goto findJavaFromJavaHome

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,24 @@ private static void DisplayTheMenu() {
5959
System.out.println(" 10: Basic Async With Retry");
6060
System.out.println(" 11: Basic Send With Retry ");
6161
System.out.println(" 12: Basic Send Complex Example ");
62+
System.out.println(" 13: Basic Send With HttpClient ");
63+
System.out.println(" 14: Basic Send With HttpAsyncClient ");
6264
System.out.println();
6365
System.out.println(" Validation Error Handling Examples: ");
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 ");
66+
System.out.println(" 15: Basic Send With Invalid Attachment");
67+
System.out.println(" 16: Basic Send With Invalid From ");
68+
System.out.println(" 17: Basic Send With Invalid Recipients ");
6769
System.out.println();
6870
System.out.println(" Bulk Send Examples: ");
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 ");
71+
System.out.println(" 18: Bulk Send ");
72+
System.out.println(" 19: Bulk Send With MergeData ");
73+
System.out.println(" 20: Bulk Send With ASCII Charset And Merge Data ");
74+
System.out.println(" 21: Bulk Send From DataSource With Merge Data ");
75+
System.out.println(" 22: Bulk Send Complex Example ");
7476
System.out.println();
7577
System.out.println(" AMP Html Examples: ");
76-
System.out.println(" 21: Basic Send With Amp Body Example ");
77-
System.out.println(" 22: Bulk Send With Amp Body Example ");
78+
System.out.println(" 23: Basic Send With Amp Body Example ");
79+
System.out.println(" 24: Bulk Send With Amp Body Example ");
7880

7981

8082
System.out.println();
@@ -104,17 +106,19 @@ private static String GetExampleName(String selection) {
104106
case 10: return "examples.basic.BasicAsyncWithRetry";
105107
case 11: return "examples.basic.BasicSendWithRetry";
106108
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";
109+
case 13: return "examples.basic.BasicSendWithHttpClient";
110+
case 14: return "examples.basic.BasicSendWithHttpAsyncClient";
111+
case 15: return "examples.basic.invalid.BasicSendWithInvalidAttachment";
112+
case 16: return "examples.basic.invalid.BasicSendWithInvalidFrom";
113+
case 17: return "examples.basic.invalid.BasicSendWithInvalidRecipients";
114+
115+
case 18: return "examples.bulk.BulkSend";
116+
case 19: return "examples.bulk.BulkSendWithMergeData";
117+
case 20: return "examples.bulk.BulkSendWithASCIICharsetMergeData";
118+
case 21: return "examples.bulk.BulkSendFromDataSourceWithMerge";
119+
case 22: return "examples.bulk.BulkSendComplexExample";
120+
case 23: return "examples.basic.BasicSendWithAmpBodyExample";
121+
case 24: return "examples.bulk.BulkSendWithAmpBodyExample";
118122
default:
119123
System.out.println("Invalid Input (Out of Range)");
120124
return null;

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

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

1312
import java.io.IOException;
1413

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.*;
11+
import org.apache.http.client.config.RequestConfig;
12+
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
13+
import org.apache.http.impl.nio.client.HttpAsyncClients;
14+
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
15+
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
16+
import org.apache.http.nio.reactor.ConnectingIOReactor;
17+
18+
import java.io.IOException;
19+
20+
public class BasicSendWithHttpAsyncClient implements Example {
21+
22+
@Override
23+
public SendResponse RunExample() throws Exception {
24+
25+
BasicMessage message = new BasicMessage();
26+
27+
message.setSubject("Sending A Test Message (Basic Send Async)");
28+
message.setHtmlBody("<html><body><h1>Sending A Test Message</h1><p>This is the Html Body of my message.</p></body></html>");
29+
message.setPlainTextBody("This is the Plain Text Body of my message.");
30+
31+
message.setFrom(new EmailAddress("[email protected]"));
32+
message.addToEmailAddress("[email protected]");
33+
34+
// create the HttpClient
35+
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
36+
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
37+
cm.setMaxTotal(200);
38+
cm.setDefaultMaxPerRoute(20);
39+
40+
RequestConfig requestConfig =RequestConfig.custom()
41+
.setConnectTimeout(5000)
42+
.setConnectionRequestTimeout(5000)
43+
.setSocketTimeout(5000)
44+
.build();
45+
46+
CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom()
47+
.setConnectionManager(cm)
48+
.setDefaultRequestConfig(requestConfig)
49+
.build();
50+
51+
// create the client
52+
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey);
53+
54+
// send the message
55+
client.sendAsync(message, httpClient, new SendAsyncCallback() {
56+
57+
@Override
58+
public void onError(Exception ex) {
59+
// Handle Exception here
60+
ex.printStackTrace();
61+
return;
62+
}
63+
64+
@Override
65+
public void onResponse(SendResponse response) throws IOException {
66+
// Handle SendResponse here
67+
68+
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
69+
70+
System.out.println("Response body : ");
71+
System.out.println(mapper.writeValueAsString(response));
72+
System.out.println();
73+
System.out.println();
74+
System.out.println("Enter a number (or QUIT to exit):");
75+
76+
return;
77+
78+
}
79+
});
80+
81+
System.out.println("Waiting for response...");
82+
return null;
83+
}
84+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package examples.basic;
2+
3+
import com.socketLabs.injectionApi.SendResponse;
4+
import com.socketLabs.injectionApi.SocketLabsClient;
5+
import com.socketLabs.injectionApi.message.BasicMessage;
6+
import com.socketLabs.injectionApi.message.EmailAddress;
7+
import examples.*;
8+
import org.apache.http.client.config.RequestConfig;
9+
import org.apache.http.impl.client.CloseableHttpClient;
10+
import org.apache.http.impl.client.HttpClients;
11+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
12+
13+
14+
public class BasicSendWithHttpClient implements Example {
15+
16+
public SendResponse RunExample () throws Exception {
17+
18+
BasicMessage message = new BasicMessage();
19+
20+
message.setSubject("Sending An Email Through A Proxy");
21+
message.setHtmlBody("<html><body><h1>Sending An Email Through A Proxy</h1><p>This is the Html Body of my message.</p></body></html>");
22+
message.setPlainTextBody("This is the Plain Text Body of my message.");
23+
24+
message.setFrom(new EmailAddress("[email protected]"));
25+
message.addToEmailAddress("[email protected]");
26+
27+
// create the HttpClient
28+
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
29+
cm.setMaxTotal(200);
30+
cm.setDefaultMaxPerRoute(20);
31+
cm.setValidateAfterInactivity(10000);
32+
33+
RequestConfig requestConfig =RequestConfig.custom()
34+
.setConnectTimeout(5000)
35+
.setConnectionRequestTimeout(5000)
36+
.setSocketTimeout(5000)
37+
.build();
38+
39+
CloseableHttpClient httpClient = HttpClients.custom()
40+
.setConnectionManager(cm)
41+
.setDefaultRequestConfig(requestConfig)
42+
.build();
43+
44+
// create the client
45+
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey);
46+
47+
// send the message
48+
SendResponse response = client.send(message, httpClient);
49+
50+
return response;
51+
52+
}
53+
}

0 commit comments

Comments
 (0)