Skip to content

Commit de0de1c

Browse files
authored
[feature]Hertzbeat custom plugin. (apache#1973)
1 parent a69d05a commit de0de1c

File tree

8 files changed

+135
-1
lines changed

8 files changed

+135
-1
lines changed

.licenserc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ header:
5252
- '**/NOTICE'
5353
- '.all-contributorsrc'
5454
- '**/*.AbstractCollect'
55+
- '**/*.Plugin'
5556
- '**/*.MockMaker'
5657
- '.prettierrc'
5758
- '.browserslistrc'

manager/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<groupId>org.apache.hertzbeat</groupId>
8383
<artifactId>hertzbeat-push</artifactId>
8484
</dependency>
85+
<!-- plugin -->
86+
<dependency>
87+
<groupId>org.apache.hertzbeat</groupId>
88+
<artifactId>hertzbeat-plugin</artifactId>
89+
</dependency>
8590
<!-- spring -->
8691
<dependency>
8792
<groupId>org.springframework.boot</groupId>

manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarm.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.Maps;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.ServiceLoader;
2324
import lombok.extern.slf4j.Slf4j;
2425
import org.apache.hertzbeat.alert.AlerterWorkerPool;
2526
import org.apache.hertzbeat.common.entity.alerter.Alert;
@@ -30,6 +31,7 @@
3031
import org.apache.hertzbeat.manager.service.NoticeConfigService;
3132
import org.apache.hertzbeat.manager.support.exception.AlertNoticeException;
3233
import org.apache.hertzbeat.manager.support.exception.IgnoreException;
34+
import org.apache.hertzbeat.plugin.Plugin;
3335
import org.springframework.beans.factory.InitializingBean;
3436
import org.springframework.stereotype.Component;
3537

@@ -61,7 +63,7 @@ public DispatcherAlarm(AlerterWorkerPool workerPool,
6163
}
6264

6365
@Override
64-
public void afterPropertiesSet() throws Exception {
66+
public void afterPropertiesSet() {
6567
// Start alarm distribution
6668
DispatchTask dispatchTask = new DispatchTask();
6769
for (int i = 0; i < DISPATCH_THREADS; i++) {
@@ -113,6 +115,11 @@ public void run() {
113115
alertStoreHandler.store(alert);
114116
// Notice distribution
115117
sendNotify(alert);
118+
// Execute the plugin
119+
ServiceLoader<Plugin> loader = ServiceLoader.load(Plugin.class, Plugin.class.getClassLoader());
120+
for (Plugin plugin : loader) {
121+
plugin.alert(alert);
122+
}
116123
}
117124
} catch (IgnoreException ignored) {
118125
} catch (InterruptedException e) {

plugin/pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<parent>
22+
<artifactId>hertzbeat</artifactId>
23+
<groupId>org.apache.hertzbeat</groupId>
24+
<version>2.0-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
28+
<artifactId>hertzbeat-plugin</artifactId>
29+
<name>${project.artifactId}</name>
30+
<properties>
31+
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
32+
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
33+
</properties>
34+
35+
<dependencies>
36+
<!-- common -->
37+
<dependency>
38+
<groupId>org.apache.hertzbeat</groupId>
39+
<artifactId>hertzbeat-common</artifactId>
40+
</dependency>
41+
</dependencies>
42+
43+
44+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
package org.apache.hertzbeat.plugin;
20+
21+
import org.apache.hertzbeat.common.entity.alerter.Alert;
22+
23+
/**
24+
* Plugin
25+
*/
26+
public interface Plugin {
27+
28+
/*
29+
* execute when alert
30+
*/
31+
void alert(Alert alert);
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
package org.apache.hertzbeat.plugin.impl;
20+
21+
import lombok.extern.slf4j.Slf4j;
22+
import org.apache.hertzbeat.common.entity.alerter.Alert;
23+
import org.apache.hertzbeat.plugin.Plugin;
24+
25+
/**
26+
* DemoPlugin
27+
*/
28+
@Slf4j
29+
public class DemoPluginImpl implements Plugin {
30+
/*
31+
* execute when alert
32+
*/
33+
@Override
34+
public void alert(Alert alert) {
35+
log.info("DemoPluginImpl alert: {}", alert);
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.apache.hertzbeat.plugin.impl.DemoPluginImpl

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<module>warehouse</module>
8787
<module>remoting</module>
8888
<module>push</module>
89+
<module>plugin</module>
8990
</modules>
9091

9192
<properties>
@@ -177,6 +178,12 @@
177178
<artifactId>hertzbeat-push</artifactId>
178179
<version>${hertzbeat.version}</version>
179180
</dependency>
181+
<!-- plugin -->
182+
<dependency>
183+
<groupId>org.apache.hertzbeat</groupId>
184+
<artifactId>hertzbeat-plugin</artifactId>
185+
<version>${hertzbeat.version}</version>
186+
</dependency>
180187
<!-- spring -->
181188
<dependency>
182189
<groupId>org.springframework.boot</groupId>

0 commit comments

Comments
 (0)