hessian(3)-hessian 远程调用接口实现类

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;


public class InvokeServer implements MapRemoteInvoke {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();


    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public Map<?, ?> remoteInvoke(Map<String, Object> params) {
        logger.info("hessian Invoke with params:{}",new Object[]{params});
        long start = System.currentTimeMillis();
        Map resultHash = new HashMap();
        String methodName = params.get("methodName").toString();
        String beanId = params.get("beanId").toString();
        StringBuilder sbr = new StringBuilder();
        sbr.append("--------------invoke server with bean(").append(beanId).append(") use method(").append(methodName)
                .append(")");


        try {
            // 获取实例对象
            Object obj = wac.getBean(beanId);
            // 获取实例Class对象
            Class clas = obj.getClass();
            // 获取方法对象(默认方法参数都为Map)
            Method method = clas.getDeclaredMethod(methodName, Map.class);
            // 将私有的方法变为公有方法
            method.setAccessible(true);
            // 调用方法设置值
            Object result = method.invoke(obj, params);
            if (result instanceof Map) {
                resultHash = (Map) result;
            } else {
                resultHash.put("result", result);
            }
        } catch (SecurityException e) {
            logger.error("SecurityException",e);
        } catch (NoSuchMethodException e) {
            logger.error("handler method not isexist",e);
        } catch (IllegalArgumentException e) {
            logger.error("IllegalArgumentException",e);
        } catch (IllegalAccessException e) {
            logger.error("IllegalAccessException",e);
        } catch (InvocationTargetException e) {
            logger.error("InvocationTargetException",e);
        }
        sbr.append("used(ms) ").append(System.currentTimeMillis() - start);
        logger.info(sbr.toString());
        return resultHash;
    }

}

猜你喜欢

转载自blog.csdn.net/xinyu100100/article/details/80629448
今日推荐