You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/09/21 08:48:24 UTC

[GitHub] [shardingsphere-elasticjob] luky116 commented on a change in pull request #1474: Add EmailJobErrorHandler to support sending email when job error

luky116 commented on a change in pull request #1474:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/1474#discussion_r491880610



##########
File path: elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandler.java
##########
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.elasticjob.error.handler.email;
+
+import com.google.common.base.Preconditions;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler;
+
+import javax.mail.Authenticator;
+import javax.mail.BodyPart;
+import javax.mail.Message;
+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;
+import javax.mail.internet.MimeMultipart;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Date;
+import java.util.Properties;
+
+/**
+ * Job error handler for sending error message by email.
+ */
+@Slf4j
+public final class EmailJobErrorHandler implements JobErrorHandler {
+    
+    public static final String CONFIG_PREFIX = "email";
+    
+    private EmailConfiguration emailConfiguration;
+    
+    private Session session;
+    
+    public EmailJobErrorHandler() {
+        loadConfiguration();
+    }
+    
+    @Override
+    public void handleException(final String jobName, final Throwable cause) {
+        try {
+            Preconditions.checkNotNull(emailConfiguration);
+            String content = buildContent(jobName, cause);
+            Message message = buildMessage(content);
+            sendMessage(message);
+        } catch (final NullPointerException | MessagingException ex) {
+            log.error("Elastic job: email job handler error", ex);
+        }

Review comment:
       if user don't set email configuration, Preconditions.checkNotNull(emailConfiguration) will throws NullPointerException,it menas user don't want email handler. So here will catch NullPointerException.




----------------------------------------------------------------
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:
users@infra.apache.org