根据模块,命令 注解来执行特定业务逻辑

2019-05-21  本文已影响0人  凯睿看世界

1:定义注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SocketModule {
    
    /**
     * 请求的模块号
     * @return
     */
    short module();
}


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SocketCommand {
    
    /**
     * 请求的命令号
     * @return
     */
    short cmd();

}

2:定义 命令执行器

/**
 * 命令执行器
 * @author -琴兽-
 *
 */
public class Invoker {
    
    /**
     * 方法
     */
    private Method method;
    
    /**
     * 目标对象
     */
    private Object target;
    
    public static Invoker valueOf(Method method, Object target){
        Invoker invoker = new Invoker();
        invoker.setMethod(method);
        invoker.setTarget(target);
        return invoker;
    }
    
    /**
     * 执行
     * @param paramValues
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     */
    public Object invoke(Object... paramValues){
        try {
            return method.invoke(target, paramValues);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }
}

3:命令执行器管理者

/**
 * 命令执行器管理者
 * @author -琴兽-
 *
 */
public class InvokerHoler {
    
    /**命令调用器*/
    private static Map<Short, Map<Short, Invoker>> invokers = new HashMap<>();
    
    /**
     * 添加命令调用
     * @param module
     * @param cmd
     * @param invoker
     */
    public static void addInvoker(short module, short cmd, Invoker invoker){
        Map<Short, Invoker> map = invokers.get(module);
        if(map == null){
            map = new HashMap<>();
            invokers.put(module, map);
        }
        map.put(cmd, invoker);
    }
    
    
    /**
     * 获取命令调用
     * @param module
     * @param cmd
     * @param invoker
     */
    public static Invoker getInvoker(short module, short cmd){
        Map<Short, Invoker> map = invokers.get(module);
        if(map != null){
            return map.get(cmd);
        }
        return null;
    }

}

4:handler扫描器 把实现接口的有注解的业务注入到执行器管理者中

/**
 * handler扫描器
 * @author -琴兽-
 *
 */
@Component
public class HandlerScaner implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        Class<? extends Object> clazz = bean.getClass();
        
        Class<?>[] interfaces = clazz.getInterfaces();
        
        if(interfaces != null && interfaces.length > 0){
            //扫描类的所有接口父类
            for (Class<?> interFace : interfaces) {
                //判断是否为handler接口类
                SocketModule socketModule = interFace.getAnnotation(SocketModule.class);
                if (socketModule == null) {
                    continue;
                }
                
                //找出命令方法
                Method[] methods = interFace.getMethods();
                if(methods != null && methods.length > 0){
                    for(Method method : methods){
                        SocketCommand socketCommand = method.getAnnotation(SocketCommand.class);
                        if(socketCommand == null){
                            continue;
                        }
                        
                        final short module = socketModule.module();
                        final short cmd = socketCommand.cmd();
                        
                        if(InvokerHoler.getInvoker(module, cmd) == null){
                            InvokerHoler.addInvoker(module, cmd, Invoker.valueOf(method, bean));
                        }else{
                            System.out.println("重复命令:"+"module:"+module +" "+"cmd:" + cmd);
                        }
                    }
                }
                
            }
        }
        return bean;
    }

}

5:根据module , cmd 参数调用执行器执行业务逻辑

/**
 * 消息处理
 * @param session
 * @param request
 */
private void handlerMessage(Session session, Request request){
    
    Response response = new Response(request);
    
    System.out.println("module:"+request.getModule() + "   " + "cmd:" + request.getCmd());
    
    //获取命令执行器
    Invoker invoker = InvokerHoler.getInvoker(request.getModule(), request.getCmd());
}
上一篇下一篇

猜你喜欢

热点阅读