Search in sources :

Example 1 with MessageHandler

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

Example 2 with MessageHandler

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

the class NettyServerExample method testServerTrace.

@Test
public void testServerTrace() throws InterruptedException {
    String interfaceName = "com.weibo.api.motan.protocol.example.IHello";
    Map<String, String> parameters = new HashMap<>();
    parameters.put("requestTimeout", "500");
    URL url = new URL("netty", "localhost", 18080, interfaceName, parameters);
    MotanSwitcherUtil.setSwitcherValue(MotanConstants.MOTAN_TRACE_INFO_SWITCHER, true);
    final Set<String> serverTraceKey = new HashSet<>();
    serverTraceKey.add(MotanConstants.TRACE_SRECEIVE);
    serverTraceKey.add(MotanConstants.TRACE_SDECODE);
    serverTraceKey.add(MotanConstants.TRACE_SEXECUTOR_START);
    serverTraceKey.add(MotanConstants.TRACE_PROCESS);
    serverTraceKey.add(MotanConstants.TRACE_SENCODE);
    serverTraceKey.add(MotanConstants.TRACE_SSEND);
    NettyServer nettyServer = new NettyServer(url, new MessageHandler() {

        @Override
        public Object handle(Channel channel, Object message) {
            final Request request = (Request) message;
            if (request instanceof Traceable) {
                if (((Traceable) request).getTraceableContext().getReceiveTime() != 0) {
                    serverTraceKey.remove(MotanConstants.TRACE_SRECEIVE);
                }
                serverTraceKey.removeAll(((Traceable) request).getTraceableContext().getTraceInfoMap().keySet());
            }
            final DefaultResponse response = new DefaultResponse();
            response.setRequestId(request.getRequestId());
            response.setValue("method: " + request.getMethodName() + " requestId: " + request.getRequestId());
            response.addFinishCallback(new Runnable() {

                @Override
                public void run() {
                    serverTraceKey.removeAll(((Traceable) response).getTraceableContext().getTraceInfoMap().keySet());
                    if (((Traceable) response).getTraceableContext().getSendTime() != 0) {
                        serverTraceKey.remove(MotanConstants.TRACE_SSEND);
                    }
                }
            }, null);
            return response;
        }
    });
    nettyServer.open();
    DefaultRequest request = new DefaultRequest();
    request.setRequestId(RequestIdGenerator.getRequestId());
    request.setInterfaceName(interfaceName);
    request.setMethodName("hello");
    request.setParamtersDesc("void");
    TraceableContext requestTraceableContext = request.getTraceableContext();
    Assert.assertEquals(0, requestTraceableContext.getSendTime());
    Assert.assertEquals(0, requestTraceableContext.getReceiveTime());
    NettyClient nettyClient = new NettyClient(url);
    nettyClient.open();
    Response response;
    try {
        response = nettyClient.request(request);
        Object result = response.getValue();
        Assert.assertEquals("method: " + request.getMethodName() + " requestId: " + request.getRequestId(), result);
        Assert.assertNotNull(requestTraceableContext.getTraceInfo(MotanConstants.TRACE_CONNECTION));
        Assert.assertNotNull(requestTraceableContext.getTraceInfo(MotanConstants.TRACE_CENCODE));
        Assert.assertFalse(0 == requestTraceableContext.getSendTime());
        if (response instanceof Traceable) {
            Assert.assertFalse(0 == ((Traceable) response).getTraceableContext().getReceiveTime());
            Assert.assertNotNull(((Traceable) response).getTraceableContext().getTraceInfo(MotanConstants.TRACE_CDECODE));
        }
    } catch (Exception e) {
        fail();
    }
    Thread.sleep(100);
    Assert.assertTrue(serverTraceKey.isEmpty());
    nettyClient.close();
    nettyServer.close();
}
Also used : MessageHandler(com.weibo.api.motan.transport.MessageHandler) HashMap(java.util.HashMap) Channel(com.weibo.api.motan.transport.Channel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with MessageHandler

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

the class NettyServerExample method main.

public static void main(String[] args) throws InterruptedException {
    URL url = new URL("netty", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
    NettyServer nettyServer = new NettyServer(url, new MessageHandler() {

        @Override
        public Object handle(Channel channel, Object message) {
            Request request = (Request) message;
            System.out.println("[server] get request: requestId: " + request.getRequestId() + " method: " + request.getMethodName());
            DefaultResponse response = new DefaultResponse();
            response.setRequestId(request.getRequestId());
            response.setValue("method: " + request.getMethodName() + " time: " + System.currentTimeMillis());
            return response;
        }
    });
    nettyServer.open();
    System.out.println("~~~~~~~~~~~~~ Server open ~~~~~~~~~~~~~");
    Thread.sleep(100000);
}
Also used : MessageHandler(com.weibo.api.motan.transport.MessageHandler) Channel(com.weibo.api.motan.transport.Channel)

Example 4 with MessageHandler

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

the class NettyDecoderTest method setUp.

@Before
public void setUp() {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("requestTimeout", "500");
    url = new URL("netty", "localhost", 18080, interfaceName, parameters);
    messageHandler = new MessageHandler() {

        @Override
        public Object handle(Channel channel, Object message) {
            Request request = (Request) message;
            DefaultResponse response = new DefaultResponse();
            response.setRequestId(request.getRequestId());
            response.setValue("method: " + request.getMethodName() + " requestId: " + request.getRequestId());
            return response;
        }
    };
    nettyServer = new NettyServer(url, messageHandler);
    nettyServer.open();
}
Also used : MessageHandler(com.weibo.api.motan.transport.MessageHandler) HashMap(java.util.HashMap) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(com.weibo.api.motan.transport.Channel) Before(org.junit.Before)

Example 5 with MessageHandler

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

the class NettyHttpRequestHandlerTest method testChannelRead0.

@Test
public void testChannelRead0() throws Exception {
    final MessageHandler messageHandler = mockery.mock(MessageHandler.class);
    final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class);
    final FullHttpResponse response = mockery.mock(FullHttpResponse.class);
    mockery.checking(new Expectations() {

        {
            allowing(ctx).write(with(any(FullHttpResponse.class)));
            will(new CustomAction("verify") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    FullHttpResponse actualResponse = (FullHttpResponse) invocation.getParameter(0);
                    assertNotNull(actualResponse);
                    assertEquals(response, actualResponse);
                    return null;
                }
            });
            allowing(ctx).flush();
            will(returnValue(null));
            allowing(ctx).close();
            will(returnValue(null));
            atLeast(1).of(messageHandler).handle(with(any(Channel.class)), with(any(Object.class)));
            will(returnValue(response));
            allowing(response).headers();
            will(returnValue(new DefaultHttpHeaders()));
        }
    });
    FullHttpRequest httpRequest = buildHttpRequest("anyPath");
    NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler);
    handler.channelRead0(ctx, httpRequest);
}
Also used : Expectations(org.jmock.Expectations) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) Channel(com.weibo.api.motan.transport.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

Aggregations

MessageHandler (com.weibo.api.motan.transport.MessageHandler)11 Channel (com.weibo.api.motan.transport.Channel)9 Test (org.junit.Test)6 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)3 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)3 Request (com.weibo.api.motan.rpc.Request)3 URL (com.weibo.api.motan.rpc.URL)2 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 HashMap (java.util.HashMap)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 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 HashSet (java.util.HashSet)1 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)1 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)1