mirror of https://gitee.com/stylefeng/roses
Merge branch 'dev-8.0-event' into dev-8.0
commit
6d615425d5
|
@ -6,7 +6,6 @@ import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum;
|
|||
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
|
||||
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
|
@ -42,20 +41,6 @@ public class CommonLoginUserUtil {
|
|||
return authToken;
|
||||
}
|
||||
|
||||
// 3. 从cookie中获取token
|
||||
String sessionCookieName = AuthConfigExpander.getSessionCookieName();
|
||||
Cookie[] cookies = request.getCookies();
|
||||
if (cookies != null && cookies.length > 0) {
|
||||
for (Cookie cookie : cookies) {
|
||||
|
||||
// 如果cookie有对应的值,并且不为空
|
||||
if (sessionCookieName.equals(cookie.getName())
|
||||
&& StrUtil.isNotBlank(cookie.getValue())) {
|
||||
return cookie.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取不到token,直接告诉用户
|
||||
throw new AuthException(AuthExceptionEnum.TOKEN_GET_ERROR);
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
# 业务事件
|
|
@ -0,0 +1 @@
|
|||
# api模块,存放接口、常量、异常、枚举等模块规则相关类
|
|
@ -0,0 +1,37 @@
|
|||
<?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.stylefeng.roses</groupId>
|
||||
<artifactId>kernel-d-event</artifactId>
|
||||
<version>8.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>event-api</artifactId>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--config模块的api-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>config-api</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--参数校验模块-->
|
||||
<!--用在控制器,参数校验-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>validator-api</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.event.api.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 业务事件监听的注解
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:12
|
||||
*/
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
public @interface BusinessListener {
|
||||
|
||||
/**
|
||||
* 监听业务的编码或者标识
|
||||
*/
|
||||
String businessCode() default "";
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.event.api.constants;
|
||||
|
||||
/**
|
||||
* 业务事件业务
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2023-07-14 15:05:54
|
||||
*/
|
||||
public interface EventConstants {
|
||||
|
||||
/**
|
||||
* 业务事件模块名称
|
||||
*/
|
||||
String EVENT_MODULE_NAME = "kernel-s-event";
|
||||
|
||||
/**
|
||||
* 异常枚举的步进值
|
||||
*/
|
||||
String EVENT_EXCEPTION_STEP_CODE = "99";
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package cn.stylefeng.roses.kernel.event.api.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 一个示例
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2023-07-14 15:05:54
|
||||
*/
|
||||
@Getter
|
||||
public enum DemoEnum {
|
||||
|
||||
/**
|
||||
* markdown格式
|
||||
*/
|
||||
MARKDOWN(1, "markdown格式"),
|
||||
|
||||
/**
|
||||
* 富文本格式
|
||||
*/
|
||||
TEXT(2, "富文本格式");
|
||||
|
||||
private final Integer code;
|
||||
|
||||
private final String message;
|
||||
|
||||
DemoEnum(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.event.api.exception;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.event.api.constants.EventConstants;
|
||||
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
|
||||
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
|
||||
|
||||
/**
|
||||
* 业务事件异常
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2023-07-14 15:05:54
|
||||
*/
|
||||
public class EventException extends ServiceException {
|
||||
|
||||
public EventException(AbstractExceptionEnum exception, Object... params) {
|
||||
super(EventConstants.EVENT_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
|
||||
}
|
||||
|
||||
public EventException(AbstractExceptionEnum exception) {
|
||||
super(EventConstants.EVENT_MODULE_NAME, exception);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.event.api.exception.enums;
|
||||
|
||||
import cn.stylefeng.roses.kernel.event.api.constants.EventConstants;
|
||||
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
|
||||
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 业务事件的异常枚举
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2023-07-14 15:05:54
|
||||
*/
|
||||
@Getter
|
||||
public enum EventExceptionEnum implements AbstractExceptionEnum {
|
||||
|
||||
/**
|
||||
* 查询不到对应业务事件
|
||||
*/
|
||||
CANT_FIND_EVENT(RuleConstants.BUSINESS_ERROR_TYPE_CODE + EventConstants.EVENT_EXCEPTION_STEP_CODE + "01", "查询不到对应业务事件,具体信息:{}");
|
||||
|
||||
/**
|
||||
* 错误编码
|
||||
*/
|
||||
private final String errorCode;
|
||||
|
||||
/**
|
||||
* 提示用户信息
|
||||
*/
|
||||
private final String userTip;
|
||||
|
||||
EventExceptionEnum(String errorCode, String userTip) {
|
||||
this.errorCode = errorCode;
|
||||
this.userTip = userTip;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
# SDK模块,存放一些本模块通用的工具处理类等
|
|
@ -0,0 +1,29 @@
|
|||
<?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.stylefeng.roses</groupId>
|
||||
<artifactId>kernel-d-event</artifactId>
|
||||
<version>8.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>event-sdk</artifactId>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--业务事件api-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>event-api</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.event.sdk;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.event.api.annotation.BusinessListener;
|
||||
import cn.stylefeng.roses.kernel.event.sdk.container.EventContainer;
|
||||
import cn.stylefeng.roses.kernel.event.sdk.pojo.BusinessListenerDetail;
|
||||
import cn.stylefeng.roses.kernel.rule.util.AopTargetUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 扫描所有的业务监听事件,最终汇总到Event容器中
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:09
|
||||
*/
|
||||
@Slf4j
|
||||
public class EventAnnotationScanner implements BeanPostProcessor {
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
|
||||
// 如果controller是代理对象,则需要获取原始类的信息
|
||||
Object aopTarget = AopTargetUtils.getTarget(bean);
|
||||
|
||||
if (aopTarget == null) {
|
||||
aopTarget = bean;
|
||||
}
|
||||
|
||||
Class<?> clazz = aopTarget.getClass();
|
||||
|
||||
// 判断类中是否有@BusinessListener注解
|
||||
this.doScan(clazz, beanName);
|
||||
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描整个类中包含的所有@BusinessListener注解
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:24
|
||||
*/
|
||||
private void doScan(Class<?> clazz, String beanName) {
|
||||
Method[] declaredMethods = clazz.getDeclaredMethods();
|
||||
for (Method declaredMethod : declaredMethods) {
|
||||
BusinessListener businessListener = declaredMethod.getAnnotation(BusinessListener.class);
|
||||
|
||||
log.debug("扫描到业务事件监听: " + businessListener);
|
||||
|
||||
if (businessListener != null) {
|
||||
createEventDetail(beanName, declaredMethod, businessListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 讲扫描到的注解信息的调用信息,封装到BusinessListenerDetail
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:27
|
||||
*/
|
||||
private void createEventDetail(String beanName, Method method, BusinessListener businessListener) {
|
||||
|
||||
// 获取注解上的业务编码标识
|
||||
String businessCode = businessListener.businessCode();
|
||||
|
||||
// 从容器中获取本业务标识是否创建
|
||||
List<BusinessListenerDetail> listener = EventContainer.getListener(businessCode);
|
||||
if (ObjectUtil.isEmpty(listener)) {
|
||||
listener = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
// 创建当前方法的调用信息
|
||||
BusinessListenerDetail businessListenerDetail = new BusinessListenerDetail();
|
||||
businessListenerDetail.setBeanName(beanName);
|
||||
businessListenerDetail.setListenerMethod(method);
|
||||
|
||||
// 获取方法参数信息
|
||||
int parameterCount = method.getParameterCount();
|
||||
if (parameterCount == 0) {
|
||||
businessListenerDetail.setParameterClassType(null);
|
||||
} else {
|
||||
businessListenerDetail.setParameterClassType(method.getParameterTypes()[0]);
|
||||
}
|
||||
|
||||
listener.add(businessListenerDetail);
|
||||
|
||||
// 放到事件容器中
|
||||
EventContainer.addListenerList(businessCode, listener);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package cn.stylefeng.roses.kernel.event.sdk.container;
|
||||
|
||||
import cn.stylefeng.roses.kernel.event.sdk.pojo.BusinessListenerDetail;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 事件的容器
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:08
|
||||
*/
|
||||
public class EventContainer {
|
||||
|
||||
/**
|
||||
* key是业务的标识,例如:ORG_ADD
|
||||
* <p>
|
||||
* value是对应监听这个事件的所有方法信息
|
||||
*/
|
||||
private static final Map<String, List<BusinessListenerDetail>> CONTEXT = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 获取业务对应的所有监听器
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:21
|
||||
*/
|
||||
public static List<BusinessListenerDetail> getListener(String businessCode) {
|
||||
return CONTEXT.get(businessCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到容器中业务监听器信息
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:22
|
||||
*/
|
||||
public static void addListenerList(String businessCode, List<BusinessListenerDetail> businessListenerItemList) {
|
||||
CONTEXT.put(businessCode, businessListenerItemList);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package cn.stylefeng.roses.kernel.event.sdk.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 业务事件监听的详情封装
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:18
|
||||
*/
|
||||
@Data
|
||||
public class BusinessListenerDetail {
|
||||
|
||||
/**
|
||||
* Spring的Bean名称
|
||||
*/
|
||||
private String beanName;
|
||||
|
||||
/**
|
||||
* 业务监听器对应注解的类方法
|
||||
*/
|
||||
private Method listenerMethod;
|
||||
|
||||
/**
|
||||
* 方法参数的类型,如果方法有多个参数,这里只获取第一个参数的类型
|
||||
* <p>
|
||||
* 如果方法没参数,则这里为null
|
||||
*/
|
||||
private Class<?> parameterClassType;
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package cn.stylefeng.roses.kernel.event.sdk.publish;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.stylefeng.roses.kernel.event.sdk.container.EventContainer;
|
||||
import cn.stylefeng.roses.kernel.event.sdk.pojo.BusinessListenerDetail;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 业务事件发布工具
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 16:21
|
||||
*/
|
||||
public class BusinessEventPublisher {
|
||||
|
||||
/**
|
||||
* 发布一个业务事件
|
||||
* <p>
|
||||
* 如果有方法上加了@BusinessListener,则会自动回调那些方法
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 15:37
|
||||
*/
|
||||
public static <T> void publishEvent(String businessCode, T businessObject) {
|
||||
|
||||
// 获取该业务是否有监听器
|
||||
List<BusinessListenerDetail> listener = EventContainer.getListener(businessCode);
|
||||
|
||||
// 没有监听器则直接返回
|
||||
if (ObjectUtil.isEmpty(listener)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 依次调用监听器方法,进行回调调用
|
||||
for (BusinessListenerDetail businessListenerDetail : listener) {
|
||||
|
||||
// 监听器的Spring Bean
|
||||
String beanName = businessListenerDetail.getBeanName();
|
||||
Object bean = SpringUtil.getBean(beanName);
|
||||
|
||||
// 监听器的具体方法
|
||||
Method listenerMethod = businessListenerDetail.getListenerMethod();
|
||||
Class<?> parameterClassType = businessListenerDetail.getParameterClassType();
|
||||
|
||||
// 如果发布事件的时候参数是空的,则直接调用无参的方法
|
||||
if (ObjectUtil.isEmpty(businessObject)) {
|
||||
|
||||
// 如果方法的参数数量为0,则直接调用,如果不为0,则跳过执行下个
|
||||
if (parameterClassType == null) {
|
||||
try {
|
||||
listenerMethod.invoke(businessObject);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 如果发布事件的时候,包含参数,则判断method的第一个参数是否和businessObject的class一样,不一样则不调用
|
||||
else {
|
||||
if (parameterClassType != null && parameterClassType.equals(businessObject.getClass())) {
|
||||
try {
|
||||
listenerMethod.invoke(bean, businessObject);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
# Spring Boot自动装配
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?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.stylefeng.roses</groupId>
|
||||
<artifactId>kernel-d-event</artifactId>
|
||||
<version>8.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>event-spring-boot-starter</artifactId>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--业务事件业务模块-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>event-sdk</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.event.starter;
|
||||
|
||||
import cn.stylefeng.roses.kernel.event.sdk.EventAnnotationScanner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 业务事件自动装配
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2023-07-14 15:05:54
|
||||
*/
|
||||
@Configuration
|
||||
public class EventAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 创建事件的扫描
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 16:00
|
||||
*/
|
||||
@Bean
|
||||
public EventAnnotationScanner eventAnnotationScanner() {
|
||||
return new EventAnnotationScanner();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.stylefeng.roses.kernel.event.starter.EventAutoConfiguration
|
|
@ -0,0 +1,35 @@
|
|||
<?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.stylefeng.roses</groupId>
|
||||
<artifactId>roses-kernel</artifactId>
|
||||
<version>8.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>kernel-d-event</artifactId>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>event-api</module>
|
||||
<module>event-sdk</module>
|
||||
<module>event-spring-boot-starter</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- 开发规则 -->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>kernel-a-rule</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -17,6 +17,13 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<!-- 事件模块 -->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>event-spring-boot-starter</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--基础核心业务api-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
|
|
|
@ -37,4 +37,9 @@ public interface OrgConstants {
|
|||
*/
|
||||
String NONE_PARENT_ORG = "无上级机构";
|
||||
|
||||
/**
|
||||
* 添加组织机构的事件监听
|
||||
*/
|
||||
String ADD_ORG_EVENT = "ADD_ORG_EVENT";
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.org.listener;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.event.api.annotation.BusinessListener;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.org.constants.OrgConstants;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.org.entity.HrOrganization;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 添加组织机构的事件监听器
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 14:19
|
||||
*/
|
||||
@Service
|
||||
public class OrgOperateListener {
|
||||
|
||||
@Resource(name = "sysOrgSubFlagCache")
|
||||
private CacheOperatorApi<Boolean> sysOrgSubFlagCache;
|
||||
|
||||
/**
|
||||
* 监听新增组织机构,删除相关的缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 16:22
|
||||
*/
|
||||
@BusinessListener(businessCode = OrgConstants.ADD_ORG_EVENT)
|
||||
public void addOrgCallback(HrOrganization businessObject) {
|
||||
|
||||
if (ObjectUtil.isNotEmpty(businessObject.getOrgId())) {
|
||||
sysOrgSubFlagCache.remove(String.valueOf(businessObject.getOrgId()));
|
||||
}
|
||||
|
||||
if (ObjectUtil.isNotEmpty(businessObject.getOrgParentId())) {
|
||||
sysOrgSubFlagCache.remove(String.valueOf(businessObject.getOrgParentId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -12,6 +12,7 @@ import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
|
|||
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
|
||||
import cn.stylefeng.roses.kernel.event.sdk.publish.BusinessEventPublisher;
|
||||
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
|
||||
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
|
||||
import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory;
|
||||
|
@ -76,6 +77,9 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
|
|||
OrganizationFactory.fillParentIds(hrOrganization);
|
||||
|
||||
this.save(hrOrganization);
|
||||
|
||||
// 发布一个新增组织机构的事件
|
||||
BusinessEventPublisher.publishEvent(OrgConstants.ADD_ORG_EVENT, hrOrganization);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue