Search in sources :

Example 1 with MethodProxy

use of cn.moyada.dubbo.faker.core.model.MethodProxy in project dubbo-faker by moyada.

the class MethodHandleProxy method getProxy.

public MethodProxy getProxy(MethodInvokeDO invokeInfo) {
    // , int poolSize) {
    MethodProxy proxy;
    log.info("init method proxy info.");
    // 检测是否已存在
    Integer id = invokeInfo.getId();
    SoftReference<MethodProxy> ref = proxyMap.get(id);
    if (null != ref) {
        proxy = ref.get();
        if (null != proxy) {
            // && proxy.getService().length == poolSize) {
            return proxy;
        }
    }
    // 获取参数类型
    Class<?>[] paramTypes;
    String[] argsType = invokeInfo.getParamType().split(",");
    int length = argsType.length;
    if (0 == length) {
        paramTypes = new Class[0];
    } else {
        paramTypes = new Class[length];
        for (int index = 0; index < length; index++) {
            try {
                paramTypes[index] = ReflectUtil.getClassType(argsType[index]);
            } catch (ClassNotFoundException e) {
                throw new InitializeInvokerException("获取参数类型失败: " + argsType[index]);
            }
        }
    }
    // 获取方法具柄
    MethodHandle methodHandle = handle.fetchHandleInfo(invokeInfo.getClassName(), invokeInfo.getMethodName(), invokeInfo.getReturnType(), paramTypes);
    // 获取接口
    Class classType;
    try {
        classType = ReflectUtil.getClassType(invokeInfo.getClassName());
    } catch (ClassNotFoundException e) {
        throw new InitializeInvokerException("获取结果失败: " + invokeInfo.getClassName());
    }
    // 获取接口实例
    Object serviceAssembly;
    // Object[] serviceAssembly = new Object[poolSize];
    try {
        serviceAssembly = beanHolder.getBean(classType);
    // for (int index = 0; index < poolSize; index++) {
    // serviceAssembly[index] = beanHelper.getBean(classType);
    // }
    } catch (BeansException e) {
        throw new RpcException("获取接口实例失败: " + invokeInfo.getClassName() + ".", e);
    }
    // for (Object service : serviceAssembly) {
    try {
        methodHandle.invoke(serviceAssembly, null);
    } catch (Throwable throwable) {
    }
    // }
    proxy = new MethodProxy();
    proxy.setParamTypes(paramTypes);
    proxy.setMethodHandle(methodHandle);
    proxy.setService(serviceAssembly);
    // 缓存调用代理
    ref = new SoftReference<>(proxy);
    proxyMap.put(id, ref);
    return proxy;
}
Also used : InitializeInvokerException(cn.moyada.dubbo.faker.core.exception.InitializeInvokerException) RpcException(com.alibaba.dubbo.rpc.RpcException) MethodProxy(cn.moyada.dubbo.faker.core.model.MethodProxy) MethodHandle(java.lang.invoke.MethodHandle) BeansException(org.springframework.beans.BeansException)

Example 2 with MethodProxy

use of cn.moyada.dubbo.faker.core.model.MethodProxy in project dubbo-faker by moyada.

the class FakerRequest method request.

public String request(int invokeId, String invokeExpression, int poolSize, int qps, int questNum, boolean random, boolean saveResult, String resultParam) {
    MethodInvokeDO invokeInfo = fakerManager.getInvokeInfo(invokeId);
    // , poolSize);
    MethodProxy proxy = methodHandleProxy.getProxy(invokeInfo);
    Object[] values = JsonUtil.toArray(invokeExpression, Object[].class);
    Class<?>[] paramTypes = proxy.getParamTypes();
    if (null == values || paramTypes.length != values.length) {
        throw InitializeInvokerException.paramError;
    }
    // 生成调用报告序号
    String fakerId = UUIDUtil.getUUID();
    // 参数提供器
    ParamProvider paramProvider = new ParamProvider(fakerManager, values, paramTypes, random);
    // 创建调用结果监听器
    CompletedListener listener = new LoggingListener(fakerId, invokeId, fakerManager, saveResult, resultParam);
    // 创建方法调用器
    AbstractInvoker invoke = new AsyncInvoker(proxy.getMethodHandle(), proxy.getService(), listener, poolSize);
    int timeout = (3600 / qps) - (10 >= qps ? 0 : 20);
    // 发起调用
    if (timeout > 50) {
        log.info("start timeout faker invoke: " + fakerId);
        for (int index = 0; index < questNum; index++) {
            invoke.invoke(paramProvider.fetchNextParam());
            LockSupport.parkNanos(timeout * 1000);
        }
    } else {
        log.info("start faker invoke: " + fakerId);
        for (int index = 0; index < questNum; index++) {
            invoke.invoke(paramProvider.fetchNextParam());
        }
    }
    invoke.destroy();
    log.info("faker invoke done: " + fakerId);
    listener.shutdownDelay();
    log.info("logging shutdown: " + fakerId);
    return "请求结果序号:" + fakerId;
}
Also used : AsyncInvoker(cn.moyada.dubbo.faker.core.invoke.AsyncInvoker) LoggingListener(cn.moyada.dubbo.faker.core.listener.LoggingListener) AbstractInvoker(cn.moyada.dubbo.faker.core.invoke.AbstractInvoker) MethodInvokeDO(cn.moyada.dubbo.faker.core.model.MethodInvokeDO) CompletedListener(cn.moyada.dubbo.faker.core.listener.CompletedListener) ParamProvider(cn.moyada.dubbo.faker.core.provider.ParamProvider) MethodProxy(cn.moyada.dubbo.faker.core.model.MethodProxy)

Aggregations

MethodProxy (cn.moyada.dubbo.faker.core.model.MethodProxy)2 InitializeInvokerException (cn.moyada.dubbo.faker.core.exception.InitializeInvokerException)1 AbstractInvoker (cn.moyada.dubbo.faker.core.invoke.AbstractInvoker)1 AsyncInvoker (cn.moyada.dubbo.faker.core.invoke.AsyncInvoker)1 CompletedListener (cn.moyada.dubbo.faker.core.listener.CompletedListener)1 LoggingListener (cn.moyada.dubbo.faker.core.listener.LoggingListener)1 MethodInvokeDO (cn.moyada.dubbo.faker.core.model.MethodInvokeDO)1 ParamProvider (cn.moyada.dubbo.faker.core.provider.ParamProvider)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 MethodHandle (java.lang.invoke.MethodHandle)1 BeansException (org.springframework.beans.BeansException)1