Initial commit: reorganized source tree

- backend/: 13 Maven modules (cw-elevator-application, cloudwalk-cloud, intelligent-cwoscomponent, ninca-crk, etc.)
- frontend/: 4 Vue projects (elevator-front, cwos-portal, alarm-front, front_acs) + decompiled + scripts
- scripts/: build, test-env, tools (Docker Compose, service templates, API parity)
- docs/: AGENTS.md, superpowers specs, architecture docs
- .gitignore: standard Java/Maven exclusions

Moved from legacy maven-*/ root layout to backend/ organized structure.
This commit is contained in:
hpd840321
2026-05-09 09:00:12 +08:00
commit 7b2bd307f1
7260 changed files with 612980 additions and 0 deletions
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.cloudwalk.cloud</groupId>
<artifactId>cloudwalk-cloud-reactor</artifactId>
<version>4.0.0-Brussels-SRX</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>cloudwalk-common-event</artifactId>
<packaging>jar</packaging>
<description>云从科技-消息总线封装(源码已迁入本模块 src/main/java</description>
<properties>
<alibaba.eclipse.codestyle.path>${project.basedir}/../../docs/style/alibaba-eclipse-codestyle.xml</alibaba.eclipse.codestyle.path>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</dependency>
<dependency>
<groupId>cn.cloudwalk.cloud</groupId>
<artifactId>cwos-sdk-event</artifactId>
<!-- 旧版 cwos-sdk-event POM 依赖 reflections-maven,会解析到 JFrog 已下线构件,编译不需要 -->
<exclusions>
<exclusion>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,362 @@
package cn.cloudwalk.event;
import cn.cloudwalk.cwos.client.event.EventClient;
import cn.cloudwalk.cwos.client.event.event.BaseEvent;
import cn.cloudwalk.cwos.client.event.event.CustomEvent;
import cn.cloudwalk.cwos.client.event.event.EventType;
import cn.cloudwalk.cwos.client.event.handler.EventListener;
import cn.cloudwalk.event.annotation.ConsumerGroup;
import cn.cloudwalk.event.annotation.CustomTopic;
import cn.cloudwalk.event.annotation.EventTopicSuffix;
import cn.cloudwalk.event.autoconfig.EventProperties;
import cn.cloudwalk.event.handler.CustomEventHandler;
import cn.cloudwalk.event.handler.EventHandler;
import cn.cloudwalk.event.handler.EventHandlerMapping;
import cn.cloudwalk.event.handler.EventHandlerWorker;
import cn.cloudwalk.event.handler.NamedThreadFactory;
import cn.cloudwalk.event.listener.CloudwalkEventListener;
import cn.cloudwalk.event.listener.GroupEventListener;
import cn.cloudwalk.event.listener.GroupListnerClassMapping;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* 应用启动阶段扫描 Spring 容器中的监听器与处理器,构建 {@link cn.cloudwalk.event.handler.EventHandlerMapping} 等运行时结构。
*/
public class CloudwalkEventInitializing implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(CloudwalkEventInitializing.class);
@Autowired(required = false)
private List<EventHandler> eventHandlers;
@Autowired
private CloudwalkEventManager cloudwalkEventManager;
@Autowired
private Environment environment;
private static final Pattern pattern = Pattern.compile("^\\$\\{(.*?)\\}$");
private static final String DEFAULT_SUFFIX = "";
private String defaultGroup;
private EventClient defaultEventClient;
private Map<String, EventClient> eventClientMap = new HashMap<>();
private EventProperties eventProperties;
private static boolean isHandlerEnable = false;
public void run(String... args) throws Exception {
init();
}
public void init() throws Exception {
initEventClient();
initHandlerMapping();
initHandlerExecutor();
subscribeEvent();
}
public CloudwalkEventInitializing(EventProperties eventProperties) {
this.eventProperties = eventProperties;
}
private void initEventClient() throws ClassNotFoundException {
String[] groupIds = this.eventProperties.getGroupId().split(",");
if (groupIds.length == 0) {
throw new IllegalArgumentException("消费组ID不能为空");
}
Map<String, String> listenerClassMap = this.eventProperties.getListenerClass();
GroupListnerClassMapping groupListnerClassMapping = new GroupListnerClassMapping();
for (int i = 0; i < groupIds.length; i++) {
Class<? extends EventListener> listenerClass;
boolean isFirst = (i == 0);
String groupId = groupIds[i];
if (null == listenerClassMap || StringUtils.isEmpty(listenerClassMap.get(groupId))) {
if (isFirst) {
listenerClass = CloudwalkEventListener.class;
} else {
throw new IllegalArgumentException(
String.format("groupId[%s]缺少listener-class的配置", new Object[] {groupId}));
}
} else {
listenerClass = (Class)Class.forName(listenerClassMap.get(groupId));
}
initEventClient(this.eventProperties.getBootstrapServers(), groupId, listenerClass, isFirst);
groupListnerClassMapping.register(listenerClass, groupId);
}
this.cloudwalkEventManager.setGroupListnerClassMapping(groupListnerClassMapping);
this.cloudwalkEventManager.setEventClient(this.defaultEventClient);
}
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
GroupEventListener.applicationContext = applicationContext;
}
private List<String> extractGroupIds(EventHandler eventHandler) {
if (eventHandler.getClass().isAnnotationPresent((Class)ConsumerGroup.class)) {
ConsumerGroup consumerGroup = eventHandler.getClass().<ConsumerGroup>getAnnotation(ConsumerGroup.class);
List<String> groupIds = new LinkedList<>();
for (String groupId : consumerGroup.groupIds()) {
Matcher matcher = pattern.matcher(groupId);
if (matcher.find()) {
String propertyKey = groupId.substring(2, groupId.length() - 1).trim();
String propertyValue = this.environment.getProperty(propertyKey);
if (!StringUtils.isEmpty(propertyValue)) {
groupId = propertyValue;
}
}
groupIds.add(groupId);
}
return groupIds;
}
return new ArrayList<>(Collections.singletonList(this.defaultGroup));
}
private void initHandlerMapping() {
if (CollectionUtils.isEmpty(this.eventHandlers)) {
return;
}
isHandlerEnable = true;
EventHandlerMapping eventHandlerMapping = new EventHandlerMapping();
for (EventHandler<BaseEvent> handler : this.eventHandlers) {
if (handler.getClass().isAnnotationPresent((Class)EventTopicSuffix.class)) {
Type type = handler.getClass().getGenericInterfaces()[0];
ParameterizedType p = (ParameterizedType)type;
Class<BaseEvent> eventClass = (Class)p.getActualTypeArguments()[0];
EventTopicSuffix eventTopicSuffix =
handler.getClass().<EventTopicSuffix>getAnnotation(EventTopicSuffix.class);
registerHandlerMapping(eventHandlerMapping, handler, eventClass, eventTopicSuffix.value());
continue;
}
if (handler.getClass().isAnnotationPresent((Class)CustomTopic.class)) {
CustomTopic customTopic = handler.getClass().<CustomTopic>getAnnotation(CustomTopic.class);
String topic = customTopic.topic();
if (StringUtils.isEmpty(topic)) {
continue;
}
registerCustomHandlerMapping(eventHandlerMapping, (EventHandler)handler, topic, customTopic.suffix());
}
}
this.cloudwalkEventManager.setEventHandlerMapping(eventHandlerMapping);
}
private void initHandlerExecutor() {
if (isHandlerEnable) {
EventHandlerWorker eventHandlerWorker = new EventHandlerWorker();
EventProperties.HandlerExecutorConfig handlerExecutorConfig =
this.eventProperties.getHandlerExecutorConfig();
eventHandlerWorker
.setPoolExecutor(new ThreadPoolExecutor(handlerExecutorConfig.getCorePoolSize().intValue(),
handlerExecutorConfig.getMaximumPoolSize().intValue(), 1000L, TimeUnit.MILLISECONDS,
new SynchronousQueue<>(),
(ThreadFactory)new NamedThreadFactory(this.eventProperties.getWorkerNamePrefix()),
new ThreadPoolExecutor.CallerRunsPolicy()));
this.cloudwalkEventManager.setEventHandlerWorker(eventHandlerWorker);
}
}
private EventType getEventType(Class<? extends BaseEvent> eventClass) {
for (EventType eventType : EventType.values()) {
BaseEvent prototype = eventType.getEventClass();
if (prototype != null && Objects.equals(eventClass, prototype.getClass())) {
return eventType;
}
}
return null;
}
private void subscribeEvent() throws IllegalAccessException, InstantiationException {
EventHandlerMapping eventHandlerMapping = this.cloudwalkEventManager.getEventHandlerMapping();
Map<String, Map<EventType, Set<String>>> subscribedMap = new HashMap<>();
for (Map.Entry<String, Map<EventType, Map<String, List<EventHandler>>>> entry : (Iterable<
Map.Entry<String, Map<EventType, Map<String, List<EventHandler>>>>>)eventHandlerMapping.getHandlerMap()
.entrySet()) {
for (Map.Entry<EventType, Map<String, List<EventHandler>>> eventTypeMapEntry : (Iterable<
Map.Entry<EventType, Map<String, List<EventHandler>>>>)((Map)entry.getValue()).entrySet()) {
for (Object serviceCodeObj : ((Map)eventTypeMapEntry.getValue()).keySet()) {
String serviceCode = (String)serviceCodeObj;
subscribe(subscribedMap, entry.getKey(), eventTypeMapEntry.getKey(), serviceCode);
}
}
}
Map<String, Map<String, Set<String>>> customSubscribedMap = new HashMap<>();
for (Map.Entry<String, Map<String, Map<String, List<CustomEventHandler>>>> entry : (Iterable<
Map.Entry<String, Map<String, Map<String, List<CustomEventHandler>>>>>)eventHandlerMapping
.getCustomHandlerMap().entrySet()) {
for (Map.Entry<String, Map<String, List<CustomEventHandler>>> subEntry : (Iterable<
Map.Entry<String, Map<String, List<CustomEventHandler>>>>)((Map)entry.getValue()).entrySet()) {
for (Map.Entry<String, List<CustomEventHandler>> handlerEntry : (Iterable<
Map.Entry<String, List<CustomEventHandler>>>)((Map)subEntry.getValue()).entrySet()) {
for (CustomEventHandler customEventHandler : handlerEntry.getValue()) {
Type type = customEventHandler.getClass().getGenericInterfaces()[0];
ParameterizedType p = (ParameterizedType)type;
Class<? extends CustomEvent> eventClass = (Class)p.getActualTypeArguments()[0];
customSubscribe(customSubscribedMap, entry.getKey(), subEntry.getKey(), handlerEntry.getKey(),
eventClass);
}
}
}
}
}
private void subscribe(Map<String, Map<EventType, Set<String>>> subscribedMap, String groupId, EventType eventType,
String serviceCode) {
if (StringUtils.isEmpty(groupId)) {
groupId = this.defaultGroup;
}
Map<EventType, Set<String>> eventTypeServiceCodeMap = subscribedMap.get(groupId);
if (null != eventTypeServiceCodeMap) {
Set<String> serviceCodesSet = eventTypeServiceCodeMap.get(eventType);
if (CollectionUtils.isEmpty(serviceCodesSet)) {
eventTypeServiceCodeMap.put(eventType, new HashSet<>(Collections.singleton(serviceCode)));
} else {
serviceCodesSet.add(serviceCode);
}
} else {
eventTypeServiceCodeMap = new HashMap<>();
eventTypeServiceCodeMap.put(eventType, new HashSet<>(Collections.singleton(serviceCode)));
subscribedMap.put(groupId, eventTypeServiceCodeMap);
}
((EventClient)this.eventClientMap.get(groupId)).subEvent(eventType, serviceCode);
}
private void customSubscribe(Map<String, Map<String, Set<String>>> customSubscribedMap, String groupId,
String topic, String serviceCode, Class<? extends CustomEvent> eventClass)
throws IllegalAccessException, InstantiationException {
if (StringUtils.isEmpty(groupId)) {
groupId = this.defaultGroup;
}
Map<String, Set<String>> topicServiceCodeMap = customSubscribedMap.get(groupId);
if (null != topicServiceCodeMap) {
Set<String> serviceCodesSet = topicServiceCodeMap.get(topic);
if (CollectionUtils.isEmpty(serviceCodesSet)) {
topicServiceCodeMap.put(topic, new HashSet<>(Collections.singleton(serviceCode)));
} else {
serviceCodesSet.add(serviceCode);
}
} else {
topicServiceCodeMap = new HashMap<>();
topicServiceCodeMap.put(topic, new HashSet<>(Collections.singleton(serviceCode)));
customSubscribedMap.put(groupId, topicServiceCodeMap);
}
EventClient eventClient = this.eventClientMap.get(groupId);
if (null == eventClient) {
throw new IllegalArgumentException(
String.format("没有找到groupId[%s]对应的配置,请检查 ${cloudwalk.event.group-id} 配置", new Object[] {groupId}));
}
eventClient.subEvent(topic, serviceCode, eventClass.newInstance());
}
private void initEventClient(String bootstrapServers, String groupId, Class<? extends EventListener> listenerClass,
boolean isDefault) {
EventClient eventClient = this.eventClientMap.get(groupId);
if (null == eventClient) {
eventClient = EventClient.getInstance(bootstrapServers, groupId);
eventClient.init();
if (null != this.eventProperties.getFetchDataWorkerNumber()) {
eventClient.setWorkNum(this.eventProperties.getFetchDataWorkerNumber());
}
eventClient.setListenerClass(listenerClass);
this.eventClientMap.put(groupId, eventClient);
}
if (isDefault) {
this.defaultGroup = groupId;
this.defaultEventClient = eventClient;
}
}
private <E extends BaseEvent> void registerHandlerMapping(EventHandlerMapping eventHandlerMapping,
EventHandler<E> handler, Class<E> eventClass, String[] suffixes) {
List<String> groupIds = extractGroupIds(handler);
EventType eventType = getEventType(eventClass);
for (String groupId : groupIds) {
Map<String, List<EventHandler>> handlers =
eventHandlerMapping.getServiceCodeHandlerListMap(groupId, eventType);
if (CollectionUtils.isEmpty(handlers)) {
handlers = new HashMap<>();
if (null == suffixes || suffixes.length == 0) {
handlers.put("", new ArrayList<EventHandler>(Collections.singletonList((EventHandler)handler)));
} else {
for (String suffix : suffixes) {
handlers.put(suffix,
new ArrayList<EventHandler>(Collections.singletonList((EventHandler)handler)));
}
}
eventHandlerMapping.registerHandlers(groupId, eventType, handlers);
continue;
}
if (null == suffixes || suffixes.length == 0) {
List<EventHandler> currentHandlers = handlers.get("");
if (CollectionUtils.isEmpty(currentHandlers)) {
currentHandlers = new ArrayList<>();
}
currentHandlers.add(handler);
handlers.put("", currentHandlers);
continue;
}
for (String suffix : suffixes) {
List<EventHandler> currentHandlers = handlers.get(suffix);
if (CollectionUtils.isEmpty(currentHandlers)) {
currentHandlers = new ArrayList<>();
}
currentHandlers.add(handler);
handlers.put(suffix, currentHandlers);
}
}
}
private void registerCustomHandlerMapping(EventHandlerMapping eventHandlerMapping,
EventHandler<? extends CustomEvent> handler, String topic, String serviceCode) {
List<String> groupIds = extractGroupIds(handler);
for (String groupId : groupIds) {
Map<String, List<CustomEventHandler>> customHandlers =
eventHandlerMapping.getServiceCodeCustomHandlerListMap(groupId, topic);
if (null == customHandlers) {
customHandlers = new HashMap<>();
if (StringUtils.isEmpty(topic)) {
customHandlers.put("",
new ArrayList<CustomEventHandler>(Collections.singletonList((CustomEventHandler)handler)));
} else {
customHandlers.put(serviceCode,
new ArrayList<CustomEventHandler>(Collections.singletonList((CustomEventHandler)handler)));
}
eventHandlerMapping.registerCustomHandlers(groupId, topic, customHandlers);
continue;
}
if (StringUtils.isEmpty(serviceCode)) {
List<CustomEventHandler> list = customHandlers.get("");
if (CollectionUtils.isEmpty(list)) {
list = new ArrayList<>();
}
list.add((CustomEventHandler)handler);
customHandlers.put("", list);
continue;
}
List<CustomEventHandler> currentHandlers = customHandlers.get(serviceCode);
if (CollectionUtils.isEmpty(currentHandlers)) {
currentHandlers = new ArrayList<>();
}
currentHandlers.add((CustomEventHandler)handler);
customHandlers.put(serviceCode, currentHandlers);
}
}
}
@@ -0,0 +1,86 @@
package cn.cloudwalk.event;
import cn.cloudwalk.cwos.client.event.EventClient;
import cn.cloudwalk.cwos.client.event.event.BaseEvent;
import cn.cloudwalk.cwos.client.event.event.CustomEvent;
import cn.cloudwalk.event.handler.CustomEventHandler;
import cn.cloudwalk.event.handler.EventHandler;
import cn.cloudwalk.event.handler.EventHandlerMapping;
import cn.cloudwalk.event.handler.EventHandlerWorker;
import cn.cloudwalk.event.listener.GroupEventListener;
import cn.cloudwalk.event.listener.GroupListnerClassMapping;
import cn.cloudwalk.event.task.EventHandleTask;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
/**
* 事件派发入口:基于 {@link cn.cloudwalk.event.handler.EventHandlerMapping} 选择处理器并委托
* {@link cn.cloudwalk.event.handler.EventHandlerWorker} 执行。
*/
public class CloudwalkEventManager {
private static final Logger LOGGER = LoggerFactory.getLogger(CloudwalkEventManager.class);
private EventHandlerMapping eventHandlerMapping;
private EventHandlerWorker eventHandlerWorker;
private GroupListnerClassMapping groupListnerClassMapping;
private EventClient eventClient;
public void handle(Class<? extends GroupEventListener> eventListnerClass, BaseEvent baseEvent) {
String groupId = this.groupListnerClassMapping.getGroupId(eventListnerClass);
if (baseEvent instanceof CustomEvent) {
Map<String, List<CustomEventHandler>> customByServiceCode = this.eventHandlerMapping
.getServiceCodeCustomHandlerListMap(groupId, ((CustomEvent)baseEvent).getTopic());
if (customByServiceCode == null) {
customByServiceCode = Collections.emptyMap();
}
List<CustomEventHandler> customEventHandlers =
(List<CustomEventHandler>)customByServiceCode.get(baseEvent.getServiceCode());
eventHandle(baseEvent, customEventHandlers);
return;
}
Map<String, List<EventHandler>> handlerByServiceCode =
this.eventHandlerMapping.getServiceCodeHandlerListMap(groupId, baseEvent.getClass());
if (handlerByServiceCode == null) {
handlerByServiceCode = Collections.emptyMap();
}
List<EventHandler> handlerList = (List<EventHandler>)handlerByServiceCode.get(baseEvent.getServiceCode());
eventHandle(baseEvent, handlerList);
}
private <H extends EventHandler> void eventHandle(BaseEvent baseEvent, List<H> handlers) {
if (CollectionUtils.isEmpty(handlers)) {
LOGGER.error("没有相应的事件处理程序");
return;
}
for (EventHandler eventHandler : handlers) {
this.eventHandlerWorker.work(new EventHandleTask(baseEvent, eventHandler));
}
}
public void publish(BaseEvent baseEvent) {
this.eventClient.pubEvent(baseEvent);
}
public void setEventHandlerMapping(EventHandlerMapping eventHandlerMapping) {
this.eventHandlerMapping = eventHandlerMapping;
}
public EventHandlerMapping getEventHandlerMapping() {
return this.eventHandlerMapping;
}
public void setEventHandlerWorker(EventHandlerWorker eventHandlerWorker) {
this.eventHandlerWorker = eventHandlerWorker;
}
public void setEventClient(EventClient eventClient) {
this.eventClient = eventClient;
}
public void setGroupListnerClassMapping(GroupListnerClassMapping groupListnerClassMapping) {
this.groupListnerClassMapping = groupListnerClassMapping;
}
}
@@ -0,0 +1,18 @@
package cn.cloudwalk.event;
import cn.cloudwalk.event.autoconfig.EventConfiguration;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
/**
* 启用云从事件总线自动配置,导入 {@link EventConfiguration} 并绑定 {@code cloudwalk.event.*} 配置项。
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({EventConfiguration.class})
public @interface EnableCloudwalkEvent {}
@@ -0,0 +1,18 @@
package cn.cloudwalk.event.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 声明类型级 Kafka 消费组标识,用于与事件监听/路由配置对齐。
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConsumerGroup {
/** 消费组 ID 列表,与平台侧配置保持一致。 */
String[] groupIds() default {};
}
@@ -0,0 +1,26 @@
package cn.cloudwalk.event.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* 为自定义事件主题提供显式 {@code topic},并通过 {@link EventTopicSuffix} 组合后缀。
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@EventTopicSuffix
public @interface CustomTopic {
/** 逻辑主题名。 */
String topic();
/**
* 与 {@link EventTopicSuffix#suffix()} 互为别名,用于拼接完整主题。
*/
@AliasFor(annotation = EventTopicSuffix.class)
String suffix() default "";
}
@@ -0,0 +1,24 @@
package cn.cloudwalk.event.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* 为主题名追加可配置后缀,避免多环境/多租户下 Kafka Topic 冲突。
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EventTopicSuffix {
/** 与 {@link #suffix()} 互为别名。 */
@AliasFor(attribute = "suffix")
String[] value() default {};
/** 追加在基础主题后的后缀片段。 */
@AliasFor(attribute = "value")
String[] suffix() default {};
}
@@ -0,0 +1,24 @@
package cn.cloudwalk.event.autoconfig;
import cn.cloudwalk.event.CloudwalkEventInitializing;
import cn.cloudwalk.event.CloudwalkEventManager;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 事件模块 Spring 配置:注册 {@link cn.cloudwalk.event.CloudwalkEventManager} 与初始化组件。
*/
@Configuration
@EnableConfigurationProperties({EventProperties.class})
public class EventConfiguration {
@Bean
public CloudwalkEventManager cloudwalkEventManager() {
return new CloudwalkEventManager();
}
@Bean
public CloudwalkEventInitializing cloudwalkEventInitializing(EventProperties eventProperties) {
return new CloudwalkEventInitializing(eventProperties);
}
}
@@ -0,0 +1,93 @@
package cn.cloudwalk.event.autoconfig;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* {@code cloudwalk.event} 前缀下的绑定配置:Kafka 地址、消费组、监听器映射与线程池等。
*/
@ConfigurationProperties(prefix = "cloudwalk.event")
public class EventProperties {
protected static final String CONF_PREFIX = "cloudwalk.event";
private String bootstrapServers;
private String groupId;
private Map<String, String> listenerClass;
private Integer fetchDataWorkerNumber;
private String workerNamePrefix;
private HandlerExecutorConfig handlerExecutorConfig =
new HandlerExecutorConfig(Integer.valueOf(5), Integer.valueOf(10));
public static class HandlerExecutorConfig {
private Integer corePoolSize;
private Integer maximumPoolSize;
public HandlerExecutorConfig(Integer corePoolSize, Integer maximumPoolSize) {
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
}
public Integer getCorePoolSize() {
return this.corePoolSize;
}
public void setCorePoolSize(Integer corePoolSize) {
this.corePoolSize = corePoolSize;
}
public Integer getMaximumPoolSize() {
return this.maximumPoolSize;
}
public void setMaximumPoolSize(Integer maximumPoolSize) {
this.maximumPoolSize = maximumPoolSize;
}
}
public String getBootstrapServers() {
return this.bootstrapServers;
}
public void setBootstrapServers(String bootstrapServers) {
this.bootstrapServers = bootstrapServers;
}
public String getGroupId() {
return this.groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public Map<String, String> getListenerClass() {
return this.listenerClass;
}
public void setListenerClass(Map<String, String> listenerClass) {
this.listenerClass = listenerClass;
}
public Integer getFetchDataWorkerNumber() {
return this.fetchDataWorkerNumber;
}
public void setFetchDataWorkerNumber(Integer fetchDataWorkerNumber) {
this.fetchDataWorkerNumber = fetchDataWorkerNumber;
}
public HandlerExecutorConfig getHandlerExecutorConfig() {
return this.handlerExecutorConfig;
}
public void setHandlerExecutorConfig(HandlerExecutorConfig handlerExecutorConfig) {
this.handlerExecutorConfig = handlerExecutorConfig;
}
public String getWorkerNamePrefix() {
return this.workerNamePrefix;
}
public void setWorkerNamePrefix(String workerNamePrefix) {
this.workerNamePrefix = workerNamePrefix;
}
}
@@ -0,0 +1,35 @@
package cn.cloudwalk.event.client;
import cn.cloudwalk.cwos.client.event.event.EventType;
import cn.cloudwalk.event.handler.EventHandler;
import java.util.List;
public class EventSubscribeHandlers {
private EventType eventType;
private String serviceCode;
private List<EventHandler> eventHandlerList;
public EventType getEventType() {
return this.eventType;
}
public void setEventType(EventType eventType) {
this.eventType = eventType;
}
public String getServiceCode() {
return this.serviceCode;
}
public void setServiceCode(String serviceCode) {
this.serviceCode = serviceCode;
}
public List<EventHandler> getEventHandlerList() {
return this.eventHandlerList;
}
public void setEventHandlerList(List<EventHandler> eventHandlerList) {
this.eventHandlerList = eventHandlerList;
}
}
@@ -0,0 +1,4 @@
package cn.cloudwalk.event.handler;
public interface CustomEventHandler<E extends cn.cloudwalk.cwos.client.event.event.CustomEvent>
extends EventHandler<E> {}
@@ -0,0 +1,5 @@
package cn.cloudwalk.event.handler;
public interface EventHandler<E extends cn.cloudwalk.cwos.client.event.event.BaseEvent> {
void onEvent(E paramE);
}
@@ -0,0 +1,77 @@
package cn.cloudwalk.event.handler;
import cn.cloudwalk.cwos.client.event.event.BaseEvent;
import cn.cloudwalk.cwos.client.event.event.EventType;
import cn.cloudwalk.cwos.client.event.handler.EventListener;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class EventHandlerMapping {
private final Map<String, Map<EventType, Map<String, List<EventHandler>>>> handlerMap = new HashMap<>();
private final Map<String, Map<String, Map<String, List<CustomEventHandler>>>> customHandlerMap = new HashMap<>();
private final Map<Class<? extends EventListener>, String> listenerClassGroupMap = new HashMap<>();
public void registerHandlers(String groupId, EventType eventType, Map<String, List<EventHandler>> handlers) {
Map<EventType, Map<String, List<EventHandler>>> map = this.handlerMap.get(groupId);
if (null == map) {
map = new HashMap<>();
map.put(eventType, handlers);
this.handlerMap.put(groupId, map);
} else {
map.put(eventType, handlers);
}
}
public Map<String, List<EventHandler>> getServiceCodeHandlerListMap(String groupId,
Class<? extends BaseEvent> eventClass) {
for (EventType eventType : EventType.values()) {
BaseEvent prototype = eventType.getEventClass();
if (prototype != null && Objects.equals(eventClass, prototype.getClass())) {
return getServiceCodeHandlerListMap(groupId, eventType);
}
}
throw new IllegalArgumentException("没有找到合适的事件类型");
}
public Map<String, List<EventHandler>> getServiceCodeHandlerListMap(String groupId, EventType eventType) {
Map<EventType, Map<String, List<EventHandler>>> map = this.handlerMap.get(groupId);
if (null != map) {
return map.get(eventType);
}
return null;
}
public Map<String, List<CustomEventHandler>> getServiceCodeCustomHandlerListMap(String groupId, String topic) {
Map<String, Map<String, List<CustomEventHandler>>> map = this.customHandlerMap.get(groupId);
if (null != map) {
return map.get(topic);
}
return null;
}
public void registerCustomHandlers(String groupId, String topic, Map<String, List<CustomEventHandler>> handlers) {
Map<String, Map<String, List<CustomEventHandler>>> map = this.customHandlerMap.get(groupId);
if (null == map) {
map = new HashMap<>();
map.put(topic, handlers);
this.customHandlerMap.put(groupId, map);
} else {
map.put(topic, handlers);
}
}
public Map<String, Map<EventType, Map<String, List<EventHandler>>>> getHandlerMap() {
return Collections.unmodifiableMap(this.handlerMap);
}
public Map<String, Map<String, Map<String, List<CustomEventHandler>>>> getCustomHandlerMap() {
return Collections.unmodifiableMap(this.customHandlerMap);
}
public Map<Class<? extends EventListener>, String> getListenerClassGroupMap() {
return Collections.unmodifiableMap(this.listenerClassGroupMap);
}
}
@@ -0,0 +1,22 @@
package cn.cloudwalk.event.handler;
import cn.cloudwalk.event.task.EventHandleTask;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class EventHandlerWorker {
private ThreadPoolExecutor poolExecutor;
public void setPoolExecutor(ThreadPoolExecutor poolExecutor) {
this.poolExecutor = poolExecutor;
}
public Future<String> work(final EventHandleTask task) {
return this.poolExecutor.submit(new Callable<String>() {
public String call() throws Exception {
return task.start();
}
});
}
}
@@ -0,0 +1,36 @@
package cn.cloudwalk.event.handler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
public class NamedThreadFactory implements ThreadFactory {
private static final String DEFAULT_POOL_NAME = "cloudwalk-common-event";
protected static final AtomicInteger POOL_SEQ = new AtomicInteger(1);
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
private final ThreadGroup threadGroup;
public NamedThreadFactory() {
this("cloudwalk-common-event");
}
public NamedThreadFactory(String namePrefix) {
SecurityManager s = System.getSecurityManager();
this.threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
if (null == namePrefix || "".equals(namePrefix.trim())) {
namePrefix = "cloudwalk-common-event";
}
this.namePrefix = namePrefix + "-" + POOL_SEQ.getAndIncrement() + "-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(this.threadGroup, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != 5) {
t.setPriority(5);
}
return t;
}
}
@@ -0,0 +1,3 @@
package cn.cloudwalk.event.listener;
public class CloudwalkEventListener extends GroupEventListener {}
@@ -0,0 +1,31 @@
package cn.cloudwalk.event.listener;
import cn.cloudwalk.cwos.client.event.event.BaseEvent;
import cn.cloudwalk.cwos.client.event.handler.EventListener;
import cn.cloudwalk.event.CloudwalkEventManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
public abstract class GroupEventListener implements EventListener {
public static ApplicationContext applicationContext;
protected final Logger logger = LoggerFactory.getLogger(getClass());
public void messageListener(BaseEvent baseEvent) throws RuntimeException {
try {
CloudwalkEventManager cloudwalkEventManager =
(CloudwalkEventManager)applicationContext.getBean(CloudwalkEventManager.class);
cloudwalkEventManager.handle(getClass(), baseEvent);
} catch (BeansException e) {
this.logger.error("事件处理出现异常(容器 Bean 解析失败),原因:", e);
throw new RuntimeException(e);
} catch (RuntimeException e) {
this.logger.error("事件处理出现异常,原因:", e);
throw e;
} catch (Exception e) {
this.logger.error("事件处理出现异常,原因:", e);
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,26 @@
package cn.cloudwalk.event.listener;
import cn.cloudwalk.cwos.client.event.handler.EventListener;
import java.util.HashMap;
import java.util.Map;
/**
* 监听类型与 Kafka 消费组 ID 的注册表。
* <p>
* 类名中的 {@code Listner} 为历史产物,与既有字节码/配置引用保持一致,请勿随意重命名。
*/
public class GroupListnerClassMapping {
private final Map<Class<? extends EventListener>, String> mapping = new HashMap<>();
public void register(Class<? extends EventListener> eventListnerClass, String groupId) {
if (this.mapping.containsKey(eventListnerClass)) {
throw new IllegalArgumentException(
String.format("eventListnerClass[%s]已经存在,不允许重复", new Object[] {eventListnerClass.toString()}));
}
this.mapping.put(eventListnerClass, groupId);
}
public String getGroupId(Class<? extends EventListener> eventListnerClass) {
return this.mapping.get(eventListnerClass);
}
}
@@ -0,0 +1,7 @@
/**
* 云从消息总线(Kafka)封装:监听器注册、事件处理链、与 CWOS SDK 的集成。
* <p>
* 入口:在 Spring Boot 启动类或配置类上使用 {@link cn.cloudwalk.event.EnableCloudwalkEvent} 通过 {@code cloudwalk.event.*} 配置项(参见
* {@link cn.cloudwalk.event.autoconfig.EventProperties})完成连接与线程池等参数绑定。
*/
package cn.cloudwalk.event;
@@ -0,0 +1,35 @@
package cn.cloudwalk.event.task;
import cn.cloudwalk.cwos.client.event.event.BaseEvent;
import cn.cloudwalk.event.handler.EventHandler;
public class EventHandleTask<E extends BaseEvent> {
private E event;
private EventHandler<E> handler;
public EventHandleTask(E event, EventHandler<E> handler) {
this.event = event;
this.handler = handler;
}
public String start() {
this.handler.onEvent(this.event);
return this.event.getMessageId();
}
public E getEvent() {
return this.event;
}
public void setEvent(E event) {
this.event = event;
}
public EventHandler<E> getHandler() {
return this.handler;
}
public void setHandler(EventHandler<E> handler) {
this.handler = handler;
}
}
@@ -0,0 +1,6 @@
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: hechunjie
Created-By: Apache Maven 3.6.0
Build-Jdk: 1.8.0_191
@@ -0,0 +1,58 @@
{
"hints": [],
"groups": [
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties",
"name": "cloudwalk.event",
"type": "cn.cloudwalk.event.autoconfig.EventProperties"
},
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties",
"name": "cloudwalk.event.handler-executor-config",
"sourceMethod": "getHandlerExecutorConfig()",
"type": "cn.cloudwalk.event.autoconfig.EventProperties$HandlerExecutorConfig"
}
],
"properties": [
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties",
"name": "cloudwalk.event.bootstrap-servers",
"description": "服务器地址",
"type": "java.lang.String"
},
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties",
"name": "cloudwalk.event.fetch-data-worker-number",
"description": "获取数据的工作线程数",
"type": "java.lang.Integer"
},
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties",
"name": "cloudwalk.event.group-id",
"description": "组ID",
"type": "java.lang.String"
},
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties$HandlerExecutorConfig",
"name": "cloudwalk.event.handler-executor-config.core-pool-size",
"type": "java.lang.Integer"
},
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties$HandlerExecutorConfig",
"name": "cloudwalk.event.handler-executor-config.maximum-pool-size",
"type": "java.lang.Integer"
},
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties",
"name": "cloudwalk.event.listener-class",
"description": "组与EventListener对应关系",
"type": "java.util.Map<java.lang.String,java.lang.String>"
},
{
"sourceType": "cn.cloudwalk.event.autoconfig.EventProperties",
"name": "cloudwalk.event.worker-name-prefix",
"description": "工作线程名字前缀",
"type": "java.lang.String"
}
]
}