Skip to content
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

feat!: Allowing custom thread pool size to configure #736

Open
wants to merge 2 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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,37 @@ public class Example {
}
```

### With Custom Thread Pool Size

```java
import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
Email from = new Email("[email protected]");
String subject = "Sending with Twilio SendGrid is Fun";
Email to = new Email("[email protected]");
Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
Mail mail = new Mail(from, subject, to, content);

SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"), 4);
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
}
```

The `Mail` constructor creates a [personalization object](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html) for you. [Here](examples/helpers/mail/Example.java#L221) is an example of how to add to it.

### Without Mail Helper Class
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/sendgrid/BaseInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class BaseInterface implements SendGridAPI {

private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
private static final int RATE_LIMIT_RESPONSE_CODE = 429;
private static final int THREAD_POOL_SIZE = 8;
private static final int DEFAULT_THREAD_POOL_SIZE = 8;

private ExecutorService pool;

Expand Down Expand Up @@ -86,6 +86,17 @@ public BaseInterface(final Client client) {
* @param host the base URL for the API
*/
public void initialize(final String auth, final String host) {
initialize(auth, host, DEFAULT_THREAD_POOL_SIZE);
}

/**
* Initialize the client.
*
* @param auth authorization header value
* @param host the base URL for the API
* @param threadPoolSize the pool size to initialize for sending email asynchronously
*/
public void initialize(final String auth, final String host, int threadPoolSize) {
this.host = host;
this.version = "v3";
this.requestHeaders = new HashMap<>();
Expand All @@ -95,7 +106,7 @@ public void initialize(final String auth, final String host) {
this.rateLimitRetry = 5;
this.rateLimitSleep = 1100;

this.pool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
this.pool = Executors.newFixedThreadPool(threadPoolSize);
}

/**
Expand Down
51 changes: 49 additions & 2 deletions src/main/java/com/sendgrid/SendGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
public class SendGrid extends BaseInterface {

private static final String SENDGRID_HOST_NAME = "api.sendgrid.com";
public static final String BEARER = "Bearer ";

/**
* Construct a new Twilio SendGrid API wrapper.
*
Expand All @@ -14,6 +17,16 @@ public SendGrid(final String apiKey) {
initializeSendGrid(apiKey);
}

/**
* Construct a new Twilio SendGrid API wrapper with custom thread pool size
*
* @param apiKey is your Twilio SendGrid API Key: https://app.sendgrid.com/settings/api_keys
* @param threadPoolSize the pool size to initialize for sending email asynchronously
*/
public SendGrid(final String apiKey, final int threadPoolSize) {
initializeSendGrid(apiKey, threadPoolSize);
}

/**
* Construct a new Twilio SendGrid API wrapper.
*
Expand All @@ -25,6 +38,18 @@ public SendGrid(final String apiKey, final Boolean test) {
initializeSendGrid(apiKey);
}

/**
* Construct a new Twilio SendGrid API wrapper with custom thread pool size
*
* @param apiKey is your Twilio SendGrid API Key: https://app.sendgrid.com/settings/api_keys
* @param test is true if you are unit testing
* @param threadPoolSize the pool size to initialize for sending email asynchronously
*/
public SendGrid(final String apiKey, final Boolean test, final int threadPoolSize) {
super(test);
initializeSendGrid(apiKey, threadPoolSize);
}

/**
* Construct a new Twilio SendGrid API wrapper.
*
Expand All @@ -37,11 +62,33 @@ public SendGrid(final String apiKey, final Client client) {
}

/**
* Initialize the client.
* Construct a new Twilio SendGrid API wrapper with custom thread pool size
*
* @param apiKey is your Twilio SendGrid API Key: https://app.sendgrid.com/settings/api_keys
* @param client the Client to use (allows to customize its configuration)
* @param threadPoolSize the pool size to initialize for sending email asynchronously
*/
public SendGrid(final String apiKey, final Client client, final int threadPoolSize) {
super(client);
initializeSendGrid(apiKey, threadPoolSize);
}

/**
* Initialize the client
*
* @param apiKey the user's API key
*/
public void initializeSendGrid(final String apiKey) {
this.initialize("Bearer " + apiKey, "api.sendgrid.com");
this.initialize(BEARER + apiKey, SENDGRID_HOST_NAME);
}

/**
* Initialize the client with custom thread pool size with custom thread pool size
*
* @param apiKey the user's API key
* @param threadPoolSize the pool size to initialize for sending email asynchronously
*/
public void initializeSendGrid(final String apiKey, final int threadPoolSize) {
this.initialize(BEARER + apiKey, SENDGRID_HOST_NAME, threadPoolSize);
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/sendgrid/SendGridAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
public interface SendGridAPI {

/**
* Initialize the client.
* Initialize the client with default thread pool size
*
* @param auth authorization header value
* @param host the base URL for the API
*/
void initialize(final String auth, final String host);

/**
* Initialize the client with custom thread pool size
*
* @param auth authorization header value
* @param host the base URL for the API
* @param threadPoolSize the pool size to initialize for sending email asynchronously
*/
void initialize(final String auth, final String host, int threadPoolSize);

/**
* Get the current library version.
*
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/sendgrid/SendGridTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public void testInitialization() {
Assert.assertEquals(sg.getRequestHeaders(), requestHeaders);
}

@Test
public void testInitializationWithCustomThreadPoolSize() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY, 4);
Assert.assertEquals(sg.getHost(), "api.sendgrid.com");
Assert.assertEquals(sg.getVersion(), "v3");
Map<String,String> requestHeaders = buildDefaultHeaders();
Assert.assertEquals(sg.getRequestHeaders(), requestHeaders);
}

@Test
public void testConstructWithClient() throws IOException {
Client client = mock(Client.class);
Expand Down