Search in sources :

Example 21 with DefaultResponse

use of com.weibo.api.motan.rpc.DefaultResponse 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 22 with DefaultResponse

use of com.weibo.api.motan.rpc.DefaultResponse 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 23 with DefaultResponse

use of com.weibo.api.motan.rpc.DefaultResponse 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 {
                    RpcContext.init(request);
                    processRequest(ctx, request, processStartTime);
                } finally {
                    RpcContext.destroy();
                }
            }
        });
    } catch (RejectedExecutionException rejectException) {
        DefaultResponse response = new DefaultResponse();
        response.setRequestId(request.getRequestId());
        response.setException(new MotanServiceException("process thread pool is full, reject", MotanErrorMsgConstant.SERVICE_REJECT));
        response.setProcessTime(System.currentTimeMillis() - processStartTime);
        e.getChannel().write(response);
        LoggerUtil.debug("process thread pool is full, reject, active={} poolSize={} corePoolSize={} maxPoolSize={} taskCount={} requestId={}", threadPoolExecutor.getActiveCount(), threadPoolExecutor.getPoolSize(), threadPoolExecutor.getCorePoolSize(), threadPoolExecutor.getMaximumPoolSize(), threadPoolExecutor.getTaskCount(), request.getRequestId());
    }
}
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)

Example 24 with DefaultResponse

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

the class NettyResponseFutureTest method testNormal.

@Test
public void testNormal() {
    DefaultRequest request = new DefaultRequest();
    DefaultResponse defaultResponse = new DefaultResponse();
    defaultResponse.setValue("success");
    NettyResponseFuture response = new NettyResponseFuture(request, 100, client);
    response.onSuccess(defaultResponse);
    Object result = response.getValue();
    Assert.assertEquals(result, defaultResponse.getValue());
    Assert.assertTrue(response.isDone());
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 25 with DefaultResponse

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

the class NettyResponseFutureTest method testListener.

@Test
public void testListener() {
    DefaultRequest request = new DefaultRequest();
    NettyResponseFuture response = new NettyResponseFuture(request, 100, client);
    final AtomicBoolean result = new AtomicBoolean(false);
    response.addListener(new FutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                result.set(true);
            } else {
                result.set(false);
            }
        }
    });
    DefaultResponse defaultResponse = new DefaultResponse();
    defaultResponse.setValue(new Object());
    response.onSuccess(defaultResponse);
    Assert.assertTrue(result.get());
    response = new NettyResponseFuture(request, 100, client);
    response.addListener(new FutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                result.set(true);
            } else {
                result.set(false);
            }
        }
    });
    response.cancel();
    result.set(true);
    response.addListener(new FutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                result.set(true);
            } else {
                result.set(false);
            }
        }
    });
    Assert.assertFalse(result.get());
}
Also used : FutureListener(com.weibo.api.motan.rpc.FutureListener) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Future(com.weibo.api.motan.rpc.Future) Test(org.junit.Test)

Aggregations

DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)28 Test (org.junit.Test)12 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)7 Request (com.weibo.api.motan.rpc.Request)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)5 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)4 Response (com.weibo.api.motan.rpc.Response)4 Channel (com.weibo.api.motan.transport.Channel)4 MessageHandler (com.weibo.api.motan.transport.MessageHandler)4 MotanBizException (com.weibo.api.motan.exception.MotanBizException)3 URL (com.weibo.api.motan.rpc.URL)2 YarResponse (com.weibo.yar.YarResponse)2 ObjectInput (java.io.ObjectInput)2 Model (com.weibo.api.motan.protocol.example.Model)1 DefaultProvider (com.weibo.api.motan.rpc.DefaultProvider)1 Future (com.weibo.api.motan.rpc.Future)1 FutureListener (com.weibo.api.motan.rpc.FutureListener)1 Provider (com.weibo.api.motan.rpc.Provider)1 YarRequest (com.weibo.yar.YarRequest)1 Tracer (io.opentracing.Tracer)1