You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/09/29 04:39:22 UTC
[shardingsphere-elasticjob] branch master updated: Init email
session in constructor (#1514)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new 9c003c6 Init email session in constructor (#1514)
9c003c6 is described below
commit 9c003c6d7797e59dd47f8cda592454d0c3edfe5e
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue Sep 29 12:37:39 2020 +0800
Init email session in constructor (#1514)
---
.../error/handler/email/EmailJobErrorHandler.java | 62 ++++++++++++----------
.../handler/email/EmailJobErrorHandlerTest.java | 31 -----------
2 files changed, 33 insertions(+), 60 deletions(-)
diff --git a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandler.java b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandler.java
index 4838598..6a1eb43 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandler.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandler.java
@@ -28,7 +28,6 @@ import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
-import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
@@ -36,7 +35,6 @@ import javax.mail.internet.MimeMultipart;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
-import java.util.Optional;
import java.util.Properties;
/**
@@ -47,9 +45,38 @@ public final class EmailJobErrorHandler implements JobErrorHandler {
public static final String CONFIG_PREFIX = "email";
- private final EmailConfiguration config = EmailConfigurationLoader.unmarshal(CONFIG_PREFIX);
+ private final EmailConfiguration config;
- private Session session;
+ private final Session session;
+
+ public EmailJobErrorHandler() {
+ config = EmailConfigurationLoader.unmarshal(CONFIG_PREFIX);
+ session = Session.getDefaultInstance(createSessionProperties(), getSessionAuthenticator());
+ }
+
+ private Properties createSessionProperties() {
+ Properties result = new Properties();
+ result.put("mail.smtp.host", config.getHost());
+ result.put("mail.smtp.port", config.getPort());
+ result.put("mail.smtp.auth", "true");
+ result.put("mail.transport.protocol", config.getProtocol());
+ result.setProperty("mail.debug", Boolean.toString(config.isDebug()));
+ if (config.isUseSsl()) {
+ result.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
+ result.setProperty("mail.smtp.socketFactory.fallback", "false");
+ }
+ return result;
+ }
+
+ private Authenticator getSessionAuthenticator() {
+ return new Authenticator() {
+
+ @Override
+ public PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(config.getUsername(), config.getPassword());
+ }
+ };
+ }
@Override
public void handleException(final String jobName, final Throwable cause) {
@@ -68,7 +95,7 @@ public final class EmailJobErrorHandler implements JobErrorHandler {
}
private Message createMessage(final String content) throws MessagingException {
- MimeMessage result = new MimeMessage(Optional.ofNullable(session).orElseGet(this::createSession));
+ MimeMessage result = new MimeMessage(session);
result.setFrom(new InternetAddress(config.getFrom()));
result.setSubject(config.getSubject());
result.setSentDate(new Date());
@@ -87,31 +114,8 @@ public final class EmailJobErrorHandler implements JobErrorHandler {
return result;
}
- private synchronized Session createSession() {
- if (null == session) {
- Properties props = new Properties();
- props.put("mail.smtp.host", config.getHost());
- props.put("mail.smtp.port", config.getPort());
- props.put("mail.smtp.auth", "true");
- props.put("mail.transport.protocol", config.getProtocol());
- props.setProperty("mail.debug", Boolean.toString(config.isDebug()));
- if (config.isUseSsl()) {
- props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.setProperty("mail.smtp.socketFactory.fallback", "false");
- }
- session = Session.getDefaultInstance(props, new Authenticator() {
-
- @Override
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(config.getUsername(), config.getPassword());
- }
- });
- }
- return session;
- }
-
private void sendMessage(final Message message) throws MessagingException {
- Transport.send(message);
+ session.getTransport().send(message);
}
@Override
diff --git a/elasticjob-error-handler/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java b/elasticjob-error-handler/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java
index 54ea0e7..100a12c 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java
@@ -17,29 +17,20 @@
package org.apache.shardingsphere.elasticjob.error.handler.email;
-import lombok.SneakyThrows;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.ArgumentMatchers;
-import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import org.slf4j.Logger;
import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public final class EmailJobErrorHandlerTest {
- @Mock
- private Logger log;
-
@Test
public void assertHandleExceptionWithYAMLConfiguration() throws ReflectiveOperationException {
EmailJobErrorHandler emailJobErrorHandler = new EmailJobErrorHandler();
@@ -59,26 +50,4 @@ public final class EmailJobErrorHandlerTest {
assertTrue(config.isUseSsl());
assertTrue(config.isDebug());
}
-
- @Test
- public void assertHandleExceptionForNullConfiguration() throws ReflectiveOperationException {
- EmailJobErrorHandler emailJobErrorHandler = new EmailJobErrorHandler();
- Field emailConfigurationField = EmailJobErrorHandler.class.getDeclaredField("config");
- emailConfigurationField.setAccessible(true);
- emailConfigurationField.set(emailJobErrorHandler, null);
- setStaticFieldValue(emailJobErrorHandler);
- Throwable cause = new RuntimeException("test exception");
- emailJobErrorHandler.handleException("test job name", cause);
- verify(log).error(ArgumentMatchers.any(String.class), ArgumentMatchers.any(NullPointerException.class));
- }
-
- @SneakyThrows
- private void setStaticFieldValue(final EmailJobErrorHandler emailJobErrorHandler) {
- Field field = emailJobErrorHandler.getClass().getDeclaredField("log");
- field.setAccessible(true);
- Field modifiers = field.getClass().getDeclaredField("modifiers");
- modifiers.setAccessible(true);
- modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
- field.set(emailJobErrorHandler, log);
- }
}