{\"mysql service name\":\"mysql200\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"190\",\"port\":\"3306\",\"no index of number\":\"80\"}
"
- + "
{\"mysql service name\":\"mysql210\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"90\",\"port\":\"3306\",\"no index of number\":\"10\"}
"
- + EmailConstants.TABLE_BODY_HTML_TAIL;
+ + "
{\"mysql service name\":\"mysql200\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"190\",\"port\":\"3306\",\"no index of number\":\"80\"}
"
+ + "
{\"mysql service name\":\"mysql210\",\"mysql address\":\"192.168.xx.xx\",\"database client connections\":\"90\",\"port\":\"3306\",\"no index of number\":\"10\"}
"
+ + EmailConstants.TABLE_BODY_HTML_TAIL;
}
}
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/pom.xml
new file mode 100644
index 0000000000000000000000000000000000000000..210bd734540524765292f5c1cc4fcd26c067a48b
--- /dev/null
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/pom.xml
@@ -0,0 +1,38 @@
+
+
+
+
+ dolphinscheduler-alert-plugins
+ org.apache.dolphinscheduler
+ 2.0.10-SNAPSHOT
+
+ 4.0.0
+ dolphinscheduler-alert-feishu
+ jar
+
+
+
+ org.apache.httpcomponents
+ httpclient
+
+
+ com.google.guava
+ guava
+
+
+
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java
similarity index 81%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java
index 3bbdaa999756c36940f63f5f222d2d510310f8b1..509eebe6157cb1c164fbb0f258caf0006cfe6a64 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannel.java
@@ -17,17 +17,16 @@
package org.apache.dolphinscheduler.plugin.alert.feishu;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertInfo;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertInfo;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.util.Map;
-public class FeiShuAlertChannel implements AlertChannel {
+public final class FeiShuAlertChannel implements AlertChannel {
@Override
public AlertResult process(AlertInfo alertInfo) {
-
AlertData alertData = alertInfo.getAlertData();
Map paramsMap = alertInfo.getAlertParams();
if (null == paramsMap) {
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactory.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactory.java
similarity index 52%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactory.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactory.java
index 6eb3376a4d54edb12c0798fcf2243884b2055e36..da78b9f39b5f96910834b51d3b813ecde0dc9b57 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactory.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactory.java
@@ -22,59 +22,62 @@ import static org.apache.dolphinscheduler.spi.utils.Constants.STRING_NO;
import static org.apache.dolphinscheduler.spi.utils.Constants.STRING_TRUE;
import static org.apache.dolphinscheduler.spi.utils.Constants.STRING_YES;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory;
-import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
import org.apache.dolphinscheduler.spi.params.PasswordParam;
-import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
+import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
import java.util.Arrays;
import java.util.List;
-public class FeiShuAlertChannelFactory implements AlertChannelFactory {
+import com.google.auto.service.AutoService;
+
+@AutoService(AlertChannelFactory.class)
+public final class FeiShuAlertChannelFactory implements AlertChannelFactory {
@Override
- public String getName() {
+ public String name() {
return "Feishu";
}
@Override
- public List getParams() {
+ public List params() {
InputParam webHookParam = InputParam.newBuilder(FeiShuParamsConstants.NAME_WEB_HOOK, FeiShuParamsConstants.WEB_HOOK)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
RadioParam isEnableProxy =
- RadioParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PROXY_ENABLE, FeiShuParamsConstants.NAME_FEI_SHU_PROXY_ENABLE)
- .addParamsOptions(new ParamsOptions(STRING_YES, STRING_TRUE, false))
- .addParamsOptions(new ParamsOptions(STRING_NO, STRING_FALSE, false))
- .setValue(STRING_TRUE)
- .addValidate(Validate.newBuilder()
- .setRequired(false)
- .build())
- .build();
+ RadioParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PROXY_ENABLE, FeiShuParamsConstants.FEI_SHU_PROXY_ENABLE)
+ .addParamsOptions(new ParamsOptions(STRING_YES, STRING_TRUE, false))
+ .addParamsOptions(new ParamsOptions(STRING_NO, STRING_FALSE, false))
+ .setValue(STRING_TRUE)
+ .addValidate(Validate.newBuilder()
+ .setRequired(false)
+ .build())
+ .build();
InputParam proxyParam =
- InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PROXY, FeiShuParamsConstants.FEI_SHU_PROXY)
- .addValidate(Validate.newBuilder()
- .setRequired(false).build())
- .build();
+ InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PROXY, FeiShuParamsConstants.FEI_SHU_PROXY)
+ .addValidate(Validate.newBuilder()
+ .setRequired(false).build())
+ .build();
InputParam portParam = InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PORT, FeiShuParamsConstants.FEI_SHU_PORT)
- .addValidate(Validate.newBuilder()
- .setRequired(false).build())
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(false).build())
+ .build();
InputParam userParam =
- InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_USER, FeiShuParamsConstants.FEI_SHU_USER)
- .addValidate(Validate.newBuilder()
- .setRequired(false).build())
- .build();
+ InputParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_USER, FeiShuParamsConstants.FEI_SHU_USER)
+ .addValidate(Validate.newBuilder()
+ .setRequired(false).build())
+ .build();
PasswordParam passwordParam = PasswordParam.newBuilder(FeiShuParamsConstants.NAME_FEI_SHU_PASSWORD, FeiShuParamsConstants.FEI_SHU_PASSWORD)
- .setPlaceholder("if enable use authentication, you need input password")
- .build();
+ .setPlaceholder("if enable use authentication, you need input password")
+ .build();
return Arrays.asList(webHookParam, isEnableProxy, proxyParam, portParam, userParam, passwordParam);
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuParamsConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuParamsConstants.java
similarity index 91%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuParamsConstants.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuParamsConstants.java
index 0b3c329792140ef5403217230c3242f6a7c368a7..bcbad1786010a8ee423dd4d37d2abbe6a8beb357 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuParamsConstants.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuParamsConstants.java
@@ -17,12 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.feishu;
-public class FeiShuParamsConstants {
-
- private FeiShuParamsConstants() {
- throw new IllegalStateException("Utility class");
- }
-
+public final class FeiShuParamsConstants {
static final String WEB_HOOK = "webhook";
static final String NAME_WEB_HOOK = "webHook";
@@ -46,4 +41,8 @@ public class FeiShuParamsConstants {
static final String FEI_SHU_PASSWORD = "password";
static final String NAME_FEI_SHU_PASSWORD = "password";
+
+ private FeiShuParamsConstants() {
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
+ }
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java
similarity index 66%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java
index 8fdafe71829640db909c5fd681584aedfa217eba..dd40c755ba875879efb4b04d93a0a688232a8d45 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSender.java
@@ -17,8 +17,8 @@
package org.apache.dolphinscheduler.plugin.alert.feishu;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.apache.commons.codec.binary.StringUtils;
@@ -31,23 +31,18 @@ import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonProperty;
-public class FeiShuSender {
-
- private static final Logger logger = LoggerFactory.getLogger(FeiShuSender.class);
-
- private String url;
-
- private Boolean enableProxy;
+public final class FeiShuSender {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(FeiShuSender.class);
+ private final String url;
+ private final Boolean enableProxy;
private String proxy;
@@ -70,7 +65,6 @@ public class FeiShuSender {
}
private static String textToJsonString(AlertData alertData) {
-
Map items = new HashMap<>(2);
items.put("msg_type", "text");
Map textContent = new HashMap<>();
@@ -87,14 +81,14 @@ public class FeiShuSender {
if (org.apache.dolphinscheduler.spi.utils.StringUtils.isBlank(result)) {
alertResult.setMessage("send fei shu msg error");
- logger.info("send fei shu msg error,fei shu server resp is null");
+ log.info("send fei shu msg error,fei shu server resp is null");
return alertResult;
}
FeiShuSendMsgResponse sendMsgResponse = JSONUtils.parseObject(result, FeiShuSendMsgResponse.class);
if (null == sendMsgResponse) {
alertResult.setMessage("send fei shu msg fail");
- logger.info("send fei shu msg error,resp error");
+ log.info("send fei shu msg error,resp error");
return alertResult;
}
if (sendMsgResponse.statusCode == 0) {
@@ -103,7 +97,7 @@ public class FeiShuSender {
return alertResult;
}
alertResult.setMessage(String.format("alert send fei shu msg error : %s", sendMsgResponse.getStatusMessage()));
- logger.info("alert send fei shu msg error : {} ,Extra : {} ", sendMsgResponse.getStatusMessage(), sendMsgResponse.getExtra());
+ log.info("alert send fei shu msg error : {} ,Extra : {} ", sendMsgResponse.getStatusMessage(), sendMsgResponse.getExtra());
return alertResult;
}
@@ -118,9 +112,7 @@ public class FeiShuSender {
StringBuilder contents = new StringBuilder(100);
contents.append(String.format("`%s`%n", alertData.getTitle()));
for (Map map : list) {
- Iterator> entries = map.entrySet().iterator();
- while (entries.hasNext()) {
- Entry entry = entries.next();
+ for (Entry entry : (Iterable>) map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
contents.append(key + ":" + value);
@@ -138,7 +130,7 @@ public class FeiShuSender {
String resp = sendMsg(alertData);
return checkSendFeiShuSendMsgResult(resp);
} catch (Exception e) {
- logger.info("send fei shu alert msg exception : {}", e.getMessage());
+ log.info("send fei shu alert msg exception : {}", e.getMessage());
alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setMessage("send fei shu alert fail.");
@@ -161,7 +153,7 @@ public class FeiShuSender {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
- logger.error("send feishu message error, return http status code: {} ", statusCode);
+ log.error("send feishu message error, return http status code: {} ", statusCode);
}
String resp;
try {
@@ -171,14 +163,14 @@ public class FeiShuSender {
} finally {
response.close();
}
- logger.info("Fei Shu send title :{} ,content :{}, resp: {}", alertData.getTitle(), alertData.getContent(), resp);
+ log.info("Fei Shu send title :{} ,content :{}, resp: {}", alertData.getTitle(), alertData.getContent(), resp);
return resp;
} finally {
httpClient.close();
}
}
- public static class FeiShuSendMsgResponse {
+ static final class FeiShuSendMsgResponse {
@JsonProperty("Extra")
private String extra;
@JsonProperty("StatusCode")
@@ -186,29 +178,76 @@ public class FeiShuSender {
@JsonProperty("StatusMessage")
private String statusMessage;
+ public FeiShuSendMsgResponse() {
+ }
+
public String getExtra() {
- return extra;
+ return this.extra;
}
+ @JsonProperty("Extra")
public void setExtra(String extra) {
this.extra = extra;
}
public Integer getStatusCode() {
- return statusCode;
+ return this.statusCode;
}
+ @JsonProperty("StatusCode")
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getStatusMessage() {
- return statusMessage;
+ return this.statusMessage;
}
+ @JsonProperty("StatusMessage")
public void setStatusMessage(String statusMessage) {
this.statusMessage = statusMessage;
}
- }
+ public boolean equals(final Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof FeiShuSendMsgResponse)) {
+ return false;
+ }
+ final FeiShuSendMsgResponse other = (FeiShuSendMsgResponse) o;
+ final Object this$extra = this.getExtra();
+ final Object other$extra = other.getExtra();
+ if (this$extra == null ? other$extra != null : !this$extra.equals(other$extra)) {
+ return false;
+ }
+ final Object this$statusCode = this.getStatusCode();
+ final Object other$statusCode = other.getStatusCode();
+ if (this$statusCode == null ? other$statusCode != null : !this$statusCode.equals(other$statusCode)) {
+ return false;
+ }
+ final Object this$statusMessage = this.getStatusMessage();
+ final Object other$statusMessage = other.getStatusMessage();
+ if (this$statusMessage == null ? other$statusMessage != null : !this$statusMessage.equals(other$statusMessage)) {
+ return false;
+ }
+ return true;
+ }
+
+ public int hashCode() {
+ final int PRIME = 59;
+ int result = 1;
+ final Object $extra = this.getExtra();
+ result = result * PRIME + ($extra == null ? 43 : $extra.hashCode());
+ final Object $statusCode = this.getStatusCode();
+ result = result * PRIME + ($statusCode == null ? 43 : $statusCode.hashCode());
+ final Object $statusMessage = this.getStatusMessage();
+ result = result * PRIME + ($statusMessage == null ? 43 : $statusMessage.hashCode());
+ return result;
+ }
+
+ public String toString() {
+ return "FeiShuSender.FeiShuSendMsgResponse(extra=" + this.getExtra() + ", statusCode=" + this.getStatusCode() + ", statusMessage=" + this.getStatusMessage() + ")";
+ }
+ }
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/HttpRequestUtil.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/HttpRequestUtil.java
similarity index 92%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/HttpRequestUtil.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/HttpRequestUtil.java
index 3d143579d49a7fa08439e51625f0bd99a26ad2dc..eca9b07d15ba1e5e95ec640b705c1b878e50b2e4 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/HttpRequestUtil.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/main/java/org/apache/dolphinscheduler/plugin/alert/feishu/HttpRequestUtil.java
@@ -28,7 +28,10 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
-public class HttpRequestUtil {
+public final class HttpRequestUtil {
+ private HttpRequestUtil() {
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
+ }
public static CloseableHttpClient getHttpClient(boolean enableProxy, String proxy, Integer port, String user, String password) {
if (enableProxy) {
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactoryTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactoryTest.java
similarity index 92%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactoryTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactoryTest.java
index d73355dbc07755d3356969bc1b821a63586cb31d..ce51e00474f4a74a7e1b82cbe6ee87e52f2fd56a 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactoryTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuAlertChannelFactoryTest.java
@@ -17,7 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.feishu;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
@@ -31,7 +31,7 @@ public class FeiShuAlertChannelFactoryTest {
@Test
public void testGetParams() {
FeiShuAlertChannelFactory feiShuAlertChannelFactory = new FeiShuAlertChannelFactory();
- List params = feiShuAlertChannelFactory.getParams();
+ List params = feiShuAlertChannelFactory.params();
JSONUtils.toJsonString(params);
Assert.assertEquals(6, params.size());
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java
similarity index 74%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java
index 05110d42fee76c2e857007700ee3d112d0708c24..8d5dfdbc6ab1814f1fc376c1f9d5bd1865f04a0f 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-feishu/src/test/java/org/apache/dolphinscheduler/plugin/alert/feishu/FeiShuSenderTest.java
@@ -17,8 +17,8 @@
package org.apache.dolphinscheduler.plugin.alert.feishu;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.util.HashMap;
import java.util.Map;
@@ -50,23 +50,23 @@ public class FeiShuSenderTest {
@Test
public void testFormatContent() {
String alertMsg = "[\n"
- + " {\n"
- + " \"owner\": \"dolphinscheduler\",\n"
- + " \"processEndTime\": \"2021-01-29 19:01:11\",\n"
- + " \"processHost\": \"10.81.129.4:5678\",\n"
- + " \"processId\": 2926,\n"
- + " \"processName\": \"3-20210129190038108\",\n"
- + " \"processStartTime\": \"2021-01-29 19:00:38\",\n"
- + " \"processState\": \"SUCCESS\",\n"
- + " \"processType\": \"START_PROCESS\",\n"
- + " \"projectId\": 2,\n"
- + " \"projectName\": \"testdelproject\",\n"
- + " \"recovery\": \"NO\",\n"
- + " \"retryTimes\": 0,\n"
- + " \"runTimes\": 1,\n"
- + " \"taskId\": 0\n"
- + " }\n"
- + "]";
+ + " {\n"
+ + " \"owner\": \"dolphinscheduler\",\n"
+ + " \"processEndTime\": \"2021-01-29 19:01:11\",\n"
+ + " \"processHost\": \"10.81.129.4:5678\",\n"
+ + " \"processId\": 2926,\n"
+ + " \"processName\": \"3-20210129190038108\",\n"
+ + " \"processStartTime\": \"2021-01-29 19:00:38\",\n"
+ + " \"processState\": \"SUCCESS\",\n"
+ + " \"processType\": \"START_PROCESS\",\n"
+ + " \"projectId\": 2,\n"
+ + " \"projectName\": \"testdelproject\",\n"
+ + " \"recovery\": \"NO\",\n"
+ + " \"retryTimes\": 0,\n"
+ + " \"runTimes\": 1,\n"
+ + " \"taskId\": 0\n"
+ + " }\n"
+ + "]";
AlertData alertData = new AlertData();
alertData.setTitle("");
alertData.setContent(alertMsg);
@@ -90,7 +90,7 @@ public class FeiShuSenderTest {
AlertResult alertResult = feiShuSender.checkSendFeiShuSendMsgResult("");
Assert.assertFalse(Boolean.valueOf(alertResult.getStatus()));
AlertResult alertResult2 = feiShuSender.checkSendFeiShuSendMsgResult("123");
- Assert.assertEquals("send fei shu msg fail",alertResult2.getMessage());
+ Assert.assertEquals("send fei shu msg fail", alertResult2.getMessage());
String response = "{\"StatusCode\":\"0\",\"extra\":\"extra\",\"StatusMessage\":\"StatusMessage\"}";
AlertResult alertResult3 = feiShuSender.checkSendFeiShuSendMsgResult(response);
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/pom.xml
new file mode 100644
index 0000000000000000000000000000000000000000..f87f23bd7cd75ca3e5c280f75caddda2662f38b6
--- /dev/null
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/pom.xml
@@ -0,0 +1,45 @@
+
+
+
+
+ dolphinscheduler-alert-plugins
+ org.apache.dolphinscheduler
+ 2.0.10-SNAPSHOT
+
+ 4.0.0
+ dolphinscheduler-alert-http
+ jar
+
+
+
+ com.google.guava
+ guava
+
+
+
+ org.apache.httpcomponents
+ httpclient
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ provided
+
+
+
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java
similarity index 78%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java
index cb550b75414494a7fce31c20ff1b8528b39fe413..14b416785dde7d184ff1dc39d31b7e2ee03a9d46 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannel.java
@@ -17,20 +17,16 @@
package org.apache.dolphinscheduler.plugin.alert.http;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertInfo;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertInfo;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.util.Map;
-/**
- * http alert channel,use sms message to seed the alertInfo
- */
-public class HttpAlertChannel implements AlertChannel {
+public final class HttpAlertChannel implements AlertChannel {
@Override
public AlertResult process(AlertInfo alertInfo) {
-
AlertData alertData = alertInfo.getAlertData();
Map paramsMap = alertInfo.getAlertParams();
if (null == paramsMap) {
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
similarity index 52%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
index d54885eb7feda2c8f07b933af93178cb067bd085..28b4c16e72e70ec763538c9f840b8b4a219cc9ed 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
@@ -17,56 +17,56 @@
package org.apache.dolphinscheduler.plugin.alert.http;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory;
-import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
+import org.apache.dolphinscheduler.spi.params.input.InputParam;
import java.util.Arrays;
import java.util.List;
-/**
- * http alert factory
- */
-public class HttpAlertChannelFactory implements AlertChannelFactory {
+import com.google.auto.service.AutoService;
+
+@AutoService(AlertChannelFactory.class)
+public final class HttpAlertChannelFactory implements AlertChannelFactory {
@Override
- public String getName() {
+ public String name() {
return "Http";
}
@Override
- public List getParams() {
+ public List params() {
InputParam url = InputParam.newBuilder(HttpAlertConstants.URL, HttpAlertConstants.URL)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam headerParams = InputParam.newBuilder(HttpAlertConstants.HEADER_PARAMS, HttpAlertConstants.HEADER_PARAMS)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam bodyParams = InputParam.newBuilder(HttpAlertConstants.BODY_PARAMS, HttpAlertConstants.BODY_PARAMS)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam contentField = InputParam.newBuilder(HttpAlertConstants.CONTENT_FIELD, HttpAlertConstants.CONTENT_FIELD)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam requestType = InputParam.newBuilder(HttpAlertConstants.REQUEST_TYPE, HttpAlertConstants.REQUEST_TYPE)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
return Arrays.asList(url, requestType, headerParams, bodyParams, contentField);
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
similarity index 89%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
index 965860d868a8e984d8df7900085ddaff7af1cb17..58cac57c3aefe1d76350136ccd8ae2a41edc46bb 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
@@ -17,11 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.http;
-public class HttpAlertConstants {
-
- private HttpAlertConstants() {
- }
-
+public final class HttpAlertConstants {
public static final String URL = "url";
public static final String HEADER_PARAMS = "headerParams";
@@ -32,4 +28,7 @@ public class HttpAlertConstants {
public static final String REQUEST_TYPE = "requestType";
+ private HttpAlertConstants() {
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
+ }
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
similarity index 89%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
index 7b9190494bc0738edf113ec3e0aa5327ea8b48ab..a04437bb8c4fe86b9c44c0fde1b17c2fabb62630 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
@@ -17,10 +17,10 @@
package org.apache.dolphinscheduler.plugin.alert.http;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.apache.dolphinscheduler.spi.utils.StringUtils;
-
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
@@ -30,48 +30,29 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
import java.util.HashMap;
import java.util.Map;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.fasterxml.jackson.databind.node.ObjectNode;
-
-/**
- * http send message
- */
-public class HttpSender {
-
- public static final Logger logger = LoggerFactory.getLogger(HttpSender.class);
-
- private String url;
-
- private final String headerParams;
-
- private final String bodyParams;
-
- private final String contentField;
-
- private final String requestType;
-
- private HttpRequestBase httpRequest;
-
-
+public final class HttpSender {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(HttpSender.class);
private static final String URL_SPLICE_CHAR = "?";
-
/**
* request type post
*/
private static final String REQUEST_TYPE_POST = "POST";
-
/**
* request type get
*/
private static final String REQUEST_TYPE_GET = "GET";
-
private static final String DEFAULT_CHARSET = "utf-8";
+ private final String headerParams;
+ private final String bodyParams;
+ private final String contentField;
+ private final String requestType;
+ private String url;
+ private HttpRequestBase httpRequest;
public HttpSender(Map paramsMap) {
@@ -102,7 +83,7 @@ public class HttpSender {
alertResult.setStatus("true");
alertResult.setMessage(resp);
} catch (Exception e) {
- logger.error("send http alert msg exception : {}", e.getMessage());
+ log.error("send http alert msg exception : {}", e.getMessage());
alertResult.setStatus("false");
alertResult.setMessage("send http request alert fail.");
}
@@ -157,16 +138,15 @@ public class HttpSender {
/**
* set body params
*/
- private void setMsgInRequestBody(String msg) {
+ private void setMsgInRequestBody(String msg) {
ObjectNode objectNode = JSONUtils.parseObject(bodyParams);
//set msg content field
objectNode.put(contentField, msg);
try {
- StringEntity entity = new StringEntity(bodyParams, DEFAULT_CHARSET);
- ((HttpPost)httpRequest).setEntity(entity);
+ StringEntity entity = new StringEntity(JSONUtils.toJsonString(objectNode), DEFAULT_CHARSET);
+ ((HttpPost) httpRequest).setEntity(entity);
} catch (Exception e) {
- logger.error("send http alert msg exception : {}", e.getMessage());
+ log.error("send http alert msg exception : {}", e.getMessage());
}
}
-
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
similarity index 93%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
index 25181ebd2616c4e9278624318c31a9ae718b3c90..089f78f3af1afb38020ad45b01b8cb8664325b72 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
@@ -17,7 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.http;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import java.util.List;
@@ -26,9 +26,6 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-/**
- * HttpAlertChannelFactory UT
- */
public class HttpAlertChannelFactoryTest {
private HttpAlertChannelFactory httpAlertChannelFactory;
@@ -41,7 +38,7 @@ public class HttpAlertChannelFactoryTest {
@Test
public void getParamsTest() {
- List pluginParamsList = httpAlertChannelFactory.getParams();
+ List pluginParamsList = httpAlertChannelFactory.params();
Assert.assertEquals(5, pluginParamsList.size());
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
similarity index 70%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
index 2d29407e71914292180d599dcbb43f373729a1fb..ca63902aae341fc4aaa25a00cdd9405bb0df0b6d 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
@@ -17,13 +17,13 @@
package org.apache.dolphinscheduler.plugin.alert.http;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertInfo;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
-import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertInfo;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
+import org.apache.dolphinscheduler.spi.params.input.InputParam;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import java.util.ArrayList;
@@ -33,9 +33,6 @@ import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
-/**
- * HttpAlertChannel UT
- */
public class HttpAlertChannelTest {
@Test
@@ -71,29 +68,29 @@ public class HttpAlertChannelTest {
List paramsList = new ArrayList<>();
InputParam urlParam = InputParam.newBuilder("url", "url")
- .setValue("http://www.baidu.com")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .build();
+ .setValue("http://www.baidu.com")
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .build();
InputParam headerParams = InputParam.newBuilder("headerParams", "headerParams")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue("{\"Content-Type\":\"application/json\"}")
- .build();
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue("{\"Content-Type\":\"application/json\"}")
+ .build();
InputParam bodyParams = InputParam.newBuilder("bodyParams", "bodyParams")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue("{\"number\":\"13457654323\"}")
- .build();
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue("{\"number\":\"13457654323\"}")
+ .build();
InputParam content = InputParam.newBuilder("contentField", "contentField")
- .setValue("content")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .build();
+ .setValue("content")
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .build();
InputParam requestType = InputParam.newBuilder("requestType", "requestType")
- .setValue("POST")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .build();
+ .setValue("POST")
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .build();
paramsList.add(urlParam);
paramsList.add(headerParams);
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
similarity index 87%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
index d59c4d47bb06fe305f546338f754a94baef94a81..fa0dfe80f16b67a7142ed830cadf2e676f8a786c 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
@@ -17,30 +17,26 @@
package org.apache.dolphinscheduler.plugin.alert.http;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
+import org.junit.Assert;
+import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * HttpSender UT
- */
public class HttpSenderTest {
@Test
public void sendTest() {
-
Map paramsMap = new HashMap<>();
- paramsMap.put(HttpAlertConstants.URL, "http://www.baidu.com");
+ paramsMap.put(HttpAlertConstants.URL, "https://httpbin.org/post");
paramsMap.put(HttpAlertConstants.REQUEST_TYPE, "POST");
paramsMap.put(HttpAlertConstants.HEADER_PARAMS, "{\"Content-Type\":\"application/json\"}");
paramsMap.put(HttpAlertConstants.BODY_PARAMS, "{\"number\":\"13457654323\"}");
paramsMap.put(HttpAlertConstants.CONTENT_FIELD, "content");
HttpSender httpSender = new HttpSender(paramsMap);
AlertResult alertResult = httpSender.send("Fault tolerance warning");
+ Assert.assertTrue(alertResult.getMessage().contains("\"content\": \"Fault tolerance warning\""));
Assert.assertEquals("true", alertResult.getStatus());
}
}
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/pom.xml
new file mode 100644
index 0000000000000000000000000000000000000000..1cd731f839c55235ccbcd5ea2feba723056d7fea
--- /dev/null
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/pom.xml
@@ -0,0 +1,34 @@
+
+
+
+
+ dolphinscheduler-alert-plugins
+ org.apache.dolphinscheduler
+ 2.0.10-SNAPSHOT
+
+ 4.0.0
+ dolphinscheduler-alert-script
+ jar
+
+
+
+ com.google.guava
+ guava
+
+
+
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java
similarity index 88%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java
index b8816724c7732a727191f9f19619e7adcdb3db4e..51e567b47e74697d9d7d59756482ba0f74bee293 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java
@@ -17,13 +17,9 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-/**
- * OSUtils
- */
-public class OSUtils {
-
+public final class OSUtils {
private OSUtils() {
- throw new UnsupportedOperationException("Construct OSUtils");
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
static Boolean isWindows() {
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java
similarity index 84%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java
index d63a350051dc9d16ae977038f827e77a634ff76d..7008267877a3efa78c99cce15fbcb19a99d7c8b8 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java
@@ -20,17 +20,12 @@ package org.apache.dolphinscheduler.plugin.alert.script;
import java.io.IOException;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-/**
- * ProcessUtils
- */
-public class ProcessUtils {
-
- private static final Logger logger = LoggerFactory.getLogger(ProcessUtils.class);
+public final class ProcessUtils {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(ProcessUtils.class);
private ProcessUtils() {
- throw new IllegalStateException("Utility class");
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
/**
@@ -53,7 +48,7 @@ public class ProcessUtils {
errorStreamGobbler.start();
return process.waitFor();
} catch (IOException | InterruptedException e) {
- logger.error("execute alert script error {}", e.getMessage());
+ log.error("execute alert script error {}", e.getMessage());
Thread.currentThread().interrupt();
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
similarity index 77%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
index af0c27615febd4ce8f9b819584411caf78751467..bd52955b747b728ed14c4af020b94a8d1a934b17 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
@@ -17,18 +17,14 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertInfo;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertInfo;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.util.Map;
-/**
- * ScriptAlertChannel
- */
-public class ScriptAlertChannel implements AlertChannel {
-
+public final class ScriptAlertChannel implements AlertChannel {
@Override
public AlertResult process(AlertInfo alertinfo) {
AlertData alertData = alertinfo.getAlertData();
@@ -36,6 +32,6 @@ public class ScriptAlertChannel implements AlertChannel {
if (null == paramsMap) {
return new AlertResult("false", "script params is null");
}
- return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle(),alertData.getContent());
+ return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle(), alertData.getContent());
}
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java
similarity index 52%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java
index 67cfc3931f384ef2d5b11e11f9ca5f83cad77c34..aacde95ba11303777cd3f1617c809b9029e30ee7 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java
@@ -17,49 +17,49 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory;
-import org.apache.dolphinscheduler.spi.params.input.InputParam;
-import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
+import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
import java.util.Arrays;
import java.util.List;
-/**
- * ScriptAlertChannelFactory
- */
-public class ScriptAlertChannelFactory implements AlertChannelFactory {
+import com.google.auto.service.AutoService;
+@AutoService(AlertChannelFactory.class)
+public final class ScriptAlertChannelFactory implements AlertChannelFactory {
@Override
- public String getName() {
+ public String name() {
return "Script";
}
@Override
- public List getParams() {
+ public List params() {
InputParam scriptUserParam = InputParam.newBuilder(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS, ScriptParamsConstants.SCRIPT_USER_PARAMS)
- .addValidate(Validate.newBuilder()
- .setRequired(false)
- .build())
- .setPlaceholder("please enter your custom parameters, which will be passed to you when calling your script")
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(false)
+ .build())
+ .setPlaceholder("please enter your custom parameters, which will be passed to you when calling your script")
+ .build();
// need check file type and file exist
InputParam scriptPathParam = InputParam.newBuilder(ScriptParamsConstants.NAME_SCRIPT_PATH, ScriptParamsConstants.SCRIPT_PATH)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .setPlaceholder("please upload the file to the disk directory of the alert server, and ensure that the path is absolute and has the corresponding access rights")
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .setPlaceholder("please upload the file to the disk directory of the alert server,"
+ + " and ensure that the path is absolute and has the corresponding access rights")
+ .build();
RadioParam scriptTypeParams = RadioParam.newBuilder(ScriptParamsConstants.NAME_SCRIPT_TYPE, ScriptParamsConstants.SCRIPT_TYPE)
- .addParamsOptions(new ParamsOptions(ScriptType.SHELL.getDescp(), ScriptType.SHELL.getDescp(), false))
- .setValue(ScriptType.SHELL.getDescp())
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .build();
+ .addParamsOptions(new ParamsOptions(ScriptType.SHELL.getDescp(), ScriptType.SHELL.getDescp(), false))
+ .setValue(ScriptType.SHELL.getDescp())
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .build();
return Arrays.asList(scriptUserParam, scriptPathParam, scriptTypeParams);
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java
similarity index 89%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java
index 5b096d54ad4096a38d40dcbd603e11acae283a0a..1f1da4a368275477637dd94a0ecca98047b32144 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java
@@ -17,14 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-/**
- * ScriptParamsConstants
- */
-public class ScriptParamsConstants {
-
- private ScriptParamsConstants() {
- throw new IllegalStateException("Utility class");
- }
+public final class ScriptParamsConstants {
static final String SCRIPT_TYPE = "type";
@@ -37,4 +30,8 @@ public class ScriptParamsConstants {
static final String SCRIPT_USER_PARAMS = "user.params";
static final String NAME_SCRIPT_USER_PARAMS = "userParams";
+
+ private ScriptParamsConstants() {
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
+ }
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
similarity index 71%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
index 8e33b792d8633d518bfd812caea5dcfe8b3641a0..2791fc1d96a6a6dadce278511e1c91db318121b4 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
@@ -17,31 +17,20 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
-
-import java.util.Map;
-
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * ScriptSender
- */
-public class ScriptSender {
-
- private static final Logger logger = LoggerFactory.getLogger(ScriptSender.class);
-
- private String scriptPath;
-
- private String scriptType;
- private String userParams;
+import java.io.File;
+import java.util.Map;
+public final class ScriptSender {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScriptSender.class);
private static final String ALERT_TITLE_OPTION = " -t ";
-
private static final String ALERT_CONTENT_OPTION = " -c ";
-
private static final String ALERT_USER_PARAMS_OPTION = " -p ";
+ private final String scriptPath;
+ private final String scriptType;
+ private final String userParams;
ScriptSender(Map config) {
scriptPath = config.get(ScriptParamsConstants.NAME_SCRIPT_PATH);
@@ -64,6 +53,21 @@ public class ScriptSender {
alertResult.setMessage("shell script not support windows os");
return alertResult;
}
+ //validate script path in case of injections
+ File shellScriptFile = new File(scriptPath);
+ //validate existence
+ if (!shellScriptFile.exists()) {
+ log.error("shell script not exist : {}", scriptPath);
+ alertResult.setMessage("shell script not exist : " + scriptPath);
+ return alertResult;
+ }
+ //validate is file
+ if (!shellScriptFile.isFile()) {
+ log.error("shell script is not a file : {}", scriptPath);
+ alertResult.setMessage("shell script is not a file : " + scriptPath);
+ return alertResult;
+ }
+
String[] cmd = {"/bin/sh", "-c", scriptPath + ALERT_TITLE_OPTION + "'" + title + "'" + ALERT_CONTENT_OPTION + "'" + content + "'" + ALERT_USER_PARAMS_OPTION + "'" + userParams + "'"};
int exitCode = ProcessUtils.executeScript(cmd);
@@ -73,7 +77,7 @@ public class ScriptSender {
return alertResult;
}
alertResult.setMessage("send script alert msg error,exitCode is " + exitCode);
- logger.info("send script alert msg error,exitCode is {}", exitCode);
+ log.info("send script alert msg error,exitCode is {}", exitCode);
return alertResult;
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java
similarity index 94%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java
index ff3b8096bb7e0eaadebc8d2d1ba3ad7d1fee9035..cbb3e11f28a63d3141312df68bbf45d1f977ee8b 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java
@@ -17,26 +17,17 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * ScriptType
- */
public enum ScriptType {
-
-
SHELL(0, "SHELL"),
;
+ private final int code;
+ private final String descp;
ScriptType(int code, String descp) {
this.code = code;
this.descp = descp;
}
- private final int code;
- private final String descp;
-
public int getCode() {
return code;
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java
similarity index 81%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java
index 41aabfe13dcd1a8f1ba8ee918b579c4e4cace840..6b1d19a072b455b01ff11bfe0edcf7c7d3371380 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java
@@ -23,16 +23,11 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-/**
- * StreamGobbler
- */
-public class StreamGobbler extends Thread {
-
- private static final Logger logger = LoggerFactory.getLogger(StreamGobbler.class);
+public final class StreamGobbler extends Thread {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(StreamGobbler.class);
- private InputStream inputStream;
+ private final InputStream inputStream;
StreamGobbler(InputStream inputStream) {
this.inputStream = inputStream;
@@ -51,16 +46,16 @@ public class StreamGobbler extends Thread {
output.append(System.getProperty("line.separator"));
}
if (output.length() > 0) {
- logger.info("out put msg is{}", output);
+ log.info("out put msg is{}", output);
}
} catch (IOException e) {
- logger.error("I/O error occurs {}", e.getMessage());
+ log.error("I/O error occurs {}", e.getMessage());
} finally {
try {
inputBufferReader.close();
inputStreamReader.close();
} catch (IOException e) {
- logger.error("I/O error occurs {}", e.getMessage());
+ log.error("I/O error occurs {}", e.getMessage());
}
}
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java
similarity index 93%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java
index 1d847a06358e059cc4d760bf69809610dadacbbf..7ee473e9a7b2b358498a8b36003d4f4914f7ff84 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java
@@ -26,7 +26,7 @@ public class ProcessUtilsTest {
private static final String rootPath = System.getProperty("user.dir");
- private static final String shellFilPath = rootPath + "/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh";
+ private static final String shellFilPath = rootPath + "/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/script/shell/test.sh";
private String[] cmd = {"/bin/sh", "-c", shellFilPath + " -t 1"};
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java
similarity index 91%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java
index 8cedc2c38ecc93df947a76970395f0a2dbd6a1f7..ca45cf7aed6968f035bb08600a72b6c50fbd3f8f 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java
@@ -17,7 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import java.util.List;
@@ -33,7 +33,7 @@ public class ScriptAlertChannelFactoryTest {
@Test
public void testGetParams() {
ScriptAlertChannelFactory scriptAlertChannelFactory = new ScriptAlertChannelFactory();
- List params = scriptAlertChannelFactory.getParams();
+ List params = scriptAlertChannelFactory.params();
Assert.assertEquals(3, params.size());
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
similarity index 97%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
index e022b9ebf7359b71d4905b38926c5409f612695d..445d0738b5df90d5eb2c67426df57355e59ef958 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
@@ -17,7 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.script;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.util.HashMap;
import java.util.Map;
@@ -31,11 +31,9 @@ import org.junit.Test;
*/
public class ScriptSenderTest {
- private static Map scriptConfig = new HashMap<>();
-
private static final String rootPath = System.getProperty("user.dir");
-
private static final String shellFilPath = rootPath + "/src/test/script/shell/scriptExample.sh";
+ private static Map scriptConfig = new HashMap<>();
@Before
public void initScriptConfig() {
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptExample.sh b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/script/shell/scriptExample.sh
similarity index 100%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptExample.sh
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/script/shell/scriptExample.sh
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/script/shell/test.sh
similarity index 100%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/script/shell/test.sh
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/pom.xml
new file mode 100644
index 0000000000000000000000000000000000000000..2772e8ee78af7a5abbdeebd51d610cb1982ecc67
--- /dev/null
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/pom.xml
@@ -0,0 +1,39 @@
+
+
+
+
+ dolphinscheduler-alert-plugins
+ org.apache.dolphinscheduler
+ 2.0.10-SNAPSHOT
+
+ 4.0.0
+ dolphinscheduler-alert-slack
+ jar
+
+
+
+ org.apache.httpcomponents
+ httpclient
+
+
+
+ com.google.guava
+ guava
+
+
+
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannel.java
similarity index 82%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannel.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannel.java
index 6399d8bca9107c763af459ddbd92df6ed5859394..b63fed05740fdb7adebce43f9b65243e64cd9b1c 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannel.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannel.java
@@ -17,18 +17,14 @@
package org.apache.dolphinscheduler.plugin.alert.slack;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertInfo;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertInfo;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.util.Map;
-/**
- * SlackAlertChannel
- */
-public class SlackAlertChannel implements AlertChannel {
-
+public final class SlackAlertChannel implements AlertChannel {
@Override
public AlertResult process(AlertInfo alertInfo) {
AlertData alertData = alertInfo.getAlertData();
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactory.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactory.java
similarity index 61%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactory.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactory.java
index d56976400a3886da7877a1d1beb3b8732a362cb7..9819ed4b33fb295cffc4f4e404b095aacfc4fb50 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactory.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactory.java
@@ -17,42 +17,41 @@
package org.apache.dolphinscheduler.plugin.alert.slack;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory;
-import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
+import org.apache.dolphinscheduler.spi.params.input.InputParam;
import java.util.LinkedList;
import java.util.List;
-/**
- * Slack alert factory, see {@link AlertChannelFactory}
- */
-public class SlackAlertChannelFactory implements AlertChannelFactory {
+import com.google.auto.service.AutoService;
+@AutoService(AlertChannelFactory.class)
+public final class SlackAlertChannelFactory implements AlertChannelFactory {
@Override
- public String getName() {
+ public String name() {
return "Slack";
}
@Override
- public List getParams() {
+ public List params() {
List paramsList = new LinkedList<>();
InputParam webHookParam = InputParam.newBuilder(SlackParamsConstants.SLACK_WEN_HOOK_URL_NAME, SlackParamsConstants.SLACK_WEB_HOOK_URL)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .setPlaceholder("Input WebHook Url")
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .setPlaceholder("Input WebHook Url")
+ .build();
InputParam botName = InputParam.newBuilder(SlackParamsConstants.SLACK_BOT_NAME, SlackParamsConstants.SLACK_BOT)
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .setPlaceholder("Input the bot username")
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .setPlaceholder("Input the bot username")
+ .build();
paramsList.add(webHookParam);
paramsList.add(botName);
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackParamsConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackParamsConstants.java
similarity index 77%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackParamsConstants.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackParamsConstants.java
index fd191c5d55af1fdd38752bfa086363bb3982193e..e43856ba3640d8dcaaa1a262e18c139b4a652fdf 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackParamsConstants.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackParamsConstants.java
@@ -17,18 +17,16 @@
package org.apache.dolphinscheduler.plugin.alert.slack;
-public class SlackParamsConstants {
-
- private SlackParamsConstants() {
-
- }
-
- public static final String SLACK_WEB_HOOK_URL = "WebHook";
- public static final String SLACK_WEN_HOOK_URL_NAME = "webHook";
- public static final String SLACK_BOT = "Username";
+public final class SlackParamsConstants {
+ public static final String SLACK_WEB_HOOK_URL = "webhook";
+ public static final String SLACK_WEN_HOOK_URL_NAME = "WebHook";
+ public static final String SLACK_BOT = "username";
public static final String SLACK_BOT_NAME = "username";
public static final String TEXT = "text";
public static final String ATTACHMENT = "attachments";
-
public static final Integer MAX_SHOW_NUMBER = 100;
+
+ private SlackParamsConstants() {
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
+ }
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSender.java
similarity index 88%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSender.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSender.java
index 21a82862116c0fadef10bf1825645b56378779c5..579fbaa05c0966598e365acbc49d896db3343cc2 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSender.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/main/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSender.java
@@ -39,17 +39,14 @@ import java.util.Objects;
import java.util.stream.Collectors;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
-public class SlackSender {
+public final class SlackSender {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(SlackSender.class);
- private static final Logger logger = LoggerFactory.getLogger(SlackSender.class);
-
- private String webHookUrl;
-
- private String botName;
+ private final String webHookUrl;
+ private final String botName;
public SlackSender(Map slackAlertParam) {
webHookUrl = slackAlertParam.get(SlackParamsConstants.SLACK_WEN_HOOK_URL_NAME);
@@ -86,7 +83,7 @@ public class SlackSender {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
- logger.error("Send message to slack error.", e);
+ log.error("Send message to slack error.", e);
return "System Exception";
}
}
@@ -122,17 +119,17 @@ public class SlackSender {
final int elementLen = maxLen;
StringBuilder stringBuilder = new StringBuilder(200);
stringBuilder.append(headers.stream()
- .map(header -> generateString(header, elementLen, " "))
- .collect(Collectors.joining("|")));
+ .map(header -> generateString(header, elementLen, " "))
+ .collect(Collectors.joining("|")));
stringBuilder.append("\n");
for (List element : elements) {
stringBuilder.append(element.stream()
- .map(lement -> generateString("", elementLen, "-"))
- .collect(Collectors.joining("|")));
+ .map(lement -> generateString("", elementLen, "-"))
+ .collect(Collectors.joining("|")));
stringBuilder.append("\n");
stringBuilder.append(element.stream()
- .map(e -> generateString(e, elementLen, " "))
- .collect(Collectors.joining("|")));
+ .map(e -> generateString(e, elementLen, " "))
+ .collect(Collectors.joining("|")));
stringBuilder.append("\n");
}
return String.format("```%s```", stringBuilder);
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactoryTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactoryTest.java
similarity index 87%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactoryTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactoryTest.java
index e294365da76084ab2a6169502f3c79eb317d6208..21ba907ee797e66a6bb1066030ded67965705d26 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactoryTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackAlertChannelFactoryTest.java
@@ -17,7 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.slack;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import java.util.List;
@@ -31,12 +31,12 @@ public class SlackAlertChannelFactoryTest {
@Test
public void testTestGetName() {
- Assert.assertEquals("Slack", slackAlertChannelFactory.getName());
+ Assert.assertEquals("Slack", slackAlertChannelFactory.name());
}
@Test
public void testGetParams() {
- List params = slackAlertChannelFactory.getParams();
+ List params = slackAlertChannelFactory.params();
Assert.assertEquals(2, params.size());
}
@@ -45,4 +45,4 @@ public class SlackAlertChannelFactoryTest {
AlertChannel alertChannel = slackAlertChannelFactory.create();
Assert.assertTrue(alertChannel instanceof SlackAlertChannel);
}
-}
\ No newline at end of file
+}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSenderTest.java
similarity index 95%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSenderTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSenderTest.java
index a488026b42bbdbcbea2f4e4d7e220b20d569740d..c675aca0e8beced4774b144f120416f24df1bd1a 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSenderTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-slack/src/test/java/org/apache/dolphinscheduler/plugin/alert/slack/SlackSenderTest.java
@@ -29,11 +29,11 @@ public class SlackSenderTest {
public void testSendMessage() {
Map alertparam = new HashMap<>();
alertparam.put(SlackParamsConstants.SLACK_WEN_HOOK_URL_NAME,
- "https://hooks.slack.com/services/123456");
+ "https://hooks.slack.com/services/123456");
alertparam.put(SlackParamsConstants.SLACK_BOT_NAME, "Dolphinscheduler");
SlackSender slackSender = new SlackSender(alertparam);
String response = slackSender.sendMessage("test title", "test content");
Assert.assertNotEquals("ok", response);
}
-}
\ No newline at end of file
+}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/pom.xml
similarity index 53%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/pom.xml
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/pom.xml
index 7dce7127151df89672a7733de1d08e3fa73fa820..2adfcaee1754eef7f98230521a3d7093181db556 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-slack/pom.xml
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/pom.xml
@@ -15,27 +15,17 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
-
+
- dolphinscheduler-alert-plugin
+ dolphinscheduler-alert-pluginsorg.apache.dolphinscheduler
- 2.0.0-SNAPSHOT
+ 2.0.10-SNAPSHOT4.0.0
-
- org.apache.dolphinscheduler
- dolphinscheduler-alert-slack
- dolphinscheduler-plugin
+ dolphinscheduler-alert-wechat
+ jar
-
-
- org.apache.httpcomponents
- httpclient
-
-
com.google.guavaguava
@@ -47,39 +37,14 @@
- org.slf4j
- slf4j-api
+ org.apache.httpcomponents
+ httpclientcom.fasterxml.jackson.core
- jackson-annotations
+ jackson-databindprovided
-
-
- junit
- junit
- test
-
-
-
- org.mockito
- mockito-core
- jar
- test
-
-
-
- org.jacoco
- org.jacoco.agent
- runtime
- test
-
-
-
- dolphinscheduler-alert-slack-${project.version}
-
-
-
\ No newline at end of file
+
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java
similarity index 80%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java
index 36cce09ff3e33f94a157f17529bebbba13cbedc5..94b43ea3006d729394aec98803da6d81b5e5a718 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannel.java
@@ -17,18 +17,14 @@
package org.apache.dolphinscheduler.plugin.alert.wechat;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertInfo;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertInfo;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.util.Map;
-/**
- * WeChatAlertChannel
- */
-public class WeChatAlertChannel implements AlertChannel {
-
+public final class WeChatAlertChannel implements AlertChannel {
@Override
public AlertResult process(AlertInfo info) {
AlertData alertData = info.getAlertData();
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java
similarity index 42%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java
index 25b1318f4256221da9cea87a05ea39d82244c250..ef5fc6f4cb5919bbbd7eba6e152e7dd7d01fd576 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactory.java
@@ -17,74 +17,80 @@
package org.apache.dolphinscheduler.plugin.alert.wechat;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory;
-import org.apache.dolphinscheduler.spi.alert.AlertConstants;
-import org.apache.dolphinscheduler.spi.alert.ShowType;
-import org.apache.dolphinscheduler.spi.params.input.InputParam;
-import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
+import org.apache.dolphinscheduler.alert.api.AlertConstants;
+import org.apache.dolphinscheduler.alert.api.ShowType;
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
+import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
import java.util.Arrays;
import java.util.List;
-/**
- * WeChatAlertChannelFactory
- */
-public class WeChatAlertChannelFactory implements AlertChannelFactory {
+import com.google.auto.service.AutoService;
+@AutoService(AlertChannelFactory.class)
+public final class WeChatAlertChannelFactory implements AlertChannelFactory {
@Override
- public String getName() {
+ public String name() {
return "WeChat";
}
@Override
- public List getParams() {
+ public List params() {
InputParam corpIdParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_CORP_ID, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_CORP_ID)
- .setPlaceholder("please input corp id ")
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .setPlaceholder("please input corp id ")
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam secretParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SECRET, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_SECRET)
- .setPlaceholder("please input secret ")
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .setPlaceholder("please input secret ")
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam usersParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USERS, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_USERS)
- .setPlaceholder("please input users ")
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .setPlaceholder("please input users ")
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam userSendMsgParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USER_SEND_MSG, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_USER_SEND_MSG)
- .setPlaceholder("please input corp id ")
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .setPlaceholder("please input corp id ")
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
InputParam agentIdParam = InputParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_AGENT_ID)
- .setPlaceholder("please input agent id ")
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .build())
- .build();
+ .setPlaceholder("please input agent id ")
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .build())
+ .build();
- RadioParam showType = RadioParam.newBuilder(AlertConstants.SHOW_TYPE, AlertConstants.SHOW_TYPE)
- .addParamsOptions(new ParamsOptions(ShowType.TABLE.getDescp(), ShowType.TABLE.getDescp(), false))
- .addParamsOptions(new ParamsOptions(ShowType.TEXT.getDescp(), ShowType.TEXT.getDescp(), false))
- .setValue(ShowType.TABLE.getDescp())
+ RadioParam sendType = RadioParam.newBuilder(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SEND_TYPE, WeChatAlertParamsConstants.ENTERPRISE_WE_CHAT_SEND_TYPE)
+ .addParamsOptions(new ParamsOptions(WeChatType.APP.getDescp(), WeChatType.APP.getDescp(), false))
+ .addParamsOptions(new ParamsOptions(WeChatType.APPCHAT.getDescp(), WeChatType.APPCHAT.getDescp(), false))
+ .setValue(WeChatType.APP.getDescp())
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();
- return Arrays.asList(corpIdParam, secretParam, usersParam, userSendMsgParam, agentIdParam, showType);
+ RadioParam showType = RadioParam.newBuilder(AlertConstants.NAME_SHOW_TYPE, AlertConstants.SHOW_TYPE)
+ .addParamsOptions(new ParamsOptions(ShowType.TABLE.getDescp(), ShowType.TABLE.getDescp(), false))
+ .addParamsOptions(new ParamsOptions(ShowType.TEXT.getDescp(), ShowType.TEXT.getDescp(), false))
+ .setValue(ShowType.TABLE.getDescp())
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .build();
+
+ return Arrays.asList(corpIdParam, secretParam, usersParam, userSendMsgParam, agentIdParam, sendType, showType);
}
@Override
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java
similarity index 81%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java
index 9aec21b9d934d860839eeffec0929b3f027f99c1..9c90d63e28feece8291ab3185eaeea90343471fd 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertConstants.java
@@ -17,15 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.wechat;
-/**
- * WeChatAlertConstants
- */
-public class WeChatAlertConstants {
-
- private WeChatAlertConstants() {
- throw new IllegalStateException(WeChatAlertConstants.class.getName());
- }
-
+public final class WeChatAlertConstants {
static final String MARKDOWN_QUOTE = ">";
static final String MARKDOWN_ENTER = "\n";
@@ -34,5 +26,12 @@ public class WeChatAlertConstants {
static final String WE_CHAT_PUSH_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}";
+ static final String WE_CHAT_APP_CHAT_PUSH_URL = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token" +
+ "={token}";
+
static final String WE_CHAT_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpId}&corpsecret={secret}";
+
+ private WeChatAlertConstants() {
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
+ }
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java
similarity index 85%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java
index c254b27ebfd2bcb87b92d3d1a1de82aba5c73fd0..8e8f6b1ff259bf16fca630f1140b0d0b8fb9eb14 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertParamsConstants.java
@@ -17,15 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.wechat;
-/**
- * WeChatAlertParamsConstants
- */
-public class WeChatAlertParamsConstants {
-
- private WeChatAlertParamsConstants() {
- throw new IllegalStateException(WeChatAlertParamsConstants.class.getName());
- }
-
+public final class WeChatAlertParamsConstants {
static final String ENTERPRISE_WE_CHAT_CORP_ID = "corp.id";
static final String NAME_ENTERPRISE_WE_CHAT_CORP_ID = "corpId";
@@ -56,4 +48,12 @@ public class WeChatAlertParamsConstants {
static final String NAME_ENTERPRISE_WE_CHAT_USERS = "users";
+ static final String NAME_ENTERPRISE_WE_CHAT_SEND_TYPE = "sendType";
+
+ static final String ENTERPRISE_WE_CHAT_SEND_TYPE = "send.type";
+
+
+ private WeChatAlertParamsConstants() {
+ throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
+ }
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java
similarity index 75%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java
index 6944da66bb3480f0022d7460d637d67f1959388e..e38eea598b9ee60cd86b61058ca673f0d63078e0 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSender.java
@@ -17,15 +17,11 @@
package org.apache.dolphinscheduler.plugin.alert.wechat;
-import static java.util.Objects.requireNonNull;
-
-import org.apache.dolphinscheduler.plugin.alert.wechat.exception.WeChatAlertException;
-import org.apache.dolphinscheduler.spi.alert.AlertConstants;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
-import org.apache.dolphinscheduler.spi.alert.ShowType;
+import org.apache.dolphinscheduler.alert.api.AlertConstants;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
+import org.apache.dolphinscheduler.alert.api.ShowType;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.apache.dolphinscheduler.spi.utils.StringUtils;
-
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
@@ -34,41 +30,16 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
import java.io.IOException;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.Map.Entry;
-import java.util.Set;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * WeChatSender
- */
-public class WeChatSender {
-
- private static Logger logger = LoggerFactory.getLogger(WeChatSender.class);
-
- private String weChatAgentId;
-
- private String weChatUsers;
-
- private String weChatUserSendMsg;
-
- private String weChatTokenUrlReplace;
-
- private String weChatToken;
-
- private String showType;
+import static java.util.Objects.requireNonNull;
+public final class WeChatSender {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(WeChatSender.class);
private static final String MUST_NOT_NULL = " must not null";
private static final String ALERT_STATUS = "false";
private static final String AGENT_ID_REG_EXP = "{agentId}";
@@ -77,6 +48,13 @@ public class WeChatSender {
private static final String CORP_ID_REGEX = "{corpId}";
private static final String SECRET_REGEX = "{secret}";
private static final String TOKEN_REGEX = "{token}";
+ private final String weChatAgentId;
+ private final String weChatUsers;
+ private final String weChatUserSendMsg;
+ private final String weChatTokenUrlReplace;
+ private final String weChatToken;
+ private final String sendType;
+ private final String showType;
WeChatSender(Map config) {
weChatAgentId = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_AGENT_ID);
@@ -85,58 +63,15 @@ public class WeChatSender {
String weChatSecret = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SECRET);
String weChatTokenUrl = WeChatAlertConstants.WE_CHAT_TOKEN_URL;
weChatUserSendMsg = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USER_SEND_MSG);
- showType = config.get(AlertConstants.SHOW_TYPE);
- requireNonNull(showType, AlertConstants.SHOW_TYPE + MUST_NOT_NULL);
+ sendType = config.get(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SEND_TYPE);
+ showType = config.get(AlertConstants.NAME_SHOW_TYPE);
+ requireNonNull(showType, AlertConstants.NAME_SHOW_TYPE + MUST_NOT_NULL);
weChatTokenUrlReplace = weChatTokenUrl
.replace(CORP_ID_REGEX, weChatCorpId)
.replace(SECRET_REGEX, weChatSecret);
weChatToken = getToken();
}
- /**
- * make user multi user message
- *
- * @param toUser the toUser
- * @param agentId the agentId
- * @param msg the msg
- * @return Enterprise WeChat send message
- */
- private String makeUserSendMsg(Collection toUser, String agentId, String msg) {
- String listUser = mkString(toUser);
- return weChatUserSendMsg.replace(USER_REG_EXP, listUser)
- .replace(AGENT_ID_REG_EXP, agentId)
- .replace(MSG_REG_EXP, msg);
- }
-
- /**
- * send Enterprise WeChat
- *
- * @return Enterprise WeChat resp, demo: {"errcode":0,"errmsg":"ok","invaliduser":""}
- */
- public AlertResult sendEnterpriseWeChat(String title, String content) {
- AlertResult alertResult;
- List userList = Arrays.asList(weChatUsers.split(","));
- String data = markdownByAlert(title, content);
- String msg = makeUserSendMsg(userList, weChatAgentId, data);
- if (null == weChatToken) {
- alertResult = new AlertResult();
- alertResult.setMessage("send we chat alert fail,get weChat token error");
- alertResult.setStatus(ALERT_STATUS);
- return alertResult;
- }
- String enterpriseWeChatPushUrlReplace = WeChatAlertConstants.WE_CHAT_PUSH_URL.replace(TOKEN_REGEX, weChatToken);
-
- try {
- return checkWeChatSendMsgResult(post(enterpriseWeChatPushUrlReplace, msg));
- } catch (Exception e) {
- logger.info("send we chat alert msg exception : {}", e.getMessage());
- alertResult = new AlertResult();
- alertResult.setMessage("send we chat alert fail");
- alertResult.setStatus(ALERT_STATUS);
- }
- return alertResult;
- }
-
private static String post(String url, String data) throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
@@ -150,7 +85,7 @@ public class WeChatSender {
} finally {
response.close();
}
- logger.info("Enterprise WeChat send [{}], param:{}, resp:{}",
+ log.info("Enterprise WeChat send [{}], param:{}, resp:{}",
url, data, resp);
return resp;
}
@@ -166,8 +101,8 @@ public class WeChatSender {
private static String markdownTable(String title, String content) {
List mapItemsList = JSONUtils.toList(content, LinkedHashMap.class);
if (null == mapItemsList || mapItemsList.isEmpty()) {
- logger.error("itemsList is null");
- throw new WeChatAlertException("itemsList is null");
+ log.error("itemsList is null");
+ throw new RuntimeException("itemsList is null");
}
StringBuilder contents = new StringBuilder(200);
for (LinkedHashMap mapItems : mapItemsList) {
@@ -199,7 +134,7 @@ public class WeChatSender {
if (StringUtils.isNotEmpty(content)) {
List mapItemsList = JSONUtils.toList(content, LinkedHashMap.class);
if (null == mapItemsList || mapItemsList.isEmpty()) {
- logger.error("itemsList is null");
+ log.error("itemsList is null");
throw new RuntimeException("itemsList is null");
}
@@ -220,31 +155,6 @@ public class WeChatSender {
return null;
}
- /**
- * Determine the mardown style based on the show type of the alert
- *
- * @return the markdown alert table/text
- */
- private String markdownByAlert(String title, String content) {
- String result = "";
- if (showType.equals(ShowType.TABLE.getDescp())) {
- result = markdownTable(title, content);
- } else if (showType.equals(ShowType.TEXT.getDescp())) {
- result = markdownText(title, content);
- }
- return result;
-
- }
-
- private String getToken() {
- try {
- return get(weChatTokenUrlReplace);
- } catch (IOException e) {
- logger.info("we chat alert get token error{}", e.getMessage());
- }
- return null;
- }
-
private static String get(String url) throws IOException {
String resp;
@@ -266,7 +176,6 @@ public class WeChatSender {
}
private static String mkString(Iterable list) {
-
if (null == list || StringUtils.isEmpty("|")) {
return null;
}
@@ -284,40 +193,19 @@ public class WeChatSender {
return sb.toString();
}
- public static class WeChatSendMsgResponse {
- private Integer errcode;
- private String errmsg;
-
- public Integer getErrcode() {
- return errcode;
- }
-
- public void setErrcode(Integer errcode) {
- this.errcode = errcode;
- }
-
- public String getErrmsg() {
- return errmsg;
- }
-
- public void setErrmsg(String errmsg) {
- this.errmsg = errmsg;
- }
- }
-
private static AlertResult checkWeChatSendMsgResult(String result) {
AlertResult alertResult = new AlertResult();
alertResult.setStatus(ALERT_STATUS);
if (null == result) {
alertResult.setMessage("we chat send fail");
- logger.info("send we chat msg error,resp is null");
+ log.info("send we chat msg error,resp is null");
return alertResult;
}
WeChatSendMsgResponse sendMsgResponse = JSONUtils.parseObject(result, WeChatSendMsgResponse.class);
if (null == sendMsgResponse) {
alertResult.setMessage("we chat send fail");
- logger.info("send we chat msg error,resp error");
+ log.info("send we chat msg error,resp error");
return alertResult;
}
if (sendMsgResponse.errcode == 0) {
@@ -329,4 +217,137 @@ public class WeChatSender {
alertResult.setMessage(sendMsgResponse.getErrmsg());
return alertResult;
}
+
+ /**
+ * make user multi user message
+ *
+ * @param toUser the toUser
+ * @param agentId the agentId
+ * @param msg the msg
+ * @return Enterprise WeChat send message
+ */
+ private String makeUserSendMsg(Collection toUser, String agentId, String msg) {
+ String listUser = mkString(toUser);
+ return weChatUserSendMsg.replace(USER_REG_EXP, listUser)
+ .replace(AGENT_ID_REG_EXP, agentId)
+ .replace(MSG_REG_EXP, msg);
+ }
+
+ /**
+ * send Enterprise WeChat
+ *
+ * @return Enterprise WeChat resp, demo: {"errcode":0,"errmsg":"ok","invaliduser":""}
+ */
+ public AlertResult sendEnterpriseWeChat(String title, String content) {
+ AlertResult alertResult;
+ List userList = Arrays.asList(weChatUsers.split(","));
+ String data = markdownByAlert(title, content);
+ String msg = makeUserSendMsg(userList, weChatAgentId, data);
+ if (null == weChatToken) {
+ alertResult = new AlertResult();
+ alertResult.setMessage("send we chat alert fail,get weChat token error");
+ alertResult.setStatus(ALERT_STATUS);
+ return alertResult;
+ }
+ String enterpriseWeChatPushUrlReplace = "";
+ if (sendType.equals(WeChatType.APP.getDescp())) {
+ enterpriseWeChatPushUrlReplace = WeChatAlertConstants.WE_CHAT_PUSH_URL.replace(TOKEN_REGEX, weChatToken);
+ } else if (sendType.equals(WeChatType.APPCHAT.getDescp())) {
+ enterpriseWeChatPushUrlReplace = WeChatAlertConstants.WE_CHAT_APP_CHAT_PUSH_URL.replace(TOKEN_REGEX, weChatToken);
+ }
+
+ try {
+ return checkWeChatSendMsgResult(post(enterpriseWeChatPushUrlReplace, msg));
+ } catch (Exception e) {
+ log.info("send we chat alert msg exception : {}", e.getMessage());
+ alertResult = new AlertResult();
+ alertResult.setMessage("send we chat alert fail");
+ alertResult.setStatus(ALERT_STATUS);
+ }
+ return alertResult;
+ }
+
+ /**
+ * Determine the mardown style based on the show type of the alert
+ *
+ * @return the markdown alert table/text
+ */
+ private String markdownByAlert(String title, String content) {
+ String result = "";
+ if (showType.equals(ShowType.TABLE.getDescp())) {
+ result = markdownTable(title, content);
+ } else if (showType.equals(ShowType.TEXT.getDescp())) {
+ result = markdownText(title, content);
+ }
+ return result;
+
+ }
+
+ private String getToken() {
+ try {
+ return get(weChatTokenUrlReplace);
+ } catch (IOException e) {
+ log.info("we chat alert get token error{}", e.getMessage());
+ }
+ return null;
+ }
+
+ static final class WeChatSendMsgResponse {
+ private Integer errcode;
+ private String errmsg;
+
+ public WeChatSendMsgResponse() {
+ }
+
+ public Integer getErrcode() {
+ return this.errcode;
+ }
+
+ public void setErrcode(Integer errcode) {
+ this.errcode = errcode;
+ }
+
+ public String getErrmsg() {
+ return this.errmsg;
+ }
+
+ public void setErrmsg(String errmsg) {
+ this.errmsg = errmsg;
+ }
+
+ public boolean equals(final Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof WeChatSendMsgResponse)) {
+ return false;
+ }
+ final WeChatSendMsgResponse other = (WeChatSendMsgResponse) o;
+ final Object this$errcode = this.getErrcode();
+ final Object other$errcode = other.getErrcode();
+ if (this$errcode == null ? other$errcode != null : !this$errcode.equals(other$errcode)) {
+ return false;
+ }
+ final Object this$errmsg = this.getErrmsg();
+ final Object other$errmsg = other.getErrmsg();
+ if (this$errmsg == null ? other$errmsg != null : !this$errmsg.equals(other$errmsg)) {
+ return false;
+ }
+ return true;
+ }
+
+ public int hashCode() {
+ final int PRIME = 59;
+ int result = 1;
+ final Object $errcode = this.getErrcode();
+ result = result * PRIME + ($errcode == null ? 43 : $errcode.hashCode());
+ final Object $errmsg = this.getErrmsg();
+ result = result * PRIME + ($errmsg == null ? 43 : $errmsg.hashCode());
+ return result;
+ }
+
+ public String toString() {
+ return "WeChatSender.WeChatSendMsgResponse(errcode=" + this.getErrcode() + ", errmsg=" + this.getErrmsg() + ")";
+ }
+ }
}
diff --git a/dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/alert/ShowType.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatType.java
similarity index 71%
rename from dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/alert/ShowType.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatType.java
index a95e73ff7f697d5ba841b729a48bf0eb4e8f38d6..eac208a97b9aac0d0ea9cdbe06dc83041c49805c 100644
--- a/dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/alert/ShowType.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatType.java
@@ -15,32 +15,22 @@
* limitations under the License.
*/
-package org.apache.dolphinscheduler.spi.alert;
-/**
- * show type for email
- * all alert plugin can use ShowType , so let it in spi package
- */
-public enum ShowType {
- /**
- * 0 TABLE;
- * 1 TEXT;
- * 2 attachment;
- * 3 TABLE+attachment;
- */
- TABLE(0, "table"),
- TEXT(1, "text"),
- ATTACHMENT(2, "attachment"),
- TABLEATTACHMENT(3, "table attachment");
+package org.apache.dolphinscheduler.plugin.alert.wechat;
- ShowType(int code, String descp) {
- this.code = code;
- this.descp = descp;
- }
+public enum WeChatType {
+ APP(1, "应用"),
+ APPCHAT(2, "群聊"),
+ ;
private final int code;
private final String descp;
+ WeChatType(int code, String descp) {
+ this.code = code;
+ this.descp = descp;
+ }
+
public int getCode() {
return code;
}
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java
similarity index 89%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java
index cc62d5aabf543aafcd734de1b1b3ea2ca8623785..648c206a33802eba331513a42a07a0adc9923ed2 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatAlertChannelFactoryTest.java
@@ -17,7 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.wechat;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.utils.JSONUtils;
@@ -34,9 +34,9 @@ public class WeChatAlertChannelFactoryTest {
@Test
public void testGetParams() {
WeChatAlertChannelFactory weChatAlertChannelFactory = new WeChatAlertChannelFactory();
- List params = weChatAlertChannelFactory.getParams();
+ List params = weChatAlertChannelFactory.params();
JSONUtils.toJsonString(params);
- Assert.assertEquals(6, params.size());
+ Assert.assertEquals(7, params.size());
}
@Test
diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java
similarity index 72%
rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java
index 61010ebe3ae35e3e5cb41192a656b989facb8b45..f4934bf9208b637f24f4c7bf6e7cf7cfd4650b6e 100644
--- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-wechat/src/test/java/org/apache/dolphinscheduler/plugin/alert/wechat/WeChatSenderTest.java
@@ -17,17 +17,16 @@
package org.apache.dolphinscheduler.plugin.alert.wechat;
-import org.apache.dolphinscheduler.spi.alert.AlertConstants;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
-import org.apache.dolphinscheduler.spi.alert.ShowType;
-
-import java.util.HashMap;
-import java.util.Map;
-
+import org.apache.dolphinscheduler.alert.api.AlertConstants;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
+import org.apache.dolphinscheduler.alert.api.ShowType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* WeChatSenderTest
*/
@@ -36,24 +35,24 @@ public class WeChatSenderTest {
private static Map weChatConfig = new HashMap<>();
private String content = "[{\"id\":\"69\","
- +
- "\"name\":\"UserBehavior-0--1193959466\","
- +
- "\"Job name\":\"Start workflow\","
- +
- "\"State\":\"SUCCESS\","
- +
- "\"Recovery\":\"NO\","
- +
- "\"Run time\":\"1\","
- +
- "\"Start time\": \"2018-08-06 10:31:34.0\","
- +
- "\"End time\": \"2018-08-06 10:31:49.0\","
- +
- "\"Host\": \"192.168.xx.xx\","
- +
- "\"Notify group\" :\"4\"}]";
+ +
+ "\"name\":\"UserBehavior-0--1193959466\","
+ +
+ "\"Job name\":\"Start workflow\","
+ +
+ "\"State\":\"SUCCESS\","
+ +
+ "\"Recovery\":\"NO\","
+ +
+ "\"Run time\":\"1\","
+ +
+ "\"Start time\": \"2018-08-06 10:31:34.0\","
+ +
+ "\"End time\": \"2018-08-06 10:31:49.0\","
+ +
+ "\"Host\": \"192.168.xx.xx\","
+ +
+ "\"Notify group\" :\"4\"}]";
@Before
public void initDingTalkConfig() {
@@ -62,12 +61,12 @@ public class WeChatSenderTest {
weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_CORP_ID, "NAME_ENTERPRISE_WE_CHAT_CORP_ID");
weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_SECRET, "NAME_ENTERPRISE_WE_CHAT_SECRET");
weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USER_SEND_MSG, "{\"touser\":\"{toUser}\",\"agentid\":{agentId}"
- +
- ",\"msgtype\":\"markdown\",\"markdown\":{\"content\":\"{msg}\"}}"
+ +
+ ",\"msgtype\":\"markdown\",\"markdown\":{\"content\":\"{msg}\"}}"
);
weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_USERS, "Kris");
weChatConfig.put(WeChatAlertParamsConstants.NAME_ENTERPRISE_WE_CHAT_TEAM_SEND_MSG, "msg");
- weChatConfig.put(AlertConstants.SHOW_TYPE, ShowType.TABLE.getDescp());
+ weChatConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TABLE.getDescp());
}
@Test
@@ -80,7 +79,7 @@ public class WeChatSenderTest {
@Test
public void testSendWeChatTextMsg() {
- weChatConfig.put(AlertConstants.SHOW_TYPE, ShowType.TEXT.getDescp());
+ weChatConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TEXT.getDescp());
WeChatSender weChatSender = new WeChatSender(weChatConfig);
AlertResult alertResult = weChatSender.sendEnterpriseWeChat("test", content);
Assert.assertEquals("false", alertResult.getStatus());
diff --git a/dolphinscheduler-alert-plugin/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/pom.xml
similarity index 73%
rename from dolphinscheduler-alert-plugin/pom.xml
rename to dolphinscheduler-alert/dolphinscheduler-alert-plugins/pom.xml
index f5dc77b04f059ad72d20ea8a1e3513b55fe7c6f9..e8aae9f553a07c7dc9b7e58868ee0a6e2245a05d 100644
--- a/dolphinscheduler-alert-plugin/pom.xml
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/pom.xml
@@ -15,18 +15,14 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
-
+
- dolphinscheduler
+ dolphinscheduler-alertorg.apache.dolphinscheduler
- 2.0.0-SNAPSHOT
+ 2.0.10-SNAPSHOT4.0.0
-
- org.apache.dolphinscheduler
- dolphinscheduler-alert-plugin
+ dolphinscheduler-alert-pluginspom
@@ -40,12 +36,10 @@
-
org.apache.dolphinscheduler
- dolphinscheduler-spi
- provided
+ dolphinscheduler-alert-api
-
\ No newline at end of file
+
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml
new file mode 100644
index 0000000000000000000000000000000000000000..3e0de686f185d8d645b57e34ae31ccb0a4bd8047
--- /dev/null
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml
@@ -0,0 +1,129 @@
+
+
+
+
+ 4.0.0
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert
+ 2.0.10-SNAPSHOT
+
+ dolphinscheduler-alert-server
+ ${project.artifactId}
+ jar
+
+
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-remote
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert-dingtalk
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert-email
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert-feishu
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert-http
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert-script
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert-slack
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-alert-wechat
+
+
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-dao
+
+
+
+ com.fasterxml.jackson.core
+ jackson-core
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+
+
+
+ com.google.guava
+ guava
+
+
+ jsr305
+ com.google.code.findbugs
+
+
+
+
+ ch.qos.logback
+ logback-classic
+
+
+
+
+ org.apache.poi
+ poi
+
+
+
+ org.mockito
+ mockito-core
+ test
+
+
+
+ org.mockito
+ mockito-inline
+ test
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+ *.yaml
+ *.yml
+ *.xml
+
+
+
+
+
+
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertPluginManager.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertPluginManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..1ad06e261a515bcc6a44be3760517d356283469c
--- /dev/null
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertPluginManager.java
@@ -0,0 +1,89 @@
+/*
+ * 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.dolphinscheduler.alert;
+
+import static java.lang.String.format;
+
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
+import org.apache.dolphinscheduler.common.enums.PluginType;
+import org.apache.dolphinscheduler.dao.PluginDao;
+import org.apache.dolphinscheduler.dao.entity.PluginDefine;
+import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
+import org.apache.dolphinscheduler.spi.params.base.PluginParams;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.ServiceLoader;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Component;
+
+@Component
+public final class AlertPluginManager {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(AlertPluginManager.class);
+
+ private final PluginDao pluginDao;
+
+ private final Map channelKeyedById = new HashMap<>();
+
+ public AlertPluginManager(PluginDao pluginDao) {
+ this.pluginDao = pluginDao;
+ }
+
+ @EventListener
+ public void installPlugin(ApplicationReadyEvent readyEvent) {
+ final Set names = new HashSet<>();
+
+ ServiceLoader.load(AlertChannelFactory.class).forEach(factory -> {
+ final String name = factory.name();
+
+ log.info("Registering alert plugin: {}", name);
+
+ if (!names.add(name)) {
+ throw new IllegalStateException(format("Duplicate alert plugins named '%s'", name));
+ }
+
+ final AlertChannel alertChannel = factory.create();
+
+ log.info("Registered alert plugin: {}", name);
+
+ final List params = factory.params();
+ final String paramsJson = PluginParamsTransfer.transferParamsToJson(params);
+
+ final PluginDefine pluginDefine = new PluginDefine(name, PluginType.ALERT.getDesc(), paramsJson);
+ final int id = pluginDao.addOrUpdatePluginDefine(pluginDefine);
+
+ channelKeyedById.put(id, alertChannel);
+ });
+ }
+
+ public Optional getAlertChannel(int id) {
+ return Optional.ofNullable(channelKeyedById.get(id));
+ }
+
+ public int size() {
+ return channelKeyedById.size();
+ }
+}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessor.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertRequestProcessor.java
similarity index 56%
rename from dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessor.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertRequestProcessor.java
index 9421a975465fc1e7cd3142975045994d932eb285..0db7f70af395134e82f3bac5393388f23b98a17d 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessor.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertRequestProcessor.java
@@ -15,11 +15,10 @@
* limitations under the License.
*/
-package org.apache.dolphinscheduler.alert.processor;
+package org.apache.dolphinscheduler.alert;
+
+import static com.google.common.base.Preconditions.checkArgument;
-import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
-import org.apache.dolphinscheduler.alert.runner.AlertSender;
-import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.remote.command.Command;
import org.apache.dolphinscheduler.remote.command.CommandType;
import org.apache.dolphinscheduler.remote.command.alert.AlertSendRequestCommand;
@@ -28,35 +27,33 @@ import org.apache.dolphinscheduler.remote.processor.NettyRequestProcessor;
import org.apache.dolphinscheduler.remote.utils.JsonSerializer;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Preconditions;
+import org.springframework.stereotype.Component;
import io.netty.channel.Channel;
-public class AlertRequestProcessor implements NettyRequestProcessor {
+@Component
+public final class AlertRequestProcessor implements NettyRequestProcessor {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(AlertRequestProcessor.class);
- private final Logger logger = LoggerFactory.getLogger(AlertRequestProcessor.class);
- private final AlertDao alertDao;
- private final AlertPluginManager alertPluginManager;
+ private final AlertSender alertSender;
- public AlertRequestProcessor(AlertDao alertDao, AlertPluginManager alertPluginManager) {
- this.alertDao = alertDao;
- this.alertPluginManager = alertPluginManager;
+ public AlertRequestProcessor(AlertSender alertSender) {
+ this.alertSender = alertSender;
}
@Override
public void process(Channel channel, Command command) {
- Preconditions.checkArgument(CommandType.ALERT_SEND_REQUEST == command.getType(),
- String.format("invalid command type : %s", command.getType()));
+ checkArgument(CommandType.ALERT_SEND_REQUEST == command.getType(), "invalid command type: %s", command.getType());
AlertSendRequestCommand alertSendRequestCommand = JsonSerializer.deserialize(
- command.getBody(), AlertSendRequestCommand.class);
- logger.info("received command : {}", alertSendRequestCommand);
+ command.getBody(), AlertSendRequestCommand.class);
- AlertSender alertSender = new AlertSender(alertDao, alertPluginManager);
- AlertSendResponseCommand alertSendResponseCommand = alertSender.syncHandler(alertSendRequestCommand.getGroupId(), alertSendRequestCommand.getTitle(), alertSendRequestCommand.getContent());
- channel.writeAndFlush(alertSendResponseCommand.convert2Command(command.getOpaque()));
+ log.info("Received command : {}", alertSendRequestCommand);
+ AlertSendResponseCommand alertSendResponseCommand = alertSender.syncHandler(
+ alertSendRequestCommand.getGroupId(),
+ alertSendRequestCommand.getTitle(),
+ alertSendRequestCommand.getContent());
+ channel.writeAndFlush(alertSendResponseCommand.convert2Command(command.getOpaque()));
}
}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertSender.java
similarity index 69%
rename from dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertSender.java
index d7bcc2c95fc789257c00047aca6a678db314cd79..ebc57e82bfd6ecd789ea8f2454f80223767f927b 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertSender.java
@@ -15,76 +15,63 @@
* limitations under the License.
*/
-package org.apache.dolphinscheduler.alert.runner;
+package org.apache.dolphinscheduler.alert;
-import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertInfo;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.common.enums.AlertStatus;
-import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance;
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand;
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseResult;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertData;
-import org.apache.dolphinscheduler.spi.alert.AlertInfo;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
+
+import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class AlertSender {
+import org.springframework.stereotype.Component;
- private static final Logger logger = LoggerFactory.getLogger(AlertSender.class);
+@Component
+public final class AlertSender {
+ private static final Logger log = org.slf4j.LoggerFactory.getLogger(AlertSender.class);
- private List alertList;
- private AlertDao alertDao;
+ private final AlertDao alertDao;
private final AlertPluginManager alertPluginManager;
- public AlertSender(AlertPluginManager alertPluginManager) {
- this.alertPluginManager = alertPluginManager;
- }
-
public AlertSender(AlertDao alertDao, AlertPluginManager alertPluginManager) {
- super();
this.alertDao = alertDao;
this.alertPluginManager = alertPluginManager;
}
- public AlertSender(List alertList, AlertDao alertDao, AlertPluginManager alertPluginManager) {
- super();
- this.alertList = alertList;
- this.alertDao = alertDao;
- this.alertPluginManager = alertPluginManager;
- }
-
- public void run() {
- for (Alert alert : alertList) {
+ public void send(List alerts) {
+ for (Alert alert : alerts) {
//get alert group from alert
int alertGroupId = alert.getAlertGroupId();
List alertInstanceList = alertDao.listInstanceByAlertGroupId(alertGroupId);
if (CollectionUtils.isEmpty(alertInstanceList)) {
- logger.error("send alert msg fail,no bind plugin instance.");
+ log.error("send alert msg fail,no bind plugin instance.");
alertDao.updateAlert(AlertStatus.EXECUTION_FAILURE, "no bind plugin instance", alert.getId());
continue;
}
AlertData alertData = new AlertData();
alertData.setId(alert.getId())
- .setContent(alert.getContent())
- .setLog(alert.getLog())
- .setTitle(alert.getTitle());
+ .setContent(alert.getContent())
+ .setLog(alert.getLog())
+ .setTitle(alert.getTitle());
for (AlertPluginInstance instance : alertInstanceList) {
AlertResult alertResult = this.alertResultHandler(instance, alertData);
AlertStatus alertStatus = Boolean.parseBoolean(String.valueOf(alertResult.getStatus())) ? AlertStatus.EXECUTION_SUCCESS : AlertStatus.EXECUTION_FAILURE;
alertDao.updateAlert(alertStatus, alertResult.getMessage(), alert.getId());
-
}
}
@@ -99,30 +86,28 @@ public class AlertSender {
* @return AlertSendResponseCommand
*/
public AlertSendResponseCommand syncHandler(int alertGroupId, String title, String content) {
-
List alertInstanceList = alertDao.listInstanceByAlertGroupId(alertGroupId);
AlertData alertData = new AlertData();
alertData.setContent(content)
- .setTitle(title);
+ .setTitle(title);
boolean sendResponseStatus = true;
List sendResponseResults = new ArrayList<>();
if (CollectionUtils.isEmpty(alertInstanceList)) {
- sendResponseStatus = false;
AlertSendResponseResult alertSendResponseResult = new AlertSendResponseResult();
String message = String.format("Alert GroupId %s send error : not found alert instance", alertGroupId);
- alertSendResponseResult.setStatus(sendResponseStatus);
+ alertSendResponseResult.setStatus(false);
alertSendResponseResult.setMessage(message);
sendResponseResults.add(alertSendResponseResult);
- logger.error("Alert GroupId {} send error : not found alert instance", alertGroupId);
- return new AlertSendResponseCommand(sendResponseStatus, sendResponseResults);
+ log.error("Alert GroupId {} send error : not found alert instance", alertGroupId);
+ return new AlertSendResponseCommand(false, sendResponseResults);
}
for (AlertPluginInstance instance : alertInstanceList) {
AlertResult alertResult = this.alertResultHandler(instance, alertData);
AlertSendResponseResult alertSendResponseResult = new AlertSendResponseResult(
- Boolean.parseBoolean(String.valueOf(alertResult.getStatus())), alertResult.getMessage());
+ Boolean.parseBoolean(String.valueOf(alertResult.getStatus())), alertResult.getMessage());
sendResponseStatus = sendResponseStatus && alertSendResponseResult.getStatus();
sendResponseResults.add(alertSendResponseResult);
}
@@ -138,15 +123,14 @@ public class AlertSender {
* @return AlertResult
*/
private AlertResult alertResultHandler(AlertPluginInstance instance, AlertData alertData) {
- String pluginName = alertPluginManager.getPluginNameById(instance.getPluginDefineId());
- AlertChannel alertChannel = alertPluginManager.getAlertChannelMap().get(pluginName);
+ Optional alertChannel = alertPluginManager.getAlertChannel(instance.getPluginDefineId());
AlertResult alertResultExtend = new AlertResult();
String pluginInstanceName = instance.getInstanceName();
- if (alertChannel == null) {
+ if (!alertChannel.isPresent()) {
String message = String.format("Alert Plugin %s send error : return value is null", pluginInstanceName);
alertResultExtend.setStatus(String.valueOf(false));
alertResultExtend.setMessage(message);
- logger.error("Alert Plugin {} send error : not found plugin {}", pluginInstanceName, pluginName);
+ log.error("Alert Plugin {} send error : not found plugin {}", pluginInstanceName, instance.getPluginDefineId());
return alertResultExtend;
}
@@ -156,29 +140,27 @@ public class AlertSender {
alertInfo.setAlertParams(paramsMap);
AlertResult alertResult;
try {
- alertResult = alertChannel.process(alertInfo);
+ alertResult = alertChannel.get().process(alertInfo);
} catch (Exception e) {
alertResult = new AlertResult("false", e.getMessage());
- logger.error("send alert error alert data id :{},", alertData.getId(), e);
+ log.error("send alert error alert data id :{},", alertData.getId(), e);
}
-
if (alertResult == null) {
String message = String.format("Alert Plugin %s send error : return alertResult value is null", pluginInstanceName);
alertResultExtend.setStatus(String.valueOf(false));
alertResultExtend.setMessage(message);
- logger.info("Alert Plugin {} send error : return alertResult value is null", pluginInstanceName);
+ log.info("Alert Plugin {} send error : return alertResult value is null", pluginInstanceName);
} else if (!Boolean.parseBoolean(String.valueOf(alertResult.getStatus()))) {
alertResultExtend.setStatus(String.valueOf(false));
alertResultExtend.setMessage(alertResult.getMessage());
- logger.info("Alert Plugin {} send error : {}", pluginInstanceName, alertResult.getMessage());
+ log.info("Alert Plugin {} send error : {}", pluginInstanceName, alertResult.getMessage());
} else {
String message = String.format("Alert Plugin %s send success", pluginInstanceName);
alertResultExtend.setStatus(String.valueOf(true));
alertResultExtend.setMessage(message);
- logger.info("Alert Plugin {} send success", pluginInstanceName);
+ log.info("Alert Plugin {} send success", pluginInstanceName);
}
return alertResultExtend;
}
-
}
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
similarity index 31%
rename from dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
index b0a8c0348db1c91a91d60fe0e51ac34054565a12..f1aa562d06beddd294fdb8dc44577df205ee9f52 100644
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertServer.java
@@ -17,140 +17,108 @@
package org.apache.dolphinscheduler.alert;
-import static org.apache.dolphinscheduler.alert.utils.Constants.ALERT_PROPERTIES_PATH;
import static org.apache.dolphinscheduler.common.Constants.ALERT_RPC_PORT;
-import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
-import org.apache.dolphinscheduler.alert.processor.AlertRequestProcessor;
-import org.apache.dolphinscheduler.alert.runner.AlertSender;
-import org.apache.dolphinscheduler.alert.utils.Constants;
import org.apache.dolphinscheduler.common.thread.Stopper;
-import org.apache.dolphinscheduler.common.utils.PropertyUtils;
import org.apache.dolphinscheduler.dao.AlertDao;
-import org.apache.dolphinscheduler.dao.DaoFactory;
import org.apache.dolphinscheduler.dao.PluginDao;
import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.remote.NettyRemotingServer;
import org.apache.dolphinscheduler.remote.command.CommandType;
import org.apache.dolphinscheduler.remote.config.NettyServerConfig;
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginLoader;
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginManagerConfig;
-import org.apache.dolphinscheduler.spi.utils.StringUtils;
+import java.io.Closeable;
import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.ImmutableList;
-
-public class AlertServer {
-
- private static final Logger logger = LoggerFactory.getLogger(AlertServer.class);
-
- private final PluginDao pluginDao = DaoFactory.getDaoInstance(PluginDao.class);
-
- private final AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
-
- private AlertPluginManager alertPluginManager;
-
- public static final String ALERT_PLUGIN_BINDING = "alert.plugin.binding";
-
- public static final String ALERT_PLUGIN_DIR = "alert.plugin.dir";
-
- public static final String MAVEN_LOCAL_REPOSITORY = "maven.local.repository";
+import org.springframework.boot.WebApplicationType;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.event.EventListener;
+
+@EnableAutoConfiguration
+@ComponentScan(value = {
+ "org.apache.dolphinscheduler.alert",
+ "org.apache.dolphinscheduler.dao"
+})
+public class AlertServer implements Closeable {
+ private static final Logger log = LoggerFactory.getLogger(AlertServer.class);
+
+ private final PluginDao pluginDao;
+ private final AlertDao alertDao;
+ private final AlertPluginManager alertPluginManager;
+ private final AlertSender alertSender;
+ private final AlertRequestProcessor alertRequestProcessor;
private NettyRemotingServer server;
- private static class AlertServerHolder {
- private static final AlertServer INSTANCE = new AlertServer();
+ public AlertServer(PluginDao pluginDao, AlertDao alertDao, AlertPluginManager alertPluginManager, AlertSender alertSender, AlertRequestProcessor alertRequestProcessor) {
+ this.pluginDao = pluginDao;
+ this.alertDao = alertDao;
+ this.alertPluginManager = alertPluginManager;
+ this.alertSender = alertSender;
+ this.alertRequestProcessor = alertRequestProcessor;
}
- public static AlertServer getInstance() {
- return AlertServerHolder.INSTANCE;
+ public static void main(String[] args) {
+ new SpringApplicationBuilder(AlertServer.class)
+ .web(WebApplicationType.NONE)
+ .run(args);
}
- private AlertServer() {
+ @EventListener
+ public void start(ApplicationReadyEvent readyEvent) {
+ log.info("Starting Alert server");
+
+ checkTable();
+ startServer();
+
+ Executors.newScheduledThreadPool(1)
+ .scheduleAtFixedRate(new Sender(), 5, 5, TimeUnit.SECONDS);
+ }
+ @Override
+ @PreDestroy
+ public void close() {
+ server.close();
}
private void checkTable() {
if (!pluginDao.checkPluginDefineTableExist()) {
- logger.error("Plugin Define Table t_ds_plugin_define Not Exist . Please Create it First !");
+ log.error("Plugin Define Table t_ds_plugin_define Not Exist . Please Create it First !");
System.exit(1);
}
}
- private void initPlugin() {
- DolphinPluginManagerConfig alertPluginManagerConfig = new DolphinPluginManagerConfig();
- alertPluginManagerConfig.setPlugins(PropertyUtils.getString(ALERT_PLUGIN_BINDING));
- if (StringUtils.isNotBlank(PropertyUtils.getString(ALERT_PLUGIN_DIR))) {
- alertPluginManagerConfig.setInstalledPluginsDir(PropertyUtils.getString(ALERT_PLUGIN_DIR, Constants.ALERT_PLUGIN_PATH).trim());
- }
-
- if (StringUtils.isNotBlank(PropertyUtils.getString(MAVEN_LOCAL_REPOSITORY))) {
- alertPluginManagerConfig.setMavenLocalRepository(PropertyUtils.getString(MAVEN_LOCAL_REPOSITORY).trim());
- }
-
- alertPluginManager = new AlertPluginManager();
- DolphinPluginLoader alertPluginLoader = new DolphinPluginLoader(alertPluginManagerConfig, ImmutableList.of(alertPluginManager));
- try {
- alertPluginLoader.loadPlugins();
- } catch (Exception e) {
- throw new RuntimeException("Load Alert Plugin Failed !", e);
- }
- }
-
- private void initRemoteServer() {
+ private void startServer() {
NettyServerConfig serverConfig = new NettyServerConfig();
serverConfig.setListenPort(ALERT_RPC_PORT);
- this.server = new NettyRemotingServer(serverConfig);
- this.server.registerProcessor(CommandType.ALERT_SEND_REQUEST, new AlertRequestProcessor(alertDao, alertPluginManager));
- this.server.start();
- }
-
- private void runSender() {
- new Thread(new Sender()).start();
- }
- public void start() {
- PropertyUtils.loadPropertyFile(ALERT_PROPERTIES_PATH);
- checkTable();
- initPlugin();
- initRemoteServer();
- logger.info("alert server ready start ");
- runSender();
- }
-
- public void stop() {
- this.server.close();
- logger.info("alert server shut down");
+ server = new NettyRemotingServer(serverConfig);
+ server.registerProcessor(CommandType.ALERT_SEND_REQUEST, alertRequestProcessor);
+ server.start();
}
final class Sender implements Runnable {
@Override
public void run() {
- while (Stopper.isRunning()) {
- try {
- Thread.sleep(Constants.ALERT_SCAN_INTERVAL);
- } catch (InterruptedException e) {
- logger.error(e.getMessage(), e);
- Thread.currentThread().interrupt();
- }
- if (alertPluginManager == null || alertPluginManager.getAlertChannelMap().size() == 0) {
- logger.warn("No Alert Plugin . Cannot send alert info. ");
- } else {
- List alerts = alertDao.listWaitExecutionAlert();
- new AlertSender(alerts, alertDao, alertPluginManager).run();
- }
+ if (!Stopper.isRunning()) {
+ return;
}
- }
- }
- public static void main(String[] args) {
- AlertServer alertServer = AlertServer.getInstance();
- alertServer.start();
- Runtime.getRuntime().addShutdownHook(new Thread(alertServer::stop));
+ try {
+ final List alerts = alertDao.listPendingAlerts();
+ alertSender.send(alerts);
+ } catch (Exception e) {
+ log.error("Failed to send alert", e);
+ }
+ }
}
-
}
diff --git a/dolphinscheduler-alert/src/main/resources/logback-alert.xml b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/resources/logback-alert.xml
similarity index 95%
rename from dolphinscheduler-alert/src/main/resources/logback-alert.xml
rename to dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/resources/logback-alert.xml
index 1718947dd13a6eaaf650d8d837629026d65398fd..48b3c35bffcc3eee91dedbe6030dfb5925c27ba1 100644
--- a/dolphinscheduler-alert/src/main/resources/logback-alert.xml
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/resources/logback-alert.xml
@@ -17,7 +17,7 @@
-->
-
+
@@ -49,4 +49,4 @@
-
\ No newline at end of file
+
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java
similarity index 57%
rename from dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java
index 5c31225edca95e72324da8ddfdc7902e88bacd24..b1f8e54240587a2c9656c44cae249284f9147e75 100644
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/plugin/EmailAlertPluginTest.java
@@ -17,54 +17,62 @@
package org.apache.dolphinscheduler.alert.plugin;
+import org.apache.dolphinscheduler.alert.AlertPluginManager;
+import org.apache.dolphinscheduler.alert.AlertSender;
import org.apache.dolphinscheduler.alert.AlertServer;
-import org.apache.dolphinscheduler.alert.runner.AlertSender;
-import org.apache.dolphinscheduler.alert.utils.Constants;
+import org.apache.dolphinscheduler.alert.api.AlertConstants;
+import org.apache.dolphinscheduler.alert.api.ShowType;
import org.apache.dolphinscheduler.common.enums.AlertStatus;
+import org.apache.dolphinscheduler.common.enums.ProfileType;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
-import org.apache.dolphinscheduler.common.utils.PropertyUtils;
import org.apache.dolphinscheduler.dao.AlertDao;
-import org.apache.dolphinscheduler.dao.DaoFactory;
import org.apache.dolphinscheduler.dao.PluginDao;
import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.AlertGroup;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance;
import org.apache.dolphinscheduler.dao.entity.PluginDefine;
-import org.apache.dolphinscheduler.spi.alert.AlertConstants;
-import org.apache.dolphinscheduler.spi.alert.ShowType;
-import org.apache.dolphinscheduler.spi.params.input.InputParam;
import org.apache.dolphinscheduler.spi.params.PasswordParam;
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
-import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
import org.apache.dolphinscheduler.spi.params.base.DataType;
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginLoader;
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginManagerConfig;
-import org.apache.dolphinscheduler.spi.utils.StringUtils;
+import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
-import org.junit.Assert;
-import org.junit.Test;
-
-import com.google.common.collect.ImmutableList;
-
-/**
- * test load and use alert plugin
- */
+@ActiveProfiles(ProfileType.H2)
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = AlertServer.class)
public class EmailAlertPluginTest {
-
- private AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
- private PluginDao pluginDao = DaoFactory.getDaoInstance(PluginDao.class);
+ @Autowired
+ private AlertDao alertDao;
+ @Autowired
+ private PluginDao pluginDao;
+ @Autowired
+ private AlertPluginManager manager;
+ @Autowired
+ private AlertSender alertSender;
+
+ @BeforeClass
+ public static void setUpClass() {
+ System.setProperty("spring.profiles.active", "h2");
+ }
@Test
public void testRunSend() {
-
//create alert group
AlertGroup alertGroup = new AlertGroup();
alertGroup.setDescription("test alert group 1");
@@ -78,7 +86,7 @@ public class EmailAlertPluginTest {
map1.put("mysql service name", "mysql200");
map1.put("mysql address", "192.168.xx.xx");
map1.put("port", "3306");
- map1.put(AlertConstants.SHOW_TYPE, ShowType.TEXT.getDescp());
+ map1.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TEXT.getDescp());
map1.put("no index of number", "80");
map1.put("database client connections", "190");
@@ -87,7 +95,7 @@ public class EmailAlertPluginTest {
map2.put("mysql address", "192.168.xx.xx");
map2.put("port", "3306");
map2.put("no index of number", "10");
- map1.put(AlertConstants.SHOW_TYPE, ShowType.TABLE.getDescp());
+ map1.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TABLE.getDescp());
map2.put("database client connections", "90");
List> maps = new ArrayList<>();
@@ -102,42 +110,20 @@ public class EmailAlertPluginTest {
List alertList = new ArrayList<>();
alertList.add(alert1);
- //load email alert plugin
- AlertPluginManager alertPluginManager = new AlertPluginManager();
- DolphinPluginManagerConfig alertPluginManagerConfig = new DolphinPluginManagerConfig();
- String path = DolphinPluginLoader.class.getClassLoader().getResource("").getPath();
- alertPluginManagerConfig.setPlugins(path + "../../../dolphinscheduler-alert-plugin/dolphinscheduler-alert-email/pom.xml");
- if (StringUtils.isNotBlank(PropertyUtils.getString(AlertServer.ALERT_PLUGIN_DIR))) {
- alertPluginManagerConfig.setInstalledPluginsDir(PropertyUtils.getString(AlertServer.ALERT_PLUGIN_DIR, Constants.ALERT_PLUGIN_PATH).trim());
- }
-
- if (StringUtils.isNotBlank(PropertyUtils.getString(AlertServer.MAVEN_LOCAL_REPOSITORY))) {
- alertPluginManagerConfig.setMavenLocalRepository(PropertyUtils.getString(AlertServer.MAVEN_LOCAL_REPOSITORY).trim());
- }
-
- DolphinPluginLoader alertPluginLoader = new DolphinPluginLoader(alertPluginManagerConfig, ImmutableList.of(alertPluginManager));
- try {
- alertPluginLoader.loadPlugins();
- } catch (Exception e) {
- throw new RuntimeException("load Alert Plugin Failed !", e);
- }
-
//create email alert plugin instance
AlertPluginInstance alertPluginInstance = new AlertPluginInstance();
alertPluginInstance.setCreateTime(new Date());
alertPluginInstance.setInstanceName("test email alert");
- List pluginDefineList = pluginDao.getPluginDefineMapper().queryByNameAndType("Email", "alert");
- if (pluginDefineList == null || pluginDefineList.size() == 0) {
+ PluginDefine pluginDefine = pluginDao.getPluginDefineMapper().queryByNameAndType("Email", "alert");
+ if (pluginDefine == null) {
throw new RuntimeException("no alert plugin be load");
}
- PluginDefine pluginDefine = pluginDefineList.get(0);
alertPluginInstance.setPluginDefineId(pluginDefine.getId());
alertPluginInstance.setPluginInstanceParams(getEmailAlertParams());
alertDao.getAlertPluginInstanceMapper().insert(alertPluginInstance);
- AlertSender alertSender = new AlertSender(alertList, alertDao, alertPluginManager);
- alertSender.run();
+ alertSender.send(alertList);
Alert alertResult = alertDao.getAlertMapper().selectById(alert1.getId());
Assert.assertNotNull(alertResult);
@@ -153,74 +139,74 @@ public class EmailAlertPluginTest {
List paramsList = new ArrayList<>();
InputParam receivesParam = InputParam.newBuilder("receivers", "receivers")
- .setValue("540957506@qq.com")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .build();
+ .setValue("540957506@qq.com")
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .build();
InputParam mailSmtpHost = InputParam.newBuilder("mailServerHost", "mail.smtp.host")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue("smtp.exmail.qq.com")
- .build();
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue("smtp.exmail.qq.com")
+ .build();
InputParam mailSmtpPort = InputParam.newBuilder("mailServerPort", "mail.smtp.port")
- .addValidate(Validate.newBuilder()
- .setRequired(true)
- .setType(DataType.NUMBER.getDataType())
- .build())
- .setValue(25)
- .build();
+ .addValidate(Validate.newBuilder()
+ .setRequired(true)
+ .setType(DataType.NUMBER.getDataType())
+ .build())
+ .setValue(25)
+ .build();
InputParam mailSender = InputParam.newBuilder("mailSender", "mail.sender")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue("easyscheduler@analysys.com.cn")
- .build();
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue("easyscheduler@analysys.com.cn")
+ .build();
RadioParam enableSmtpAuth = RadioParam.newBuilder("enableSmtpAuth", "mail.smtp.auth")
- .addParamsOptions(new ParamsOptions("YES", true, false))
- .addParamsOptions(new ParamsOptions("NO", false, false))
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue(true)
- .build();
+ .addParamsOptions(new ParamsOptions("YES", true, false))
+ .addParamsOptions(new ParamsOptions("NO", false, false))
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue(true)
+ .build();
InputParam mailUser = InputParam.newBuilder("mailUser", "mail.user")
- .setPlaceholder("if enable use authentication, you need input user")
- .setValue("easyscheduler@analysys.com.cn")
- .build();
+ .setPlaceholder("if enable use authentication, you need input user")
+ .setValue("easyscheduler@analysys.com.cn")
+ .build();
PasswordParam mailPassword = PasswordParam.newBuilder("mailPasswd", "mail.passwd")
- .setPlaceholder("if enable use authentication, you need input password")
- .setValue("xxxxxxx")
- .build();
+ .setPlaceholder("if enable use authentication, you need input password")
+ .setValue("xxxxxxx")
+ .build();
RadioParam enableTls = RadioParam.newBuilder("starttlsEnable", "mail.smtp.starttls.enable")
- .addParamsOptions(new ParamsOptions("YES", true, false))
- .addParamsOptions(new ParamsOptions("NO", false, false))
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue(true)
- .build();
+ .addParamsOptions(new ParamsOptions("YES", true, false))
+ .addParamsOptions(new ParamsOptions("NO", false, false))
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue(true)
+ .build();
RadioParam enableSsl = RadioParam.newBuilder("sslEnable", "mail.smtp.ssl.enable")
- .addParamsOptions(new ParamsOptions("YES", true, false))
- .addParamsOptions(new ParamsOptions("NO", false, false))
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue(false)
- .build();
+ .addParamsOptions(new ParamsOptions("YES", true, false))
+ .addParamsOptions(new ParamsOptions("NO", false, false))
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue(false)
+ .build();
InputParam sslTrust = InputParam.newBuilder("mailSmtpSslTrust", "mail.smtp.ssl.trust")
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .setValue("smtp.exmail.qq.com")
- .build();
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .setValue("smtp.exmail.qq.com")
+ .build();
List emailShowTypeList = new ArrayList<>();
emailShowTypeList.add(new ParamsOptions(ShowType.TABLE.getDescp(), ShowType.TABLE.getDescp(), false));
emailShowTypeList.add(new ParamsOptions(ShowType.TEXT.getDescp(), ShowType.TEXT.getDescp(), false));
emailShowTypeList.add(new ParamsOptions(ShowType.ATTACHMENT.getDescp(), ShowType.ATTACHMENT.getDescp(), false));
emailShowTypeList.add(new ParamsOptions(ShowType.TABLEATTACHMENT.getDescp(), ShowType.TABLEATTACHMENT.getDescp(), false));
- RadioParam showType = RadioParam.newBuilder(AlertConstants.SHOW_TYPE, "showType")
- .setOptions(emailShowTypeList)
- .setValue(ShowType.TABLE.getDescp())
- .addValidate(Validate.newBuilder().setRequired(true).build())
- .build();
+ RadioParam showType = RadioParam.newBuilder(AlertConstants.NAME_SHOW_TYPE, "showType")
+ .setOptions(emailShowTypeList)
+ .setValue(ShowType.TABLE.getDescp())
+ .addValidate(Validate.newBuilder().setRequired(true).build())
+ .build();
paramsList.add(receivesParam);
paramsList.add(mailSmtpHost);
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java
similarity index 77%
rename from dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java
index 052d2f3d55fcfd931b84ed8100d35727580d0087..20e67687a4568baeffc902a6ceac8d39ee565533 100644
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/processor/AlertRequestProcessorTest.java
@@ -17,7 +17,10 @@
package org.apache.dolphinscheduler.alert.processor;
-import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
+import static org.mockito.Mockito.mock;
+
+import org.apache.dolphinscheduler.alert.AlertRequestProcessor;
+import org.apache.dolphinscheduler.alert.AlertSender;
import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.remote.command.Command;
import org.apache.dolphinscheduler.remote.command.CommandType;
@@ -26,30 +29,21 @@ import org.apache.dolphinscheduler.remote.command.alert.AlertSendRequestCommand;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-import org.powermock.api.mockito.PowerMockito;
import io.netty.channel.Channel;
-/**
- * alert request processor test
- */
public class AlertRequestProcessorTest {
-
- private AlertDao alertDao;
- private AlertPluginManager alertPluginManager;
-
private AlertRequestProcessor alertRequestProcessor;
@Before
public void before() {
- alertDao = PowerMockito.mock(AlertDao.class);
- alertPluginManager = PowerMockito.mock(AlertPluginManager.class);
- alertRequestProcessor = new AlertRequestProcessor(alertDao, alertPluginManager);
+ final AlertDao alertDao = mock(AlertDao.class);
+ alertRequestProcessor = new AlertRequestProcessor(new AlertSender(alertDao, null));
}
@Test
public void testProcess() {
- Channel channel = PowerMockito.mock(Channel.class);
+ Channel channel = mock(Channel.class);
AlertSendRequestCommand alertSendRequestCommand = new AlertSendRequestCommand(1, "title", "content");
Command reqCommand = alertSendRequestCommand.convert2Command();
Assert.assertEquals(CommandType.ALERT_SEND_REQUEST, reqCommand.getType());
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java
similarity index 60%
rename from dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java
rename to dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java
index 3b84bdbe671f138f10ad8ada65d7ffa541c781c2..160afeb5d9c016154e9d88059391ba3e425df480 100644
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java
@@ -17,34 +17,32 @@
package org.apache.dolphinscheduler.alert.runner;
-import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.dolphinscheduler.alert.AlertPluginManager;
+import org.apache.dolphinscheduler.alert.AlertSender;
+import org.apache.dolphinscheduler.alert.api.AlertChannel;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.dao.PluginDao;
import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance;
import org.apache.dolphinscheduler.dao.entity.PluginDefine;
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertResult;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.Optional;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
-import org.powermock.api.mockito.PowerMockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/**
- * alert sender test
- */
public class AlertSenderTest {
-
private static final Logger logger = LoggerFactory.getLogger(AlertSenderTest.class);
private AlertDao alertDao;
@@ -55,10 +53,9 @@ public class AlertSenderTest {
@Before
public void before() {
- alertDao = PowerMockito.mock(AlertDao.class);
- pluginDao = PowerMockito.mock(PluginDao.class);
- alertPluginManager = PowerMockito.mock(AlertPluginManager.class);
-
+ alertDao = mock(AlertDao.class);
+ pluginDao = mock(PluginDao.class);
+ alertPluginManager = mock(AlertPluginManager.class);
}
@Test
@@ -70,12 +67,12 @@ public class AlertSenderTest {
alertSender = new AlertSender(alertDao, alertPluginManager);
//1.alert instance does not exist
- PowerMockito.when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(null);
+ when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(null);
AlertSendResponseCommand alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content);
Assert.assertFalse(alertSendResponseCommand.getResStatus());
alertSendResponseCommand.getResResults().forEach(result ->
- logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
+ logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
//2.alert plugin does not exist
int pluginDefineId = 1;
@@ -83,59 +80,52 @@ public class AlertSenderTest {
String pluginInstanceName = "alert-instance-mail";
List alertInstanceList = new ArrayList<>();
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(
- pluginDefineId, pluginInstanceParams, pluginInstanceName);
+ pluginDefineId, pluginInstanceParams, pluginInstanceName);
alertInstanceList.add(alertPluginInstance);
- PowerMockito.when(alertDao.listInstanceByAlertGroupId(1)).thenReturn(alertInstanceList);
+ when(alertDao.listInstanceByAlertGroupId(1)).thenReturn(alertInstanceList);
String pluginName = "alert-plugin-mail";
PluginDefine pluginDefine = new PluginDefine(pluginName, "1", null);
- PowerMockito.when(pluginDao.getPluginDefineById(pluginDefineId)).thenReturn(pluginDefine);
+ when(pluginDao.getPluginDefineById(pluginDefineId)).thenReturn(pluginDefine);
alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content);
Assert.assertFalse(alertSendResponseCommand.getResStatus());
alertSendResponseCommand.getResResults().forEach(result ->
- logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
+ logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
//3.alert result value is null
- AlertChannel alertChannelMock = PowerMockito.mock(AlertChannel.class);
- PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(null);
- Map alertChannelMap = new ConcurrentHashMap<>();
- alertChannelMap.put(pluginName, alertChannelMock);
- PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
- PowerMockito.when(alertPluginManager.getPluginNameById(Mockito.anyInt())).thenReturn("alert-plugin-mail");
+ AlertChannel alertChannelMock = mock(AlertChannel.class);
+ when(alertChannelMock.process(Mockito.any())).thenReturn(null);
+ when(alertPluginManager.getAlertChannel(1)).thenReturn(Optional.of(alertChannelMock));
alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content);
Assert.assertFalse(alertSendResponseCommand.getResStatus());
alertSendResponseCommand.getResResults().forEach(result ->
- logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
+ logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
//4.abnormal information inside the alert plug-in code
AlertResult alertResult = new AlertResult();
alertResult.setStatus(String.valueOf(false));
alertResult.setMessage("Abnormal information inside the alert plug-in code");
- PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult);
- alertChannelMap = new ConcurrentHashMap<>();
- alertChannelMap.put(pluginName, alertChannelMock);
- PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
+ when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult);
+ when(alertPluginManager.getAlertChannel(1)).thenReturn(Optional.of(alertChannelMock));
alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content);
Assert.assertFalse(alertSendResponseCommand.getResStatus());
alertSendResponseCommand.getResResults().forEach(result ->
- logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
+ logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
//5.alert plugin send success
alertResult = new AlertResult();
alertResult.setStatus(String.valueOf(true));
alertResult.setMessage(String.format("Alert Plugin %s send success", pluginInstanceName));
- PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult);
- alertChannelMap = new ConcurrentHashMap<>();
- alertChannelMap.put(pluginName, alertChannelMock);
- PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
+ when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult);
+ when(alertPluginManager.getAlertChannel(1)).thenReturn(Optional.of(alertChannelMock));
alertSendResponseCommand = alertSender.syncHandler(alertGroupId, title, content);
Assert.assertTrue(alertSendResponseCommand.getResStatus());
alertSendResponseCommand.getResResults().forEach(result ->
- logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
+ logger.info("alert send response result, status:{}, message:{}", result.getStatus(), result.getMessage()));
}
@@ -151,33 +141,28 @@ public class AlertSenderTest {
alert.setContent(content);
alertList.add(alert);
- alertSender = new AlertSender(alertList, alertDao, alertPluginManager);
+ alertSender = new AlertSender(alertDao, alertPluginManager);
int pluginDefineId = 1;
String pluginInstanceParams = "alert-instance-mail-params";
String pluginInstanceName = "alert-instance-mail";
List alertInstanceList = new ArrayList<>();
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(
- pluginDefineId, pluginInstanceParams, pluginInstanceName);
+ pluginDefineId, pluginInstanceParams, pluginInstanceName);
alertInstanceList.add(alertPluginInstance);
- PowerMockito.when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(alertInstanceList);
+ when(alertDao.listInstanceByAlertGroupId(alertGroupId)).thenReturn(alertInstanceList);
String pluginName = "alert-plugin-mail";
PluginDefine pluginDefine = new PluginDefine(pluginName, "1", null);
- PowerMockito.when(pluginDao.getPluginDefineById(pluginDefineId)).thenReturn(pluginDefine);
- PowerMockito.when(alertPluginManager.getPluginNameById(1)).thenReturn("alert-instance-mail");
+ when(pluginDao.getPluginDefineById(pluginDefineId)).thenReturn(pluginDefine);
AlertResult alertResult = new AlertResult();
alertResult.setStatus(String.valueOf(true));
alertResult.setMessage(String.format("Alert Plugin %s send success", pluginInstanceName));
- AlertChannel alertChannelMock = PowerMockito.mock(AlertChannel.class);
- PowerMockito.when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult);
- ConcurrentHashMap alertChannelMap = new ConcurrentHashMap<>();
- alertChannelMap.put(pluginName, alertChannelMock);
- PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
+ AlertChannel alertChannelMock = mock(AlertChannel.class);
+ when(alertChannelMock.process(Mockito.any())).thenReturn(alertResult);
+ when(alertPluginManager.getAlertChannel(1)).thenReturn(Optional.of(alertChannelMock));
Assert.assertTrue(Boolean.parseBoolean(alertResult.getStatus()));
- alertSender.run();
-
+ alertSender.send(alertList);
}
-
}
diff --git a/dolphinscheduler-alert/pom.xml b/dolphinscheduler-alert/pom.xml
index 456c076e8f6a52a8c9e2b909f50bb23f578de5fe..80d46f4906f0cd588480de78f70ea6d7923e989d 100644
--- a/dolphinscheduler-alert/pom.xml
+++ b/dolphinscheduler-alert/pom.xml
@@ -1,125 +1,48 @@
- 4.0.0
- org.apache.dolphinschedulerdolphinscheduler
- 2.0.0-SNAPSHOT
+ org.apache.dolphinscheduler
+ 2.0.10-SNAPSHOT
+ 4.0.0
+ pomdolphinscheduler-alert
- ${project.artifactId}
- jar
-
-
- UTF-8
-
-
-
-
- org.apache.dolphinscheduler
- dolphinscheduler-remote
-
-
-
- org.apache.dolphinscheduler
- dolphinscheduler-dao
-
-
- log4j-api
- org.apache.logging.log4j
-
-
-
-
- junit
- junit
- test
-
-
-
- com.fasterxml.jackson.core
- jackson-core
-
-
-
- com.fasterxml.jackson.core
- jackson-databind
-
+
+ dolphinscheduler-alert-api
+ dolphinscheduler-alert-plugins
+ dolphinscheduler-alert-server
+
+ org.slf4jslf4j-api
-
- com.google.guava
- guava
-
-
- jsr305
- com.google.code.findbugs
-
-
-
-
- ch.qos.logback
- logback-classic
-
-
-
-
- org.apache.poi
- poi
-
-
-
- org.powermock
- powermock-module-junit4
- test
-
-
-
- org.mockito
- mockito-core
- test
-
-
-
- org.powermock
- powermock-api-mockito2
- test
-
-
- org.mockito
- mockito-core
-
-
-
-
-
- org.jacoco
- org.jacoco.agent
- runtime
+ org.springframework.boot
+ spring-boot-starter-testtest
-
diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/AlertPluginManager.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/AlertPluginManager.java
deleted file mode 100644
index 02f4b0ff8a4e90b62a15569e805a60f09b45457a..0000000000000000000000000000000000000000
--- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/AlertPluginManager.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * 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.dolphinscheduler.alert.plugin;
-
-import static java.lang.String.format;
-import static java.util.Objects.requireNonNull;
-
-import static com.google.common.base.Preconditions.checkState;
-
-import org.apache.dolphinscheduler.common.enums.PluginType;
-import org.apache.dolphinscheduler.spi.plugin.AbstractDolphinPluginManager;
-import org.apache.dolphinscheduler.dao.DaoFactory;
-import org.apache.dolphinscheduler.dao.PluginDao;
-import org.apache.dolphinscheduler.dao.entity.PluginDefine;
-import org.apache.dolphinscheduler.spi.DolphinSchedulerPlugin;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory;
-import org.apache.dolphinscheduler.spi.classloader.ThreadContextClassLoader;
-import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer;
-import org.apache.dolphinscheduler.spi.params.base.PluginParams;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * load the configured alert plugin and manager them
- */
-public class AlertPluginManager extends AbstractDolphinPluginManager {
- private static final Logger logger = LoggerFactory.getLogger(AlertPluginManager.class);
-
- private final Map alertChannelFactoryMap = new ConcurrentHashMap<>();
- private final Map alertChannelMap = new ConcurrentHashMap<>();
-
- /**
- * k->pluginDefineId v->pluginDefineName
- */
- private final Map pluginDefineMap = new HashMap<>();
-
- private final PluginDao pluginDao = DaoFactory.getDaoInstance(PluginDao.class);
-
- private void addAlertChannelFactory(AlertChannelFactory alertChannelFactory) {
- requireNonNull(alertChannelFactory, "alertChannelFactory is null");
-
- if (alertChannelFactoryMap.putIfAbsent(alertChannelFactory.getName(), alertChannelFactory) != null) {
- throw new IllegalArgumentException(format("Alert Plugin '%s' is already registered", alertChannelFactory.getName()));
- }
-
- try {
- loadAlertChannel(alertChannelFactory.getName());
- } catch (Exception e) {
- throw new IllegalArgumentException(format("Alert Plugin '%s' is can not load .", alertChannelFactory.getName()));
- }
- }
-
- private void loadAlertChannel(String name) {
- requireNonNull(name, "name is null");
-
- AlertChannelFactory alertChannelFactory = alertChannelFactoryMap.get(name);
- checkState(alertChannelFactory != null, "Alert Plugin %s is not registered", name);
-
- try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(alertChannelFactory.getClass().getClassLoader())) {
- AlertChannel alertChannel = alertChannelFactory.create();
- this.alertChannelMap.put(name, alertChannel);
- }
-
- logger.info("-- Loaded Alert Plugin {} --", name);
- }
-
- Map getAlertChannelFactoryMap() {
- return alertChannelFactoryMap;
- }
-
- public Map getAlertChannelMap() {
- return alertChannelMap;
- }
-
- public String getPluginNameById(int id) {
- return pluginDefineMap.get(id);
- }
-
- @Override
- public void installPlugin(DolphinSchedulerPlugin dolphinSchedulerPlugin) {
- for (AlertChannelFactory alertChannelFactory : dolphinSchedulerPlugin.getAlertChannelFactorys()) {
- logger.info("Registering Alert Plugin '{}'", alertChannelFactory.getName());
- this.addAlertChannelFactory(alertChannelFactory);
- List params = alertChannelFactory.getParams();
- String nameEn = alertChannelFactory.getName();
- String paramsJson = PluginParamsTransfer.transferParamsToJson(params);
-
- PluginDefine pluginDefine = new PluginDefine(nameEn, PluginType.ALERT.getDesc(), paramsJson);
- int id = pluginDao.addOrUpdatePluginDefine(pluginDefine);
- pluginDefineMap.put(id, pluginDefine.getPluginName());
- }
- }
-}
diff --git a/dolphinscheduler-alert/src/main/resources/alert.properties b/dolphinscheduler-alert/src/main/resources/alert.properties
deleted file mode 100644
index c41ae61237d0212932380c5cd7ac86471f826148..0000000000000000000000000000000000000000
--- a/dolphinscheduler-alert/src/main/resources/alert.properties
+++ /dev/null
@@ -1,30 +0,0 @@
-#
-# 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.
-#
-
-#This configuration file configures the configuration parameters related to the AlertServer.
-#These parameters are only related to the AlertServer, and it has nothing to do with the specific Alert Plugin.
-#eg : max retry num.
-#eg : Alert Server Listener port
-
-#alert.plugin.dir config the Alert Plugin dir . AlertServer while find and load the Alert Plugin Jar from this dir when deploy and start AlertServer on the server .
-#alert.plugin.dir=lib/plugin/alert
-
-#maven.local.repository=/Users/gaojun/Documents/jianguoyun/localRepository
-
-#alert.plugin.binding config the Alert Plugin need be load when development and run in IDE
-#alert.plugin.binding=\
-# ./dolphinscheduler-alert-plugin/dolphinscheduler-alert-email/pom.xml
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java
deleted file mode 100644
index 38fb6b055ecd30b7b10d4b5f815d7821552aba78..0000000000000000000000000000000000000000
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/AlertServerTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * 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.dolphinscheduler.alert;
-
-import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginLoader;
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginManagerConfig;
-import org.apache.dolphinscheduler.alert.runner.AlertSender;
-import org.apache.dolphinscheduler.alert.utils.Constants;
-import org.apache.dolphinscheduler.dao.AlertDao;
-import org.apache.dolphinscheduler.dao.DaoFactory;
-import org.apache.dolphinscheduler.dao.PluginDao;
-import org.apache.dolphinscheduler.remote.NettyRemotingServer;
-import org.apache.dolphinscheduler.spi.alert.AlertChannel;
-
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mockito;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({AlertServer.class, DaoFactory.class})
-public class AlertServerTest {
-
- @Before
- public void before() {
-
- }
-
- @Test
- public void testMain() throws Exception {
- AlertDao alertDao = PowerMockito.mock(AlertDao.class);
- PowerMockito.mockStatic(DaoFactory.class);
- PowerMockito.when(DaoFactory.getDaoInstance(AlertDao.class)).thenReturn(alertDao);
-
- PluginDao pluginDao = PowerMockito.mock(PluginDao.class);
- PowerMockito.when(DaoFactory.getDaoInstance(PluginDao.class)).thenReturn(pluginDao);
- PowerMockito.when(pluginDao.checkPluginDefineTableExist()).thenReturn(true);
-
- AlertChannel alertChannelMock = PowerMockito.mock(AlertChannel.class);
-
- AlertPluginManager alertPluginManager = PowerMockito.mock(AlertPluginManager.class);
- PowerMockito.whenNew(AlertPluginManager.class).withNoArguments().thenReturn(alertPluginManager);
- ConcurrentHashMap alertChannelMap = new ConcurrentHashMap<>();
- alertChannelMap.put("pluginName", alertChannelMock);
- PowerMockito.when(alertPluginManager.getPluginNameById(Mockito.anyInt())).thenReturn("pluginName");
- PowerMockito.when(alertPluginManager.getAlertChannelMap()).thenReturn(alertChannelMap);
-
- DolphinPluginManagerConfig alertPluginManagerConfig = PowerMockito.mock(DolphinPluginManagerConfig.class);
- PowerMockito.whenNew(DolphinPluginManagerConfig.class).withNoArguments().thenReturn(alertPluginManagerConfig);
-
- NettyRemotingServer nettyRemotingServer = PowerMockito.mock(NettyRemotingServer.class);
- PowerMockito.whenNew(NettyRemotingServer.class).withAnyArguments().thenReturn(nettyRemotingServer);
- AlertSender alertSender = PowerMockito.mock(AlertSender.class);
- PowerMockito.whenNew(AlertSender.class).withAnyArguments().thenReturn(alertSender);
-
- DolphinPluginLoader dolphinPluginLoader = PowerMockito.mock(DolphinPluginLoader.class);
- PowerMockito.whenNew(DolphinPluginLoader.class).withAnyArguments().thenReturn(dolphinPluginLoader);
-
- AlertServer alertServer = AlertServer.getInstance();
- Assert.assertNotNull(alertServer);
-
- new Thread(() -> alertServer.start()).start();
-
- Thread.sleep(5 * Constants.ALERT_SCAN_INTERVAL);
-
- alertServer.stop();
- }
-
-}
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/DolphinPluginLoaderTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/DolphinPluginLoaderTest.java
deleted file mode 100644
index aceb6a1f72cb836f89d3b37727d1044114371bcd..0000000000000000000000000000000000000000
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/plugin/DolphinPluginLoaderTest.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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.dolphinscheduler.alert.plugin;
-
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginLoader;
-import org.apache.dolphinscheduler.spi.plugin.DolphinPluginManagerConfig;
-
-import java.util.Objects;
-
-import org.junit.Assert;
-import org.junit.Ignore;
-import org.junit.Test;
-
-import com.google.common.collect.ImmutableList;
-
-/**
- * DolphinPluginLoader Tester.
- */
-@Ignore("load jar fail,don't care plugin,should mock plugin")
-public class DolphinPluginLoaderTest {
-
- /**
- * Method: loadPlugins()
- */
- @Test
- public void testLoadPlugins() {
- AlertPluginManager alertPluginManager = new AlertPluginManager();
- DolphinPluginManagerConfig alertPluginManagerConfig = new DolphinPluginManagerConfig();
- String path = Objects.requireNonNull(DolphinPluginLoader.class.getClassLoader().getResource("")).getPath();
- alertPluginManagerConfig.setPlugins(path + "../../../dolphinscheduler-alert-plugin/dolphinscheduler-alert-email/pom.xml");
- DolphinPluginLoader alertPluginLoader = new DolphinPluginLoader(alertPluginManagerConfig, ImmutableList.of(alertPluginManager));
- try {
- alertPluginLoader.loadPlugins();
- } catch (Exception e) {
- throw new RuntimeException("load Alert Plugin Failed !", e);
- }
-
- Assert.assertNotNull(alertPluginManager.getAlertChannelFactoryMap().get("Email"));
- }
-}
diff --git a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/PropertyUtilsTest.java b/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/PropertyUtilsTest.java
deleted file mode 100644
index d72c09ae42befab678de98608d85add05d69295d..0000000000000000000000000000000000000000
--- a/dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/PropertyUtilsTest.java
+++ /dev/null
@@ -1,221 +0,0 @@
-/*
- * 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.dolphinscheduler.alert.utils;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import org.apache.dolphinscheduler.common.enums.NodeType;
-import org.apache.dolphinscheduler.common.utils.PropertyUtils;
-
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Test PropertyUtils
- * and the resource path is src/test/resources/alert.properties.
- */
-public class PropertyUtilsTest {
-
- private static final Logger logger = LoggerFactory.getLogger(PropertyUtilsTest.class);
-
- /**
- * Test getString
- */
- @Test
- public void testGetString() {
-
- //Expected "EMAIL"
- String result = PropertyUtils.getString("alert.type");
- logger.info(result);
- assertEquals("EMAIL", result);
-
- //Expected "xxx.xxx.test"
- result = PropertyUtils.getString("mail.server.host");
- assertEquals("xxx.xxx.test", result);
-
- //If key is undefine in alert.properties, then return null
- result = PropertyUtils.getString("abc");
- assertNull(result);
-
- //If key is null, then return null
- result = PropertyUtils.getString(null);
- assertNull(result);
- }
-
-
- /**
- * Test getBoolean
- */
- @Test
- public void testGetBoolean() {
-
- //Expected true
- Boolean result = PropertyUtils.getBoolean("mail.smtp.starttls.enable");
- assertTrue(result);
-
- //Expected false
- result = PropertyUtils.getBoolean("mail.smtp.ssl.enable");
- assertFalse(result);
-
- //If key is undefine in alert.properties, then return null
- result = PropertyUtils.getBoolean("abc");
- assertFalse(result);
-
- //If key is null, then return false
- result = PropertyUtils.getBoolean(null);
- assertFalse(result);
- }
-
- /**
- * Test getLong
- */
- @Test
- public void testGetLong() {
-
- //Expected 25
- long result = PropertyUtils.getLong("mail.server.port");
- assertSame(25L, result);
-
- //If key is null, then return -1
- result = PropertyUtils.getLong(null);
- assertSame(-1L, result);
-
- //If key is undefine in alert.properties, then return -1
- result = PropertyUtils.getLong("abc");
- assertSame(-1L, result);
-
- //If key is undefine in alert.properties, and there is a defaultval, then return defaultval
- result = PropertyUtils.getLong("abc", 200);
- assertEquals(200L, result);
-
- //If the value can not parse to long ,it will log the error and return -1L
- result = PropertyUtils.getLong("test.server.testnumber");
- assertSame(-1L, result);
- }
-
- /**
- * Test getDouble
- */
- @Test
- public void testGetDouble() {
-
- //Expected 3.0
- double result = PropertyUtils.getDouble("test.server.factor", 3.0);
- assertEquals(3.0, result, 0);
-
- //If key is null, then return -1.0
- result = PropertyUtils.getDouble(null, -1.0);
- assertEquals(-1.0, result, 0);
-
- //If key is undefine in alert.properties, then return -1
- result = PropertyUtils.getDouble("abc", -1.0);
- assertEquals(-1.0, result, 0);
-
- //If key is undefine in alert.properties, and there is a defaultval, then return defaultval
- result = PropertyUtils.getDouble("abc", 5.0);
- assertEquals(5.0, result, 0);
-
- //If the value can not parse to double ,it will log the error and return -1.0
- result = PropertyUtils.getDouble("test.server.testnumber", -1.0);
- assertEquals(-1.0, result, 0);
- }
-
- /**
- * Test getArray
- */
- @Test
- public void testGetArray() {
-
- //Expected length 3
- String[] result = PropertyUtils.getArray("test.server.list", ",");
- assertEquals(result.length, 3);
-
- //Equal array values
- assertEquals("xxx.xxx.test1", result[0]);
- assertEquals("xxx.xxx.test2", result[1]);
- assertEquals("xxx.xxx.test3", result[2]);
-
- //If key is null, then return -1
- result = PropertyUtils.getArray(null, ",");
- assertNull(result);
-
- //If key is undefine in alert.properties, then return null
- result = PropertyUtils.getArray("abc", ",");
- assertNull(result);
-
- //If splitStr is null, then return null
- result = PropertyUtils.getArray("test.server.list", null);
- assertNull(result);
- }
-
- /**
- * test getInt
- */
- @Test
- public void testGetInt() {
-
- //Expected 25
- int result = PropertyUtils.getInt("mail.server.port");
- assertSame(25, result);
-
- //If key is null, then return -1
- result = PropertyUtils.getInt(null);
- assertSame(-1, result);
-
- //If key is undefine in alert.properties, then return -1
- result = PropertyUtils.getInt("abc");
- assertSame(-1, result);
-
- //If key is undefine in alert.properties, and there is a defaultval, then return defaultval
- result = PropertyUtils.getInt("abc", 300);
- assertEquals(300, result);
-
- //If the value can not parse to int ,it will log the error and return -1
- result = PropertyUtils.getInt("test.server.testnumber");
- assertSame(-1, result);
- }
-
- /**
- * Test getEnum
- */
- @Test
- public void testGetEnum() {
-
- //Expected MASTER
- NodeType nodeType = PropertyUtils.getEnum("test.server.enum1", NodeType.class, NodeType.WORKER);
- assertEquals(NodeType.MASTER, nodeType);
-
- //Expected DEAD_SERVER
- nodeType = PropertyUtils.getEnum("test.server.enum2", NodeType.class, NodeType.WORKER);
- assertEquals(NodeType.DEAD_SERVER, nodeType);
-
- //If key is null, then return defaultval
- nodeType = PropertyUtils.getEnum(null, NodeType.class, NodeType.WORKER);
- assertEquals(NodeType.WORKER, nodeType);
-
- //If the value doesn't define in enum ,it will log the error and return -1
- nodeType = PropertyUtils.getEnum("test.server.enum3", NodeType.class, NodeType.WORKER);
- assertEquals(NodeType.WORKER, nodeType);
- }
-
-}
diff --git a/dolphinscheduler-api/pom.xml b/dolphinscheduler-api/pom.xml
index c6c976003844c828b38e5872a37b10b44caf1af5..ed03dbc9c327c63a4f2212218d52aa59038ad682 100644
--- a/dolphinscheduler-api/pom.xml
+++ b/dolphinscheduler-api/pom.xml
@@ -16,13 +16,12 @@
~ limitations under the License.
-->
-
+4.0.0org.apache.dolphinschedulerdolphinscheduler
- 2.0.0-SNAPSHOT
+ 2.0.10-SNAPSHOTdolphinscheduler-api${project.artifactId}
@@ -35,6 +34,12 @@
dolphinscheduler-service
+
+ org.apache.dolphinscheduler
+ dolphinscheduler-datasource-all
+ ${project.version}
+
+
org.springframework.boot
@@ -222,13 +227,10 @@
-
- junit
- junit
- test
+ org.hibernate.validator
+ hibernate-validator
-
org.powermockpowermock-module-junit4
@@ -239,20 +241,35 @@
org.powermockpowermock-api-mockito2test
+
+
+
+ org.apache.curator
+ curator-test
+ ${curator.test}
- org.mockito
- mockito-core
+ org.javassist
+ javassist
-
-
-
- org.jacoco
- org.jacoco.agent
- runtimetest
-
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+ *.yaml
+ *.yml
+ *.xml
+
+
+
+
+
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/ApiApplicationServer.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/ApiApplicationServer.java
index 6afb9d54c2e3948add4d1ac6b298267e841a2b6b..d622fa3a5c76e6799ba1a453ab7175c3105d29b5 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/ApiApplicationServer.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/ApiApplicationServer.java
@@ -17,21 +17,39 @@
package org.apache.dolphinscheduler.api;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
+import static org.apache.dolphinscheduler.common.Constants.SPRING_DATASOURCE_DRIVER_CLASS_NAME;
+
+import org.apache.dolphinscheduler.common.utils.PropertyUtils;
+
+import javax.annotation.PostConstruct;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
-@SpringBootApplication
+@EnableAutoConfiguration
@ServletComponentScan
-@ComponentScan(value = "org.apache.dolphinscheduler",
- excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.apache.dolphinscheduler.server.*"))
+@ComponentScan(value = "org.apache.dolphinscheduler", excludeFilters = {
+ @ComponentScan.Filter(type = FilterType.REGEX, pattern = {
+ "org.apache.dolphinscheduler.server.*",
+ "org.apache.dolphinscheduler.alert.*"
+ })
+})
public class ApiApplicationServer extends SpringBootServletInitializer {
public static void main(String[] args) {
- SpringApplication.run(ApiApplicationServer.class, args);
+ new SpringApplicationBuilder(ApiApplicationServer.class).profiles("api").run(args);
}
+ @Value("${spring.datasource.driver-class-name}")
+ private String driverClassName;
+
+ @PostConstruct
+ public void run() {
+ PropertyUtils.setValue(SPRING_DATASOURCE_DRIVER_CLASS_NAME, driverClassName);
+ }
}
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAnnotation.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAnnotation.java
index a7bee62a7ef932f54b7041d44c684296901a3452..db62bcda3afc658d26a6df58625c0ae9f101143d 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAnnotation.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAnnotation.java
@@ -28,7 +28,7 @@ import java.lang.annotation.Target;
@Documented
public @interface AccessLogAnnotation {
// ignore request args
- String[] ignoreRequestArgs() default {};
+ String[] ignoreRequestArgs() default {"loginUser"};
boolean ignoreRequest() default false;
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAspect.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAspect.java
index a34d04124267489d53befcba434602228d5bb6b1..b20b6d84254a25fd76d423227cb6cb1e6257cc77 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAspect.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/AccessLogAspect.java
@@ -19,14 +19,17 @@ package org.apache.dolphinscheduler.api.aspect;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
+import org.apache.dolphinscheduler.spi.utils.StringUtils;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
-import java.util.List;
import java.util.Set;
import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
import javax.servlet.http.HttpServletRequest;
@@ -46,6 +49,12 @@ import org.springframework.web.context.request.ServletRequestAttributes;
public class AccessLogAspect {
private static final Logger logger = LoggerFactory.getLogger(AccessLogAspect.class);
+ private static final String TRACE_ID = "traceId";
+
+ public static final String sensitiveDataRegEx = "(password=[\'\"]+)(\\S+)([\'\"]+)";
+
+ private static final Pattern sensitiveDataPattern = Pattern.compile(sensitiveDataRegEx, Pattern.CASE_INSENSITIVE);
+
@Pointcut("@annotation(org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation)")
public void logPointCut(){
// Do nothing because of it's a pointcut
@@ -60,21 +69,26 @@ public class AccessLogAspect {
Method method = sign.getMethod();
AccessLogAnnotation annotation = method.getAnnotation(AccessLogAnnotation.class);
- String tranceId = UUID.randomUUID().toString();
+ String traceId = UUID.randomUUID().toString();
// log request
if (!annotation.ignoreRequest()) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
-
+ String traceIdFromHeader = request.getHeader(TRACE_ID);
+ if (!StringUtils.isEmpty(traceIdFromHeader)) {
+ traceId = traceIdFromHeader;
+ }
// handle login info
String userName = parseLoginInfo(request);
// handle args
String argsString = parseArgs(proceedingJoinPoint, annotation);
- logger.info("REQUEST TRANCE_ID:{}, LOGIN_USER:{}, URI:{}, METHOD:{}, HANDLER:{}, ARGS:{}",
- tranceId,
+ // handle sensitive data in the string
+ argsString = handleSensitiveData(argsString);
+ logger.info("REQUEST TRACE_ID:{}, LOGIN_USER:{}, URI:{}, METHOD:{}, HANDLER:{}, ARGS:{}",
+ traceId,
userName,
request.getRequestURI(),
request.getMethod(),
@@ -88,7 +102,7 @@ public class AccessLogAspect {
// log response
if (!annotation.ignoreResponse()) {
- logger.info("RESPONSE TRANCE_ID:{}, BODY:{}, REQUEST DURATION:{} milliseconds", tranceId, ob, (System.currentTimeMillis() - startTime));
+ logger.info("RESPONSE TRACE_ID:{}, BODY:{}, REQUEST DURATION:{} milliseconds", traceId, ob, (System.currentTimeMillis() - startTime));
}
return ob;
@@ -114,6 +128,28 @@ public class AccessLogAspect {
return argsString;
}
+ protected String handleSensitiveData(String originalData) {
+ Matcher matcher = sensitiveDataPattern.matcher(originalData.toLowerCase());
+ IntStream stream = IntStream.builder().build();
+ boolean exists = false;
+ while (matcher.find()) {
+ if (matcher.groupCount() == 3) {
+ stream = IntStream.concat(stream, IntStream.range(matcher.end(1),matcher.end(2)));
+ exists = true;
+ }
+ }
+
+ if (exists) {
+ char[] chars = originalData.toCharArray();
+ stream.forEach(idx -> {
+ chars[idx] = '*';
+ });
+ return new String(chars);
+ }
+
+ return originalData;
+ }
+
private String parseLoginInfo(HttpServletRequest request) {
String userName = "NOT LOGIN";
User loginUser = (User) (request.getAttribute(Constants.SESSION_USER));
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/CacheEvictAspect.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/CacheEvictAspect.java
new file mode 100644
index 0000000000000000000000000000000000000000..e7f1ba188c29ac2deb47f98cca55c07f3630671b
--- /dev/null
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/aspect/CacheEvictAspect.java
@@ -0,0 +1,138 @@
+/*
+ * 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.dolphinscheduler.api.aspect;
+
+import org.apache.dolphinscheduler.common.enums.CacheType;
+import org.apache.dolphinscheduler.remote.command.CacheExpireCommand;
+import org.apache.dolphinscheduler.service.cache.CacheNotifyService;
+import org.apache.dolphinscheduler.service.cache.impl.CacheKeyGenerator;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.expression.EvaluationContext;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.expression.spel.support.StandardEvaluationContext;
+import org.springframework.stereotype.Component;
+
+/**
+ * aspect for cache evict
+ */
+@Aspect
+@Component
+public class CacheEvictAspect {
+
+ private static final Logger logger = LoggerFactory.getLogger(CacheEvictAspect.class);
+
+ /**
+ * symbol of spring el
+ */
+ private static final String EL_SYMBOL = "#";
+
+ /**
+ * prefix of spring el
+ */
+ private static final String P = "p";
+
+ @Autowired
+ private CacheKeyGenerator cacheKeyGenerator;
+
+ @Autowired
+ private CacheNotifyService cacheNotifyService;
+
+ @Pointcut("@annotation(org.springframework.cache.annotation.CacheEvict)")
+ public void cacheEvictPointCut() {
+ // Do nothing because of it's a pointcut
+ }
+
+ @Around("cacheEvictPointCut()")
+ public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
+ MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature();
+ Method method = sign.getMethod();
+ Object target = proceedingJoinPoint.getTarget();
+ Object[] args = proceedingJoinPoint.getArgs();
+
+ Object result = proceedingJoinPoint.proceed();
+
+ CacheConfig cacheConfig = method.getDeclaringClass().getAnnotation(CacheConfig.class);
+ CacheEvict cacheEvict = method.getAnnotation(CacheEvict.class);
+
+ CacheType cacheType = getCacheType(cacheConfig, cacheEvict);
+ if (cacheType != null) {
+ String cacheKey;
+ if (cacheEvict.key().isEmpty()) {
+ cacheKey = (String) cacheKeyGenerator.generate(target, method, args);
+ } else {
+ cacheKey = cacheEvict.key();
+ if (cacheEvict.key().contains(EL_SYMBOL)) {
+ cacheKey = parseKey(cacheEvict.key(), Arrays.asList(args));
+ }
+ }
+ if (StringUtils.isNotEmpty(cacheKey)) {
+ cacheNotifyService.notifyMaster(new CacheExpireCommand(cacheType, cacheKey).convert2Command());
+ }
+ }
+
+ return result;
+ }
+
+ private CacheType getCacheType(CacheConfig cacheConfig, CacheEvict cacheEvict) {
+ String cacheName = null;
+ if (cacheEvict.cacheNames().length > 0) {
+ cacheName = cacheEvict.cacheNames()[0];
+ }
+ if (cacheConfig.cacheNames().length > 0) {
+ cacheName = cacheConfig.cacheNames()[0];
+ }
+ if (cacheName == null) {
+ return null;
+ }
+ for (CacheType cacheType : CacheType.values()) {
+ if (cacheType.getCacheName().equals(cacheName)) {
+ return cacheType;
+ }
+ }
+ return null;
+ }
+
+ private String parseKey(String key, List