mailjet-apiv3-java
mailjet-apiv3-java copied to clipboard
OutOfMemoryException when sending emails
Hello,
I get the following error when sending numerous emails via Java Api V3
java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method) ~[na:1.8.0_144]
at java.lang.Thread.start(Thread.java:717) ~[na:1.8.0_144]
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:957) ~[na:1.8.0_144]
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1378) ~[na:1.8.0_144]
at okhttp3.ConnectionPool.put(ConnectionPool.java:153) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.OkHttpClient$1.put(OkHttpClient.java:167) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:266) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at okhttp3.RealCall.execute(RealCall.java:92) ~[com.squareup.okhttp3.okhttp-3.12.0.jar:na]
at com.mailjet.client.MailjetClient.post(MailjetClient.java:118) ~[com.mailjet.mailjet-client-5.1.1.jar:na]
at com.mailjet.client.transactional.SendEmailsRequest.sendWith(SendEmailsRequest.java:61) ~[com.mailjet.mailjet-client-5.1.1.jar:na]
It stops after roughly 300 mails.
The code looks as follows:
def sendToUserMailjet(to: User, subject: String, body: String): Unit = {
// https://github.com/mailjet/mailjet-apiv3-java
val client: MailjetClient = new MailjetClient(mailjetOptions)
val mail = TransactionalEmail
.builder()
.to(new SendContact(buildTo(to)))
.from(new SendContact(sender))
.htmlPart(body)
.subject(subject)
.build()
val request = SendEmailsRequest
.builder()
.message(mail) // you can add up to 50 messages per request
.sandboxMode(mailjetSandbox)
.build()
request.sendWith(client)
}
Environment
- PlayFramework
- Scala
- Mailjet Client 5.1.1
Do I have to create an instance of the client for all actions or is it okay to create a new one for each sending of an email.
Thank you for your answers.
@faustas hi, You are right, better to use MailjetClient as a singleton. Under the hood, each instance of the client creates an instance of OkhttpClient, which can be really expensive in terms of resource consumption.