Skip to content

addin configurable HttpClients to handle connection pooling. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ proguard/
.gradletasknamecache
.gradle
.gradle/
gradle.properties

build/
bin/
gradle.properties
/.vs/VSWorkspaceState.json
/.vs/socketlabs-java/v17/TestStore/0/testlog.manifest
/.vs/socketlabs-java/v17/TestStore/0/000.testlog
Expand Down
10 changes: 7 additions & 3 deletions examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ version '1.0-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenLocal()
mavenCentral()
maven {
url = uri("https://central.sonatype.com/repository/maven-snapshots/")
}
mavenLocal()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.9.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.14.0'
compile 'com.fasterxml.jackson.core:jackson-databind:2.14.0'
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.4.2'
compile 'org.apache.httpcomponents:httpclient:4.5.14'
compile 'org.apache.httpcomponents:httpasyncclient:4.1.5'
compile group: 'com.socketlabs', name: 'injectionApi', version: '2.0.1-SNAPSHOT'
}
Binary file modified examples/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 2 additions & 3 deletions examples/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Thu Jun 18 12:46:49 EDT 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion examples/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

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

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
2 changes: 1 addition & 1 deletion examples/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

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

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
46 changes: 25 additions & 21 deletions examples/src/main/java/examples/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,24 @@ private static void DisplayTheMenu() {
System.out.println(" 10: Basic Async With Retry");
System.out.println(" 11: Basic Send With Retry ");
System.out.println(" 12: Basic Send Complex Example ");
System.out.println(" 13: Basic Send With HttpClient ");
System.out.println(" 14: Basic Send With HttpAsyncClient ");
System.out.println();
System.out.println(" Validation Error Handling Examples: ");
System.out.println(" 13: Basic Send With Invalid Attachment");
System.out.println(" 14: Basic Send With Invalid From ");
System.out.println(" 15: Basic Send With Invalid Recipients ");
System.out.println(" 15: Basic Send With Invalid Attachment");
System.out.println(" 16: Basic Send With Invalid From ");
System.out.println(" 17: Basic Send With Invalid Recipients ");
System.out.println();
System.out.println(" Bulk Send Examples: ");
System.out.println(" 16: Bulk Send ");
System.out.println(" 17: Bulk Send With MergeData ");
System.out.println(" 18: Bulk Send With ASCII Charset And Merge Data ");
System.out.println(" 19: Bulk Send From DataSource With Merge Data ");
System.out.println(" 20: Bulk Send Complex Example ");
System.out.println(" 18: Bulk Send ");
System.out.println(" 19: Bulk Send With MergeData ");
System.out.println(" 20: Bulk Send With ASCII Charset And Merge Data ");
System.out.println(" 21: Bulk Send From DataSource With Merge Data ");
System.out.println(" 22: Bulk Send Complex Example ");
System.out.println();
System.out.println(" AMP Html Examples: ");
System.out.println(" 21: Basic Send With Amp Body Example ");
System.out.println(" 22: Bulk Send With Amp Body Example ");
System.out.println(" 23: Basic Send With Amp Body Example ");
System.out.println(" 24: Bulk Send With Amp Body Example ");


System.out.println();
Expand Down Expand Up @@ -104,17 +106,19 @@ private static String GetExampleName(String selection) {
case 10: return "examples.basic.BasicAsyncWithRetry";
case 11: return "examples.basic.BasicSendWithRetry";
case 12: return "examples.basic.BasicSendComplexExample";
case 13: return "examples.basic.invalid.BasicSendWithInvalidAttachment";
case 14: return "examples.basic.invalid.BasicSendWithInvalidFrom";
case 15: return "examples.basic.invalid.BasicSendWithInvalidRecipients";

case 16: return "examples.bulk.BulkSend";
case 17: return "examples.bulk.BulkSendWithMergeData";
case 18: return "examples.bulk.BulkSendWithASCIICharsetMergeData";
case 19: return "examples.bulk.BulkSendFromDataSourceWithMerge";
case 20: return "examples.bulk.BulkSendComplexExample";
case 21: return "examples.basic.BasicSendWithAmpBodyExample";
case 22: return "examples.bulk.BulkSendWithAmpBodyExample";
case 13: return "examples.basic.BasicSendWithHttpClient";
case 14: return "examples.basic.BasicSendWithHttpAsyncClient";
case 15: return "examples.basic.invalid.BasicSendWithInvalidAttachment";
case 16: return "examples.basic.invalid.BasicSendWithInvalidFrom";
case 17: return "examples.basic.invalid.BasicSendWithInvalidRecipients";

case 18: return "examples.bulk.BulkSend";
case 19: return "examples.bulk.BulkSendWithMergeData";
case 20: return "examples.bulk.BulkSendWithASCIICharsetMergeData";
case 21: return "examples.bulk.BulkSendFromDataSourceWithMerge";
case 22: return "examples.bulk.BulkSendComplexExample";
case 23: return "examples.basic.BasicSendWithAmpBodyExample";
case 24: return "examples.bulk.BulkSendWithAmpBodyExample";
default:
System.out.println("Invalid Input (Out of Range)");
return null;
Expand Down
1 change: 0 additions & 1 deletion examples/src/main/java/examples/basic/BasicAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.socketLabs.injectionApi.message.BasicMessage;
import com.socketLabs.injectionApi.message.EmailAddress;
import examples.*;
import okhttp3.Call;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package examples.basic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.socketLabs.injectionApi.SendResponse;
import com.socketLabs.injectionApi.SocketLabsClient;
import com.socketLabs.injectionApi.core.SendAsyncCallback;
import com.socketLabs.injectionApi.message.BasicMessage;
import com.socketLabs.injectionApi.message.EmailAddress;
import examples.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.nio.reactor.ConnectingIOReactor;

import java.io.IOException;

public class BasicSendWithHttpAsyncClient implements Example {

@Override
public SendResponse RunExample() throws Exception {

BasicMessage message = new BasicMessage();

message.setSubject("Sending A Test Message (Basic Send Async)");
message.setHtmlBody("<html><body><h1>Sending A Test Message</h1><p>This is the Html Body of my message.</p></body></html>");
message.setPlainTextBody("This is the Plain Text Body of my message.");

message.setFrom(new EmailAddress("[email protected]"));
message.addToEmailAddress("[email protected]");

// create the HttpClient
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);

RequestConfig requestConfig =RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.build();

CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(requestConfig)
.build();

// create the client
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey);

// send the message
client.sendAsync(message, httpClient, new SendAsyncCallback() {

@Override
public void onError(Exception ex) {
// Handle Exception here
ex.printStackTrace();
return;
}

@Override
public void onResponse(SendResponse response) throws IOException {
// Handle SendResponse here

ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

System.out.println("Response body : ");
System.out.println(mapper.writeValueAsString(response));
System.out.println();
System.out.println();
System.out.println("Enter a number (or QUIT to exit):");

return;

}
});

System.out.println("Waiting for response...");
return null;
}
}
53 changes: 53 additions & 0 deletions examples/src/main/java/examples/basic/BasicSendWithHttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package examples.basic;

import com.socketLabs.injectionApi.SendResponse;
import com.socketLabs.injectionApi.SocketLabsClient;
import com.socketLabs.injectionApi.message.BasicMessage;
import com.socketLabs.injectionApi.message.EmailAddress;
import examples.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;


public class BasicSendWithHttpClient implements Example {

public SendResponse RunExample () throws Exception {

BasicMessage message = new BasicMessage();

message.setSubject("Sending An Email Through A Proxy");
message.setHtmlBody("<html><body><h1>Sending An Email Through A Proxy</h1><p>This is the Html Body of my message.</p></body></html>");
message.setPlainTextBody("This is the Plain Text Body of my message.");

message.setFrom(new EmailAddress("[email protected]"));
message.addToEmailAddress("[email protected]");

// create the HttpClient
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
cm.setValidateAfterInactivity(10000);

RequestConfig requestConfig =RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.build();

CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(requestConfig)
.build();

// create the client
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey);

// send the message
SendResponse response = client.send(message, httpClient);

return response;

}
}
40 changes: 27 additions & 13 deletions injectionApi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,53 @@ plugins {
id 'maven-publish'
id 'signing'
}

def baseTitle ="SocketLabs Java"
def baseGroupId = "com.socketlabs"
def baseArtifactId = 'injectionApi'
def computeVersion() {
def baseVersion = "1.4.4"
def release = true
def baseArtifactId = "injectionApi"

static def computeVersion() {
def baseVersion = "2.0.1"
def release = false
if (release)
return "${baseVersion}"
else
return "${baseVersion}-SNAPSHOT"
}
group "${baseGroupId}"
version = computeVersion()

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
mavenLocal()
}

compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}

dependencies {
compile 'com.fasterxml.jackson.core:jackson-core:2.14.0'
compile 'com.fasterxml.jackson.core:jackson-databind:2.14.0'
compile group: 'com.google.guava', name: 'guava', version: '32.1.3-jre'
compile 'com.squareup.okhttp3:okhttp:4.10.0'

compile 'org.apache.httpcomponents:httpclient:4.5.14'
compile 'org.apache.httpcomponents:httpasyncclient:4.1.5'
}

jar {
manifest {
attributes 'Implementation-Title': 'SocketLabs Java',
attributes(
'Implementation-Title': baseTitle,
'Implementation-Version': version
)
}
}


signing {
required { gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
Expand All @@ -59,20 +72,21 @@ artifacts {
archives sourceJar
archives packageJavadoc
}

def mavenReleaseUrl = "https://ossrh-staging-api.central.sonatype.com/service/local/"
def mavenSnapshotUrl = "https://central.sonatype.com/repository/maven-snapshots/"

uploadArchives {
repositories {
mavenDeployer {
beforeDeployment {
MavenDeployment deployment -> signing.signPom(deployment)
}

repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
repository(url: mavenReleaseUrl) {
authentication(userName: sonatypeUsername, password: sonatypePassword)
}
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
snapshotRepository(url: mavenSnapshotUrl) {
authentication(userName: sonatypeUsername, password: sonatypePassword)
}

pom.project {
groupId "${baseGroupId}"
artifactId "${baseArtifactId}"
Expand Down
4 changes: 2 additions & 2 deletions injectionApi/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Oct 30 12:53:58 EDT 2018
#Mon Jun 23 10:40:41 EDT 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip
Loading