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:42:11 UTC

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

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



##########
File path: elasticjob-error-handler/elasticjob-error-handler-email/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/email/ConfigurationLoader.java
##########
@@ -0,0 +1,45 @@
+/*
+ * 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 lombok.NoArgsConstructor;
+import org.apache.shardingsphere.elasticjob.infra.yaml.YamlEngine;
+
+import java.io.InputStream;
+
+/**
+ * Job error configuration loader.
+ */
+@NoArgsConstructor

Review comment:
       "Private" access required.

##########
File path: elasticjob-error-handler/elasticjob-error-handler-email/pom.xml
##########
@@ -27,4 +27,38 @@
     </parent>
     
     <artifactId>elasticjob-error-handler-email</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere.elasticjob</groupId>
+            <artifactId>elasticjob-error-handler-general</artifactId>
+            <version>${project.parent.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.sun.mail</groupId>
+            <artifactId>javax.mail</artifactId>
+            <version>1.6.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-inline</artifactId>
+        </dependency>

Review comment:
       "Test" scope is required.

##########
File path: elasticjob-error-handler/elasticjob-error-handler-general/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/JobErrorHandlerFactory.java
##########
@@ -56,5 +56,5 @@ public static JobErrorHandler getHandler(final String type) {
             throw new JobConfigurationException("Can not find job error handler type '%s'.", type);
         }
         return HANDLERS.get(type);
-    } 
+    }

Review comment:
       Unnecessary changes.

##########
File path: elasticjob-error-handler/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/JobErrorHandlerFactoryTest.java
##########
@@ -7,7 +7,7 @@
  * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
- *  
+ *

Review comment:
       Unnecessary changes.

##########
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:
       Catch 'NullPointerException' seems weird.

##########
File path: elasticjob-error-handler/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/impl/IgnoreJobErrorHandlerTest.java
##########
@@ -20,7 +20,7 @@
 import org.junit.Test;
 
 public final class IgnoreJobErrorHandlerTest {
-    
+

Review comment:
       Unnecessary changes.

##########
File path: elasticjob-error-handler/elasticjob-error-handler-email/src/main/resources/META-INF/services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
##########
@@ -15,6 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.elasticjob.infra.handler.error.impl.LogJobErrorHandler
-org.apache.shardingsphere.elasticjob.infra.handler.error.impl.IgnoreJobErrorHandler
-org.apache.shardingsphere.elasticjob.infra.handler.error.impl.ThrowJobErrorHandler
+org.apache.shardingsphere.elasticjob.error.handler.email.EmailJobErrorHandler

Review comment:
       Need a blank line.

##########
File path: elasticjob-error-handler/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/impl/IgnoreJobErrorHandlerTest.java
##########
@@ -7,7 +7,7 @@
  * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
- *  
+ *

Review comment:
       Unnecessary changes.




----------------------------------------------------------------
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