You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by te...@apache.org on 2020/09/28 07:20:04 UTC
[shardingsphere-elasticjob] branch master updated: Add support for
SSL and debug in email error handler (#1507)
This is an automated email from the ASF dual-hosted git repository.
technoboy 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 fe0617d Add support for SSL and debug in email error handler (#1507)
fe0617d is described below
commit fe0617ddcc4871e5cf11634d6eef77e82cccdd3b
Author: 吴伟杰 <ro...@me.com>
AuthorDate: Mon Sep 28 15:19:54 2020 +0800
Add support for SSL and debug in email error handler (#1507)
---
.../elasticjob/error/handler/email/ConfigurationLoader.java | 12 ++++++++++--
.../elasticjob/error/handler/email/EmailConfiguration.java | 4 ++++
.../elasticjob/error/handler/email/EmailJobErrorHandler.java | 10 ++++++++--
.../src/main/resources/conf/error-handler-email.yaml | 2 ++
.../error/handler/email/EmailJobErrorHandlerTest.java | 8 ++++++++
.../src/test/resources/conf/error-handler-email.yaml | 2 ++
6 files changed, 34 insertions(+), 4 deletions(-)
diff --git a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/ConfigurationLoader.java b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/ConfigurationLoader.java
index 3b28ef2..521e5ca 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/ConfigurationLoader.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/ConfigurationLoader.java
@@ -62,17 +62,25 @@ public class ConfigurationLoader {
emailConfiguration.setCc(System.getProperty("error-handler-email.cc"));
emailConfiguration.setBcc(System.getProperty("error-handler-email.bcc"));
String protocol = System.getProperty("error-handler-email.protocol");
- String subject = System.getProperty("error-handler-email.subject");
- String port = System.getProperty("error-handler-email.port");
if (StringUtils.isNotBlank(protocol)) {
emailConfiguration.setProtocol(System.getProperty("error-handler-email.protocol"));
}
+ String useSsl = System.getProperty("error-handler-email.use-ssl");
+ if (StringUtils.isNotBlank(useSsl)) {
+ emailConfiguration.setUseSsl(Boolean.parseBoolean(useSsl));
+ }
+ String subject = System.getProperty("error-handler-email.subject");
if (StringUtils.isNotBlank(subject)) {
emailConfiguration.setSubject(subject);
}
+ String port = System.getProperty("error-handler-email.port");
if (StringUtils.isNotBlank(port)) {
emailConfiguration.setPort(Integer.valueOf(port));
}
+ String debug = System.getProperty("error-handler-email.debug");
+ if (StringUtils.isNotBlank(debug)) {
+ emailConfiguration.setDebug(Boolean.parseBoolean(debug));
+ }
return emailConfiguration;
}
}
diff --git a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailConfiguration.java b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailConfiguration.java
index 515b95e..20afa66 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailConfiguration.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailConfiguration.java
@@ -37,6 +37,8 @@ public final class EmailConfiguration {
private String protocol = "smtp";
+ private boolean useSsl;
+
private String subject = "ElasticJob error message";
private String from;
@@ -46,4 +48,6 @@ public final class EmailConfiguration {
private String cc;
private String bcc;
+
+ private boolean debug;
}
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 34702bf..abaafd4 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
@@ -37,6 +37,7 @@ 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;
/**
@@ -79,13 +80,18 @@ public final class EmailJobErrorHandler implements JobErrorHandler {
return "EMAIL";
}
- private Session buildSession() {
+ private synchronized Session buildSession() {
if (null == session) {
Properties props = new Properties();
props.put("mail.smtp.host", emailConfiguration.getHost());
props.put("mail.smtp.port", emailConfiguration.getPort());
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", emailConfiguration.getProtocol());
+ props.setProperty("mail.debug", Boolean.toString(emailConfiguration.isDebug()));
+ if (emailConfiguration.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() {
@@ -97,7 +103,7 @@ public final class EmailJobErrorHandler implements JobErrorHandler {
}
private Message buildMessage(final String content) throws MessagingException {
- MimeMessage message = new MimeMessage(buildSession());
+ MimeMessage message = new MimeMessage(Optional.ofNullable(session).orElseGet(this::buildSession));
message.setFrom(new InternetAddress(emailConfiguration.getFrom()));
message.setSubject(emailConfiguration.getSubject());
message.setSentDate(new Date());
diff --git a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/resources/conf/error-handler-email.yaml b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/resources/conf/error-handler-email.yaml
index 08fcb90..11db315 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-email/src/main/resources/conf/error-handler-email.yaml
+++ b/elasticjob-error-handler/elasticjob-error-handler-email/src/main/resources/conf/error-handler-email.yaml
@@ -21,7 +21,9 @@ email:
username: username
password: password
protocol: smtp
+ useSsl: true
from: xxx
to: xxx
cc: xxx
bcc: xxx
+ debug: true
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 d5a761c..9674751 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
@@ -29,6 +29,7 @@ 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)
@@ -55,6 +56,8 @@ public final class EmailJobErrorHandlerTest {
assertThat(emailConfiguration.getBcc(), equalTo("yaml.bcc@ejob.com"));
assertThat(emailConfiguration.getProtocol(), equalTo("yaml.smtp"));
assertThat(emailConfiguration.getSubject(), equalTo("yaml.subject"));
+ assertTrue(emailConfiguration.isUseSsl());
+ assertTrue(emailConfiguration.isDebug());
}
@Test
@@ -74,6 +77,8 @@ public final class EmailJobErrorHandlerTest {
assertThat(emailConfiguration.getTo(), equalTo("system.to@ejob.com"));
assertThat(emailConfiguration.getCc(), equalTo("system.cc@ejob.com"));
assertThat(emailConfiguration.getProtocol(), equalTo("smtp"));
+ assertTrue(emailConfiguration.isUseSsl());
+ assertTrue(emailConfiguration.isDebug());
}
@Test
@@ -109,6 +114,8 @@ public final class EmailJobErrorHandlerTest {
System.setProperty("error-handler-email.use-system-properties", "true");
System.setProperty("error-handler-email.host", "system.email.com");
System.setProperty("error-handler-email.port", "345");
+ System.setProperty("error-handler-email.protocol", "smtp");
+ System.setProperty("error-handler-email.use-ssl", "true");
System.setProperty("error-handler-email.username", "system.username");
System.setProperty("error-handler-email.password", "system.password");
System.setProperty("error-handler-email.subject", "system.subject");
@@ -116,6 +123,7 @@ public final class EmailJobErrorHandlerTest {
System.setProperty("error-handler-email.to", "system.to@ejob.com");
System.setProperty("error-handler-email.cc", "system.cc@ejob.com");
System.setProperty("error-handler-email.bcc", "system.bcc@ejob.com");
+ System.setProperty("error-handler-email.debug", "true");
}
private void resetSystemProperties() {
diff --git a/elasticjob-error-handler/elasticjob-error-handler-email/src/test/resources/conf/error-handler-email.yaml b/elasticjob-error-handler/elasticjob-error-handler-email/src/test/resources/conf/error-handler-email.yaml
index 4a0c252..6c7e2c1 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-email/src/test/resources/conf/error-handler-email.yaml
+++ b/elasticjob-error-handler/elasticjob-error-handler-email/src/test/resources/conf/error-handler-email.yaml
@@ -26,3 +26,5 @@ email:
to: yaml.to@ejob.com
cc: yaml.cc@ejob.com
bcc: yaml.bcc@ejob.com
+ useSsl: true
+ debug: true