Search in sources :

Example 1 with Channel

use of com.weibo.api.motan.transport.Channel in project motan by weibocom.

the class CompressRpcCodecTest method testGzip.

// 测试gz压缩
@Test
public void testGzip() throws IOException {
    DefaultRequest request = getRequest("int[]", new Object[] { new int[] { 1, 2 } });
    byte[] bytes = rpcCodec.encode(channel, request);
    assertFalse(isGzip(bytes));
    // 小于阈值
    int bodyLength = ByteUtil.bytes2int(bytes, 12);
    URL url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
    Map<String, String> params = url.getParameters();
    params.put(URLParamType.usegz.name(), "true");
    params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength - 1));
    Channel tempChannel = new MockChannel(url);
    bytes = rpcCodec.encode(tempChannel, request);
    assertTrue(isGzip(bytes));
    // 等于、大于阈值.url内部对数字类型参数有缓存,且无法动态更新,需要重新生产url及channel
    url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
    params = url.getParameters();
    params.put(URLParamType.usegz.name(), "true");
    params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength));
    tempChannel = new MockChannel(url);
    bytes = rpcCodec.encode(tempChannel, request);
    assertFalse(isGzip(bytes));
}
Also used : MockChannel(com.weibo.api.motan.mock.MockChannel) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) MockChannel(com.weibo.api.motan.mock.MockChannel) Channel(com.weibo.api.motan.transport.Channel) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 2 with Channel

use of com.weibo.api.motan.transport.Channel in project motan by weibocom.

the class YarMessageHandlerWarpperTest method testHandle.

@Test
public void testHandle() throws Exception {
    YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
    final YarResponse yarResponse = YarProtocolUtil.buildDefaultErrorResponse("test err", "JSON");
    YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {

        @Override
        public Object handle(Channel channel, Object message) {
            AttachmentRequest request = (AttachmentRequest) message;
            verifyAttachments(request.getAttachments());
            return yarResponse;
        }
    });
    FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
    assertNotNull(httpResponse);
    assertNotNull(httpResponse.content());
    YarResponse retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertEquals(yarResponse, retYarResponse);
}
Also used : YarMessageRouter(com.weibo.api.motan.protocol.yar.YarMessageRouter) YarRequest(com.weibo.yar.YarRequest) Channel(com.weibo.api.motan.transport.Channel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) YarResponse(com.weibo.yar.YarResponse) AttachmentRequest(com.weibo.api.motan.protocol.yar.AttachmentRequest) Test(org.junit.Test)

Example 3 with Channel

use of com.weibo.api.motan.transport.Channel in project motan by weibocom.

the class YarMessageHandlerWarpperTest method testAbnormal.

@Test
public void testAbnormal() throws Exception {
    final String errmsg = "rpc process error";
    YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {

        @Override
        public Object handle(Channel channel, Object message) {
            throw new RuntimeException(errmsg);
        }
    });
    // yar协议无法解析
    FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    YarResponse retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertNotNull(retYarResponse.getError());
    // yar协议可以正常解析,但后续处理异常
    YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
    httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertEquals(errmsg, retYarResponse.getError());
}
Also used : YarMessageRouter(com.weibo.api.motan.protocol.yar.YarMessageRouter) YarRequest(com.weibo.yar.YarRequest) Channel(com.weibo.api.motan.transport.Channel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) YarResponse(com.weibo.yar.YarResponse) Test(org.junit.Test)

Example 4 with Channel

use of com.weibo.api.motan.transport.Channel in project motan by weibocom.

the class NettyClient method request.

/**
	 * 请求remote service
	 * 
	 * <pre>
	 * 		1)  get connection from pool
	 * 		2)  async requset
	 * 		3)  return connection to pool
	 * 		4)  check if async return response, true: return ResponseFuture;  false: return result
	 * </pre>
	 * 
	 * @param request
	 * @param async
	 * @return
	 * @throws TransportException
	 */
private Response request(Request request, boolean async) throws TransportException {
    Channel channel = null;
    Response response = null;
    try {
        // return channel or throw exception(timeout or connection_fail)
        channel = borrowObject();
        if (channel == null) {
            LoggerUtil.error("NettyClient borrowObject null: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request));
            return null;
        }
        // async request
        response = channel.request(request);
        // return channel to pool
        returnObject(channel);
    } catch (Exception e) {
        LoggerUtil.error("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
        //TODO 对特定的异常回收channel
        invalidateObject(channel);
        if (e instanceof MotanAbstractException) {
            throw (MotanAbstractException) e;
        } else {
            throw new MotanServiceException("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
        }
    }
    // aysnc or sync result
    response = asyncResponse(response, async);
    return response;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) MotanAbstractException(com.weibo.api.motan.exception.MotanAbstractException) Channel(com.weibo.api.motan.transport.Channel) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanAbstractException(com.weibo.api.motan.exception.MotanAbstractException) TransportException(com.weibo.api.motan.transport.TransportException)

Example 5 with Channel

use of com.weibo.api.motan.transport.Channel in project motan by weibocom.

the class UnSerializableClass method testNettyRequestDecodeException.

/**
     * @throws Exception
     */
@Test
public void testNettyRequestDecodeException() 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("success");
            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("framework decode error"));
    } 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)

Aggregations

Channel (com.weibo.api.motan.transport.Channel)9 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 Test (org.junit.Test)6 MessageHandler (com.weibo.api.motan.transport.MessageHandler)5 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)4 Request (com.weibo.api.motan.rpc.Request)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 YarMessageRouter (com.weibo.api.motan.protocol.yar.YarMessageRouter)2 Response (com.weibo.api.motan.rpc.Response)2 URL (com.weibo.api.motan.rpc.URL)2 YarRequest (com.weibo.yar.YarRequest)2 YarResponse (com.weibo.yar.YarResponse)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 MotanAbstractException (com.weibo.api.motan.exception.MotanAbstractException)1 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)1 MockChannel (com.weibo.api.motan.mock.MockChannel)1 AttachmentRequest (com.weibo.api.motan.protocol.yar.AttachmentRequest)1 TransportException (com.weibo.api.motan.transport.TransportException)1 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)1 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)1