diff --git a/README.md b/README.md index fc60fb70..0d857527 100644 --- a/README.md +++ b/README.md @@ -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("test@example.com"); + String subject = "Sending with Twilio SendGrid is Fun"; + Email to = new Email("test@example.com"); + 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 diff --git a/src/main/java/com/sendgrid/BaseInterface.java b/src/main/java/com/sendgrid/BaseInterface.java index 0a812c19..82771742 100644 --- a/src/main/java/com/sendgrid/BaseInterface.java +++ b/src/main/java/com/sendgrid/BaseInterface.java @@ -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; @@ -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<>(); @@ -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); } /** diff --git a/src/main/java/com/sendgrid/SendGrid.java b/src/main/java/com/sendgrid/SendGrid.java index e30a00f2..a0aca306 100644 --- a/src/main/java/com/sendgrid/SendGrid.java +++ b/src/main/java/com/sendgrid/SendGrid.java @@ -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. * @@ -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. * @@ -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. * @@ -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); } } diff --git a/src/main/java/com/sendgrid/SendGridAPI.java b/src/main/java/com/sendgrid/SendGridAPI.java index 71b2c3cc..5ce6d89c 100644 --- a/src/main/java/com/sendgrid/SendGridAPI.java +++ b/src/main/java/com/sendgrid/SendGridAPI.java @@ -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. * diff --git a/src/test/java/com/sendgrid/SendGridTest.java b/src/test/java/com/sendgrid/SendGridTest.java index 2ac11fdb..d8b490bf 100644 --- a/src/test/java/com/sendgrid/SendGridTest.java +++ b/src/test/java/com/sendgrid/SendGridTest.java @@ -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 requestHeaders = buildDefaultHeaders(); + Assert.assertEquals(sg.getRequestHeaders(), requestHeaders); + } + @Test public void testConstructWithClient() throws IOException { Client client = mock(Client.class);