-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
925b052
commit 3061400
Showing
11 changed files
with
437 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ntcore-core/src/main/java/com/huaweicloud/sermant/core/notification/NotificationInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.core.notification; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* 通知信息 | ||
* | ||
* @author zhp | ||
* @since 2023-06-16 | ||
*/ | ||
public class NotificationInfo { | ||
/** | ||
* 通知类型 | ||
*/ | ||
private NotificationType notificationType; | ||
|
||
/** | ||
* 通知开始事件 | ||
*/ | ||
private Date startDate; | ||
|
||
/** | ||
* 通知内容 | ||
*/ | ||
private Object content; | ||
|
||
/** | ||
* 构造方法 | ||
* | ||
* @param notificationType 通知类型 | ||
* @param startDate 通知开始事件 | ||
* @param content 通知内容 | ||
*/ | ||
public NotificationInfo(NotificationType notificationType, Date startDate, Object content) { | ||
this.notificationType = notificationType; | ||
this.startDate = startDate; | ||
this.content = content; | ||
} | ||
|
||
/** | ||
* 无参构造 | ||
*/ | ||
public NotificationInfo() { | ||
} | ||
|
||
public NotificationType getNotificationType() { | ||
return notificationType; | ||
} | ||
|
||
public void setNotificationType(NotificationType notificationType) { | ||
this.notificationType = notificationType; | ||
} | ||
|
||
public Date getStartDate() { | ||
return startDate; | ||
} | ||
|
||
public void setStartDate(Date startDate) { | ||
this.startDate = startDate; | ||
} | ||
|
||
public Object getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(Object content) { | ||
this.content = content; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...re-core/src/main/java/com/huaweicloud/sermant/core/notification/NotificationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.core.notification; | ||
|
||
/** | ||
* 通知监听器 | ||
* | ||
* @author zhp | ||
* @since 2023-06-16 | ||
*/ | ||
public interface NotificationListener { | ||
/** | ||
* 通知 | ||
* | ||
* @param notificationInfo 通知信息 | ||
*/ | ||
void notify(NotificationInfo notificationInfo); | ||
} |
90 changes: 90 additions & 0 deletions
90
...ore-core/src/main/java/com/huaweicloud/sermant/core/notification/NotificationManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.core.notification; | ||
|
||
import com.huaweicloud.sermant.core.config.ConfigManager; | ||
import com.huaweicloud.sermant.core.notification.config.NotificationConfig; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* 通知管理器 | ||
* | ||
* @author zhp | ||
* @since 2023-06-16 | ||
*/ | ||
public class NotificationManager { | ||
private static final Map<String, List<NotificationListener>> NOTIFICATION_LISTENER_MAP = new ConcurrentHashMap<>(); | ||
|
||
private static NotificationConfig notificationConfig = ConfigManager.getConfig(NotificationConfig.class); | ||
|
||
private NotificationManager() { | ||
} | ||
|
||
/** | ||
* 通知监听器注册 | ||
* | ||
* @param notificationListener 通知监听器 | ||
* @param notificationType 通知类型 | ||
*/ | ||
public static void registry(NotificationListener notificationListener, NotificationType notificationType) { | ||
if (notificationConfig == null || !notificationConfig.isEnable()) { | ||
return; | ||
} | ||
List<NotificationListener> listenerList = NOTIFICATION_LISTENER_MAP.computeIfAbsent(notificationType.getName(), | ||
s -> new ArrayList<>()); | ||
listenerList.add(notificationListener); | ||
} | ||
|
||
/** | ||
* 通知监听器取消注册 | ||
* | ||
* @param notificationListener 通知监听器 | ||
* @param notificationType 通知类型 | ||
*/ | ||
public static void unRegistry(NotificationListener notificationListener, NotificationType notificationType) { | ||
if (notificationConfig == null || !notificationConfig.isEnable()) { | ||
return; | ||
} | ||
List<NotificationListener> listenerList = NOTIFICATION_LISTENER_MAP.get(notificationType.getName()); | ||
if (listenerList != null) { | ||
listenerList.remove(notificationListener); | ||
} | ||
} | ||
|
||
/** | ||
* 事件通知 | ||
* | ||
* @param notificationInfo 通知信息 | ||
*/ | ||
public static void sendNotification(NotificationInfo notificationInfo) { | ||
if (notificationConfig == null || !notificationConfig.isEnable()) { | ||
return; | ||
} | ||
List<NotificationListener> notificationListeners = | ||
NOTIFICATION_LISTENER_MAP.get(notificationInfo.getNotificationType().getName()); | ||
if (notificationListeners == null || notificationListeners.isEmpty()) { | ||
return; | ||
} | ||
notificationListeners.forEach(notificationListener -> | ||
CompletableFuture.runAsync(() -> notificationListener.notify(notificationInfo))); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ntcore-core/src/main/java/com/huaweicloud/sermant/core/notification/NotificationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.core.notification; | ||
|
||
/** | ||
* 通知类型 | ||
* | ||
* @author zhp | ||
* @since 2023-06-16 | ||
*/ | ||
public enum NotificationType { | ||
/** | ||
* sermant启动完成 | ||
*/ | ||
SERMANT_START_COMPLETE("SERMANT_START_COMPLETE", "sermant启动完成"), | ||
|
||
/** | ||
* netty已链接 | ||
*/ | ||
NETTY_CONNECTED("NETTY_CONNECTED", "netty已链接"), | ||
|
||
/** | ||
* netty链接断开 | ||
*/ | ||
NETTY_DISCONNECTED("NETTY_DISCONNECTED", "netty链接断开"), | ||
|
||
/** | ||
* zookeeper已链接 | ||
*/ | ||
ZOOKEEPER_CONNECTED("ZOOKEEPER_CONNECTED", "zookeeper已链接"), | ||
|
||
/** | ||
* zookeeper链接断开 | ||
*/ | ||
ZOOKEEPER_DISCONNECTED("ZOOKEEPER_DISCONNECTED", "zookeeper链接断开"); | ||
|
||
private final String name; | ||
|
||
private final String description; | ||
|
||
NotificationType(String name, String description) { | ||
this.name = name; | ||
this.description = description; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...re/src/main/java/com/huaweicloud/sermant/core/notification/config/NotificationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed 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 com.huaweicloud.sermant.core.notification.config; | ||
|
||
import com.huaweicloud.sermant.core.config.common.BaseConfig; | ||
import com.huaweicloud.sermant.core.config.common.ConfigTypeKey; | ||
|
||
/** | ||
* 通知配置 | ||
* | ||
* @author zhp | ||
* @since 2023-06-16 | ||
*/ | ||
@ConfigTypeKey("notification") | ||
public class NotificationConfig implements BaseConfig { | ||
private boolean enable; | ||
|
||
public boolean isEnable() { | ||
return enable; | ||
} | ||
|
||
public void setEnable(boolean enable) { | ||
this.enable = enable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.