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();
}
}
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();
}
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);
}
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();
}
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);
}
Aggregations