TeslaCN commented on a change in pull request #1462: URL: https://github.com/apache/shardingsphere-elasticjob/pull/1462#discussion_r489421665
########## File path: elasticjob-error-handler/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/impl/DingtalkJobErrorHandlerTest.java ########## @@ -0,0 +1,44 @@ +/* + * 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.impl; + +import org.apache.shardingsphere.elasticjob.error.handler.env.DingtalkEnvironment; +import org.junit.Test; + +public final class DingtalkJobErrorHandlerTest { + + @Test + public void assertHandleException() { + DingtalkJobErrorHandler actual = new DingtalkJobErrorHandler(); + Throwable cause = new RuntimeException("test"); + actual.handleException("test_job", cause); + } + + @Test + public void assertHandleExceptionWithSystemProperties() { + System.getProperties().setProperty(DingtalkEnvironment.EnvironmentArgument.WEBHOOK.getKey(), + "https://oapi.dingtalk.com/robot/send?access_token=42eead064e81ce81fc6af2c107fbe10a4339a3d40a7db8abf5b34d8261527a3f"); + System.getProperties().setProperty(DingtalkEnvironment.EnvironmentArgument.SECRET.getKey(), + "SEC0b0a6b13b6823b95737dd83491c23adee5d8a7a649899a12217e038eddc84ff4"); + System.getProperties().setProperty(DingtalkEnvironment.EnvironmentArgument.KEYWORD.getKey(), "关键字1111"); Review comment: English is required. ########## File path: elasticjob-error-handler/elasticjob-error-handler-dingtalk/pom.xml ########## @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.shardingsphere.elasticjob</groupId> + <artifactId>elasticjob-error-handler</artifactId> + <version>3.0.0-beta-SNAPSHOT</version> + </parent> + <artifactId>elasticjob-error-handler-dingtalk</artifactId> + <name>${project.artifactId}</name> + + <dependencies> + <dependency> + <groupId>org.apache.shardingsphere.elasticjob</groupId> + <artifactId>elasticjob-infra-common</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> Review comment: "provided" scope is recommended. ########## File path: elasticjob-error-handler/elasticjob-error-handler-dingtalk/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/impl/DingtalkJobErrorHandler.java ########## @@ -0,0 +1,144 @@ +/* + * 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.impl; + +import com.google.common.base.Strings; +import com.google.common.collect.Maps; +import com.google.gson.JsonObject; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.elasticjob.error.handler.config.DingtalkConfiguration; +import org.apache.shardingsphere.elasticjob.error.handler.env.DingtalkEnvironment; +import org.apache.shardingsphere.elasticjob.infra.exception.JobExecutionException; +import org.apache.shardingsphere.elasticjob.infra.handler.error.JobErrorHandler; +import org.apache.shardingsphere.elasticjob.infra.json.GsonFactory; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; +import java.util.Collections; +import java.util.Map; + +/** + * Job error handler for dingtalk error message. + */ +@Slf4j +public final class DingtalkJobErrorHandler implements JobErrorHandler { + + private DingtalkConfiguration dingtalkConfiguration; + + @Override + public void handleException(final String jobName, final Throwable cause) { + if (dingtalkConfiguration == null) { + dingtalkConfiguration = DingtalkEnvironment.getINSTANCE().getDingtalkConfiguration(); + } + HttpURLConnection connection = null; + try { + URL url = getUrl(); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setConnectTimeout(dingtalkConfiguration.getConnectTimeout()); + connection.setReadTimeout(dingtalkConfiguration.getReadTimeout()); + connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); + connection.connect(); + OutputStream outputStream = connection.getOutputStream(); + String msg = getMsg(jobName, cause); + String paramJson = getParamJson(msg); + outputStream.write(paramJson.getBytes(StandardCharsets.UTF_8)); + int code = connection.getResponseCode(); + if (HttpURLConnection.HTTP_OK == code) { + InputStream resultInputStream = connection.getInputStream(); + StringBuilder result = new StringBuilder(); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) { + String line; + while (null != (line = bufferedReader.readLine())) { + result.append(line); + } + } + JsonObject resp = GsonFactory.getGson().fromJson(result.toString(), JsonObject.class); + if (!"0".equals(resp.get("errcode").getAsString())) { + log.error("Job '{}' exception occur in job processing, But the notification Dingtalk failure, error is : {}", jobName, resp.get("errmsg").getAsString(), cause); + } + } + } catch (IOException | NoSuchAlgorithmException | InvalidKeyException ex) { + throw new JobExecutionException(ex); + } finally { + if (null != connection) { + connection.disconnect(); + } + } + } + + private String getParamJson(final String msg) { + Map<String, Object> param = Maps.newLinkedHashMap(); Review comment: <p><b>Note for Java 7 and later:</b> this method is now unnecessary and should be treated as deprecated. Instead, use the {@code LinkedHashMap} constructor directly, taking advantage of the new <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>. ---------------------------------------------------------------- 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]
