TeslaCN commented on a change in pull request #1550:
URL:
https://github.com/apache/shardingsphere-elasticjob/pull/1550#discussion_r502766310
##########
File path:
elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailConfiguration.java
##########
@@ -50,4 +52,27 @@
private String bcc;
private boolean debug;
+
+ /**
+ * Get email config.
+ *
+ * @param props props
+ * @return email config.
+ */
+ public static EmailConfiguration getByProps(final Properties props) {
+ EmailConfiguration configuration = new EmailConfiguration();
+ configuration.setHost(props.getProperty("email.host"));
+
configuration.setPort(Integer.parseInt(props.getOrDefault("email.port",
"25").toString()));
Review comment:
There are many SMTP servers required secure connection. I think it is
better to let users declare port explicitly.
##########
File path:
elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandler.java
##########
@@ -95,28 +96,35 @@ private String createErrorContext(final String jobName,
final Throwable cause) {
return String.format("Job '%s' exception occur in job processing,
caused by %s", jobName, writer.toString());
}
- private Message createMessage(final String content) throws
MessagingException {
- MimeMessage result = new MimeMessage(session);
- result.setFrom(new InternetAddress(config.getFrom()));
- result.setSubject(config.getSubject());
+ private Message createMessage(final String content, final
EmailConfiguration emailConfiguration) throws MessagingException {
+ MimeMessage result = new
MimeMessage(createSession(emailConfiguration));
+ result.setFrom(new InternetAddress(emailConfiguration.getFrom()));
+ result.setSubject(emailConfiguration.getSubject());
result.setSentDate(new Date());
Multipart multipart = new MimeMultipart();
BodyPart mailBody = new MimeBodyPart();
mailBody.setContent(content, "text/html; charset=utf-8");
multipart.addBodyPart(mailBody);
result.setContent(multipart);
- if (StringUtils.isNotBlank(config.getTo())) {
- result.addRecipient(Message.RecipientType.TO, new
InternetAddress(config.getTo()));
+ String to = emailConfiguration.getTo();
+ if (StringUtils.isNotBlank(to)) {
+ String[] tos = to.split(",");
+ for (String t : tos) {
+ result.addRecipient(Message.RecipientType.TO, new
InternetAddress(t));
+ }
+ }
+ if (StringUtils.isNotBlank(emailConfiguration.getCc())) {
+ result.addRecipient(Message.RecipientType.CC, new
InternetAddress(emailConfiguration.getCc()));
}
- if (StringUtils.isNotBlank(config.getCc())) {
- result.addRecipient(Message.RecipientType.CC, new
InternetAddress(config.getCc()));
+ if (StringUtils.isNotBlank(emailConfiguration.getBcc())) {
+ result.addRecipient(Message.RecipientType.BCC, new
InternetAddress(emailConfiguration.getBcc()));
}
result.saveChanges();
return result;
}
- private void sendMessage(final Message message) throws MessagingException {
- try (Transport transport = session.getTransport()) {
+ private void sendMessage(final Message message, final EmailConfiguration
emailConfiguration) throws MessagingException {
+ try (Transport transport =
createSession(emailConfiguration).getTransport()) {
Review comment:
The method `createSession` is synchronized. I think it is better to
check if session present before invoking synchronized method.
##########
File path:
elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandler.java
##########
@@ -44,48 +45,48 @@
@Slf4j
public final class EmailJobErrorHandler implements JobErrorHandler {
- public static final String CONFIG_PREFIX = "email";
+ private Session session;
- private final EmailConfiguration config;
-
- private final Session session;
-
- public EmailJobErrorHandler() {
- config = EmailConfigurationLoader.unmarshal(CONFIG_PREFIX);
- session = Session.getDefaultInstance(createSessionProperties(),
getSessionAuthenticator());
+ private synchronized Session createSession(final EmailConfiguration
emailConfiguration) {
+ if (session == null) {
Review comment:
Constant on the left.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]