Search in sources :

Example 16 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class YarProtocolUtilTest method verifyMethodParam.

private <T> void verifyMethodParam(Class<T> interfaceClazz, T service, Method method, Object[] params, Object expectResult) throws Exception {
    YarRequest yarRequest = new YarRequest();
    yarRequest.setId(123);
    yarRequest.setMethodName(method.getName());
    yarRequest.setPackagerName("JSON");
    yarRequest.setParameters(params);
    Request request = YarProtocolUtil.convert(yarRequest, interfaceClazz);
    assertNotNull(request);
    assertEquals(method.getName(), request.getMethodName());
    Object[] requestParams = request.getArguments();
    assertEquals(params.length, requestParams.length);
    Object result = method.invoke(service, requestParams);
    assertEquals(expectResult, result);
}
Also used : YarRequest(com.weibo.yar.YarRequest) YarRequest(com.weibo.yar.YarRequest) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Example 17 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class YarMessageRouter method handle.

@Override
public Object handle(Channel channel, Object message) {
    YarRequest yarRequest = (YarRequest) message;
    String packagerName = yarRequest.getPackagerName();
    Provider<?> provider = providerMap.get(yarRequest.getRequestPath());
    if (provider == null) {
        throw new MotanServiceException("can not find service provider. request path:" + yarRequest.getRequestPath());
    }
    Class<?> clazz = provider.getInterface();
    Request request = YarProtocolUtil.convert(yarRequest, clazz);
    Response response = call(request, provider);
    YarResponse yarResponse = YarProtocolUtil.convert(response, packagerName);
    return yarResponse;
}
Also used : Response(com.weibo.api.motan.rpc.Response) YarResponse(com.weibo.yar.YarResponse) YarRequest(com.weibo.yar.YarRequest) YarRequest(com.weibo.yar.YarRequest) Request(com.weibo.api.motan.rpc.Request) YarResponse(com.weibo.yar.YarResponse) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 18 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class UnSerializableClass method testNettyEncodeException.

@Test
public void testNettyEncodeException() throws Exception {
    NettyServer nettyServer;
    nettyServer = new NettyServer(url, new MessageHandler() {

        @Override
        public Object handle(Channel channel, Object message) {
            Request request = (Request) message;
            DefaultResponse response = new DefaultResponse();
            response.setRequestId(request.getRequestId());
            // 序列化错误
            response.setValue(new UnSerializableClass());
            return response;
        }
    });
    nettyServer.open();
    NettyClient nettyClient = new NettyClient(url);
    nettyClient.open();
    DefaultRequest request = new DefaultRequest();
    request.setRequestId(RequestIdGenerator.getRequestId());
    request.setInterfaceName(url.getPath());
    request.setMethodName("helloSerializable");
    request.setParamtersDesc("com.weibo.api.motan.procotol.example.UnSerializableClass");
    request.setArguments(new Object[] { new UnSerializableClass() });
    try {
        nettyClient.request(request);
        Assert.assertFalse(true);
    } catch (Exception e) {
        Assert.assertTrue(true);
    }
    DefaultRequest request1 = new DefaultRequest();
    request1.setRequestId(RequestIdGenerator.getRequestId());
    request1.setInterfaceName(url.getPath());
    request1.setMethodName("helloSerializable");
    request1.setParamtersDesc("void");
    try {
        nettyClient.request(request1);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("error_code: 20002"));
    } finally {
        nettyClient.close();
        nettyServer.close();
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MessageHandler(com.weibo.api.motan.transport.MessageHandler) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Channel(com.weibo.api.motan.transport.Channel) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 19 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class UnSerializableClass method testNettyDecodeException.

@Test
public void testNettyDecodeException() throws Exception {
    NettyServer nettyServer;
    nettyServer = new NettyServer(url, new MessageHandler() {

        @Override
        public Object handle(Channel channel, Object message) {
            Request request = (Request) message;
            DefaultResponse response = new DefaultResponse();
            response.setRequestId(request.getRequestId());
            response.setValue("error");
            return response;
        }
    });
    nettyServer.open();
    NettyClient nettyClient = new NettyClient(url);
    nettyClient.open();
    DefaultRequest request = new DefaultRequest();
    request.setRequestId(RequestIdGenerator.getRequestId());
    request.setInterfaceName(url.getPath());
    request.setMethodName("hello");
    request.setParamtersDesc("void");
    try {
        nettyClient.request(request);
        Assert.assertTrue(false);
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("response dataType not support"));
    } finally {
        nettyClient.close();
        nettyServer.close();
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MessageHandler(com.weibo.api.motan.transport.MessageHandler) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Channel(com.weibo.api.motan.transport.Channel) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 20 with Request

use of com.weibo.api.motan.rpc.Request in project motan by weibocom.

the class NettyChannelHandler method processRequest.

/**
 * <pre>
 *  request process: 主要来自于client的请求,需要使用threadPoolExecutor进行处理,避免service message处理比较慢导致iothread被阻塞
 * </pre>
 *
 * @param ctx
 * @param e
 */
private void processRequest(final ChannelHandlerContext ctx, MessageEvent e) {
    final Request request = (Request) e.getMessage();
    request.setAttachment(URLParamType.host.getName(), NetUtils.getHostName(ctx.getChannel().getRemoteAddress()));
    final long processStartTime = System.currentTimeMillis();
    // 使用线程池方式处理
    try {
        threadPoolExecutor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    MotanFrameworkUtil.logEvent(request, MotanConstants.TRACE_SEXECUTOR_START);
                    RpcContext.init(request);
                    processRequest(ctx, request, processStartTime);
                } finally {
                    RpcContext.destroy();
                }
            }
        });
    } catch (RejectedExecutionException rejectException) {
        DefaultResponse response = MotanFrameworkUtil.buildErrorResponse(request, new MotanServiceException("process thread pool is full, reject", MotanErrorMsgConstant.SERVICE_REJECT, false));
        response.setProcessTime(System.currentTimeMillis() - processStartTime);
        e.getChannel().write(response);
        LoggerUtil.warn("process thread pool is full, reject, active={} poolSize={} corePoolSize={} maxPoolSize={} taskCount={} requestId={}", threadPoolExecutor.getActiveCount(), threadPoolExecutor.getPoolSize(), threadPoolExecutor.getCorePoolSize(), threadPoolExecutor.getMaximumPoolSize(), threadPoolExecutor.getTaskCount(), request.getRequestId());
        rejectCounter.incrementAndGet();
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Request(com.weibo.api.motan.rpc.Request) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

Request (com.weibo.api.motan.rpc.Request)31 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)12 Expectations (org.jmock.Expectations)12 Response (com.weibo.api.motan.rpc.Response)10 IHello (com.weibo.api.motan.protocol.example.IHello)9 URL (com.weibo.api.motan.rpc.URL)9 Test (org.junit.Test)9 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)6 Referer (com.weibo.api.motan.rpc.Referer)5 HashMap (java.util.HashMap)5 RegistryService (com.weibo.api.motan.registry.RegistryService)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IWorld (com.weibo.api.motan.protocol.example.IWorld)3 Channel (com.weibo.api.motan.transport.Channel)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 YarRequest (com.weibo.yar.YarRequest)3 ArrayList (java.util.ArrayList)3 Serialization (com.weibo.api.motan.codec.Serialization)2 IOException (java.io.IOException)2