地质所 沉降监测网建设项目
chenhuan
2024-05-16 98b1fc47a5c40422a7289c9ac10960f26f742d93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.javaweb.applicationEvent;
 
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.javaweb.common.utils.spring.SpringUtils;
import com.javaweb.framework.util.ShiroUtils;
import com.javaweb.system.domain.EventLog;
import com.javaweb.system.domain.SysUser;
import com.javaweb.system.service.IEventLogService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.annotation.PostConstruct;
import java.util.*;
 
/**
 * 事件管理器
 * Created by zengchao on 2017-03-09.
 */
public class ApplicationEventManager {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private Map<ApplicationEventDefined, List<IApplicationEvent>> eventMap = new HashMap<>();//按照系事件分组
 
    List<IApplicationEvent> applicationEventList;//所有实现系统事件类
 
    IEventLogService eventLogService;
 
    public void setApplicationEventList(List<IApplicationEvent> applicationEventList) {
        this.applicationEventList = applicationEventList;
    }
 
    public void setEventLogService(IEventLogService eventLogService) {
        this.eventLogService = eventLogService;
    }
 
    /**
     * 绑定事件
     *
     * @param applicationEventDefineds 系统自定义事件数组
     * @param applicationEvent     事件处理程序
     */
    public synchronized void bind(ApplicationEventDefined[] applicationEventDefineds, IApplicationEvent applicationEvent) {
        for(ApplicationEventDefined applicationEventDefined:applicationEventDefineds){
            List<IApplicationEvent> eventList = eventMap.get(applicationEventDefined);
            if (eventList == null) {
                eventList = new ArrayList<>();
            }
            eventList.add(applicationEvent);
            eventMap.put(applicationEventDefined, eventList);
 
            logger.debug("Bind Event : " + applicationEventDefined.name());
        }
 
    }
 
    /**
     * 解绑事件
     *
     * @param applicationEventDefined 事件名称
     * @param applicationEvent     事件处理程序
     */
    public synchronized void unbind(ApplicationEventDefined applicationEventDefined, IApplicationEvent applicationEvent) {
        List<IApplicationEvent> eventList = eventMap.get(applicationEventDefined);
        if (eventList == null) {
            return;
        }
        eventList.remove(applicationEvent);
        logger.debug("UnBind Event : " + applicationEventDefined.name());
    }
 
    /**
     * 触发事件
     *
     * @param applicationEventDefined 事件名称
     * @param source    来源
     * @param params    参数
     */
    public synchronized void trigger(ApplicationEventDefined applicationEventDefined, Object source, Object params) {
        List<IApplicationEvent> eventList = eventMap.get(applicationEventDefined);
        if (eventList == null) {
            return;
        }
        for (IApplicationEvent event : eventList) {
            try {
                event.onTrigger(source, params);
                saveLog(applicationEventDefined,source,params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void saveLog(ApplicationEventDefined applicationEventDefined, Object source, Object params){
            String clsName="";
            if(source instanceof String){
                clsName=source.toString();
            }else{
                clsName=source.getClass().getTypeName();
            }
            JSONObject jsonObject=JSONObject.parseObject(JSON.toJSONString(params));
            String orgid=(String)jsonObject.get("orgid");
            EventLog log= new EventLog();
            SysUser user= ShiroUtils.getSysUser();
            log.setUserId(user.getUserId().toString());
            log.setUserName(user.getUserName());
            log.setEventCode(applicationEventDefined.getValue());
            log.setEventName(applicationEventDefined.getDescription());
            log.setCreateTime(new Date());
            log.setSource(clsName);
            log.setDatas(JSON.toJSONString(params));
            eventLogService.insertEventLog(log);
    }
    /**
     * 初始化
     * 扫描系统中注解形式的事件
     */
    @PostConstruct
    public void init() {
        if(CollectionUtils.isNotEmpty(applicationEventList)){
            for(IApplicationEvent event:applicationEventList){
                 Class cls =   event.getClass();
                Object instance = null;
                if(!cls.getName().contains("proxy")){
                    instance= SpringUtils.getBean(cls);
                    if (instance instanceof IApplicationEvent) {
                        ApplicationEvent eventAnno =(ApplicationEvent) cls.getAnnotation(ApplicationEvent.class);
                        this.bind(eventAnno.value(), (IApplicationEvent) instance);
                    }
                }
            }
            logger.info("系统事件总计注册数量:" + this.eventMap.size());
            //遍历自定义系统内置事件列出各个事件注册数量
            ApplicationEventDefined[] applicationEventDefineds= ApplicationEventDefined.values();
            for(ApplicationEventDefined applicationEventDefined:applicationEventDefineds){
                List<IApplicationEvent> eventList = eventMap.get(applicationEventDefined);
                if(CollectionUtils.isNotEmpty(eventList)){
                    logger.info("系统事件"+applicationEventDefined.getValue()+"["+applicationEventDefined.getDescription()+"]注册数量:" + eventList.size());
                }else{
                    logger.info("系统事件"+applicationEventDefined.getValue()+"["+applicationEventDefined.getDescription()+"]注册数量:0");
                }
            }
 
        }
        /*Set<Class<?>> classes = ClassKit.getClassesByAnnotation("com.jrelax", ApplicationEvent.class);
        for (Class<?> cls : classes) {
            Object instance = null;
            Map<String, ?> beanMap = ApplicationContextHelper.getInstance().getBeansOfType(cls);
            if (beanMap.size() == 0) {
                instance = ApplicationContextHelper.getInstance().createBean(cls);
            } else {
                for (Map.Entry<String, ?> map : beanMap.entrySet()) {
                    instance = map.getValue();
 
                    if (instance != null) break;
                }
            }
            if (instance instanceof IApplicationEvent) {
                ApplicationEvent event = cls.getAnnotation(ApplicationEvent.class);
 
                this.bind(event.value(), (IApplicationEvent) instance);
            }
        }
 
        logger.info("系统事件注册数量:" + this.eventMap.size());*/
    }
}