leutu
2024-06-03 3ef35e6cd16bbfa206b26bb3271eac40ad020bcb
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
package com.fastbee.base.core;
 
import com.fastbee.base.core.annotation.Async;
import com.fastbee.base.core.annotation.AsyncBatch;
import com.fastbee.base.core.annotation.PakMapping;
import com.fastbee.base.core.hanler.AsyncBatchHandler;
import com.fastbee.base.core.hanler.BaseHandler;
import com.fastbee.base.core.hanler.SyncHandler;
 
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 消息处理映射
 *
 * @author bill
 */
public abstract class AbstractHandlerMapping implements HandlerMapping {
 
    private final Map<Object, BaseHandler> handlerMap = new HashMap<>(64);
 
    /**
     * 将node中被@Column标记的方法注册到映射表
     */
    protected synchronized void registerHandlers(Object bean) {
        Class<?> beanClass = bean.getClass();
        Method[] methods = beanClass.getDeclaredMethods();
 
        for (Method method : methods) {
            PakMapping annotation = method.getAnnotation(PakMapping.class);
            if (annotation != null) {
 
                String desc = annotation.desc();
                int[] types = annotation.types();
 
                AsyncBatch asyncBatch = method.getAnnotation(AsyncBatch.class);
                BaseHandler baseHandler;
                // 异步处理
                if (asyncBatch != null) {
                    baseHandler = new AsyncBatchHandler(bean, method, desc, asyncBatch.poolSize(), asyncBatch.maxMessageSize(), asyncBatch.maxWaitTime());
                } else {
                    baseHandler = new SyncHandler(bean, method, desc, method.isAnnotationPresent(Async.class));
                }
 
                for (int type : types) {
                    handlerMap.put(type, baseHandler);
                }
 
            }
        }
    }
 
    /**
     * 根据消息类型获取handler
     */
    @Override
    public BaseHandler getHandler(int messageId) {
        return handlerMap.get(messageId);
    }
}