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;
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值