Search in sources :

Example 6 with MessageHandler

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

the class NettyEndpointFactoryTest method testShareChannel.

private void testShareChannel(boolean isServer) {
    NettyEndpointFactory factory = new NettyEndpointFactory();
    MessageHandler handler = new ProviderMessageRouter();
    URL url1 = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
    url1.addParameter(URLParamType.shareChannel.getName(), "true");
    Endpoint endpoint1 = createEndpoint(url1, handler, isServer, factory);
    Assert.assertEquals(endpoint1.getUrl().getServerPortStr(), url1.getServerPortStr());
    URL url2 = new URL("motan", "localhost", 18081, "com.weibo.api.motan.protocol.example.IHello1");
    url2.addParameter(URLParamType.shareChannel.getName(), "true");
    Endpoint endpoint2 = createEndpoint(url2, handler, isServer, factory);
    Assert.assertEquals(endpoint2.getUrl().getServerPortStr(), url2.getServerPortStr());
    URL url3 = new URL("motan", "localhost", 18081, "com.weibo.api.motan.protocol.example.IHello2");
    url3.addParameter(URLParamType.shareChannel.getName(), "true");
    Endpoint endpoint3 = createEndpoint(url3, handler, isServer, factory);
    if (isServer) {
        Assert.assertTrue(endpoint2 == endpoint3);
    } else {
        Assert.assertTrue(endpoint2 != endpoint3);
    }
    URL url4 = new URL("injvm", "localhost", 18081, "com.weibo.api.motan.protocol.example.IHello3");
    url4.addParameter(URLParamType.shareChannel.getName(), "true");
    Endpoint endpoint4 = null;
    if (isServer) {
        try {
            endpoint4 = createEndpoint(url4, handler, isServer, factory);
            Assert.assertTrue(false);
        } catch (Exception e) {
            Assert.assertTrue(true);
        }
    } else {
        try {
            endpoint4 = createEndpoint(url4, handler, isServer, factory);
            Assert.assertTrue(true);
        } catch (Exception e) {
            Assert.assertTrue(false);
        }
    }
    if (isServer) {
        Assert.assertEquals(factory.getShallServerChannels().size(), 2);
    }
    if (isServer) {
        factory.safeReleaseResource((Server) endpoint1, url1);
        factory.safeReleaseResource((Server) endpoint2, url2);
        factory.safeReleaseResource((Server) endpoint3, url3);
        Assert.assertEquals(factory.getShallServerChannels().size(), 0);
    } else {
        Assert.assertEquals(((HeartbeatClientEndpointManager) factory.getEndpointManager()).getClients().size(), 4);
        factory.safeReleaseResource((Client) endpoint1, url1);
        Assert.assertEquals(((HeartbeatClientEndpointManager) factory.getEndpointManager()).getClients().size(), 3);
        factory.safeReleaseResource((Client) endpoint2, url2);
        Assert.assertEquals(((HeartbeatClientEndpointManager) factory.getEndpointManager()).getClients().size(), 2);
        factory.safeReleaseResource((Client) endpoint3, url3);
        Assert.assertEquals(((HeartbeatClientEndpointManager) factory.getEndpointManager()).getClients().size(), 1);
        factory.safeReleaseResource((Client) endpoint4, url4);
        Assert.assertEquals(((HeartbeatClientEndpointManager) factory.getEndpointManager()).getClients().size(), 0);
    }
}
Also used : HeartbeatClientEndpointManager(com.weibo.api.motan.transport.support.HeartbeatClientEndpointManager) ProviderMessageRouter(com.weibo.api.motan.transport.ProviderMessageRouter) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Endpoint(com.weibo.api.motan.transport.Endpoint) URL(com.weibo.api.motan.rpc.URL)

Example 7 with MessageHandler

use of com.weibo.api.motan.transport.MessageHandler 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 8 with MessageHandler

use of com.weibo.api.motan.transport.MessageHandler 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 9 with MessageHandler

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

the class NettyClient method initClientBootstrap.

/**
	 * 初始化 netty clientBootstrap
	 */
private void initClientBootstrap() {
    bootstrap = new ClientBootstrap(channelFactory);
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    // 实际上,极端情况下,connectTimeout会达到500ms,因为netty nio的实现中,是依赖BossThread来控制超时,
    // 如果为了严格意义的timeout,那么需要应用端进行控制。
    int timeout = getUrl().getIntParameter(URLParamType.connectTimeout.getName(), URLParamType.connectTimeout.getIntValue());
    if (timeout <= 0) {
        throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
    bootstrap.setOption("connectTimeoutMillis", timeout);
    // 最大响应包限制
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", new NettyDecoder(codec, NettyClient.this, maxContentLength));
            pipeline.addLast("encoder", new NettyEncoder(codec, NettyClient.this));
            pipeline.addLast("handler", new NettyChannelHandler(NettyClient.this, new MessageHandler() {

                @Override
                public Object handle(Channel channel, Object message) {
                    Response response = (Response) message;
                    NettyResponseFuture responseFuture = NettyClient.this.removeCallback(response.getRequestId());
                    if (responseFuture == null) {
                        LoggerUtil.warn("NettyClient has response from server, but resonseFuture not exist,  requestId={}", response.getRequestId());
                        return null;
                    }
                    if (response.getException() != null) {
                        responseFuture.onFailure(response);
                    } else {
                        responseFuture.onSuccess(response);
                    }
                    return null;
                }
            }));
            return pipeline;
        }
    });
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Channel(com.weibo.api.motan.transport.Channel) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory)

Aggregations

MessageHandler (com.weibo.api.motan.transport.MessageHandler)9 Channel (com.weibo.api.motan.transport.Channel)7 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)5 Test (org.junit.Test)5 Request (com.weibo.api.motan.rpc.Request)4 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)3 URL (com.weibo.api.motan.rpc.URL)3 Endpoint (com.weibo.api.motan.transport.Endpoint)2 ProviderMessageRouter (com.weibo.api.motan.transport.ProviderMessageRouter)2 HeartbeatClientEndpointManager (com.weibo.api.motan.transport.support.HeartbeatClientEndpointManager)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 Expectations (org.jmock.Expectations)2 Invocation (org.jmock.api.Invocation)2 CustomAction (org.jmock.lib.action.CustomAction)2 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 Response (com.weibo.api.motan.rpc.Response)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1