use of com.weibo.api.motan.rpc.Request in project motan by weibocom.
the class YarProtocolUtilTest method verifyMethodParam.
private <T> void verifyMethodParam(Class<T> interfaceClazz, T service, Method method, Object[] params, Object expectResult) throws Exception {
YarRequest yarRequest = new YarRequest();
yarRequest.setId(123);
yarRequest.setMethodName(method.getName());
yarRequest.setPackagerName("JSON");
yarRequest.setParameters(params);
Request request = YarProtocolUtil.convert(yarRequest, interfaceClazz);
assertNotNull(request);
assertEquals(method.getName(), request.getMethodName());
Object[] requestParams = request.getArguments();
assertEquals(params.length, requestParams.length);
Object result = method.invoke(service, requestParams);
assertEquals(expectResult, result);
}
use of com.weibo.api.motan.rpc.Request in project motan by weibocom.
the class YarMessageRouter method handle.
@Override
public Object handle(Channel channel, Object message) {
YarRequest yarRequest = (YarRequest) message;
String packagerName = yarRequest.getPackagerName();
Provider<?> provider = providerMap.get(yarRequest.getRequestPath());
if (provider == null) {
throw new MotanServiceException("can not find service provider. request path:" + yarRequest.getRequestPath());
}
Class<?> clazz = provider.getInterface();
Request request = YarProtocolUtil.convert(yarRequest, clazz);
Response response = call(request, provider);
YarResponse yarResponse = YarProtocolUtil.convert(response, packagerName);
return yarResponse;
}
use of com.weibo.api.motan.rpc.Request 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();
}
}
use of com.weibo.api.motan.rpc.Request 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();
}
}
use of com.weibo.api.motan.rpc.Request 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 {
MotanFrameworkUtil.logEvent(request, MotanConstants.TRACE_SEXECUTOR_START);
RpcContext.init(request);
processRequest(ctx, request, processStartTime);
} finally {
RpcContext.destroy();
}
}
});
} catch (RejectedExecutionException rejectException) {
DefaultResponse response = MotanFrameworkUtil.buildErrorResponse(request, new MotanServiceException("process thread pool is full, reject", MotanErrorMsgConstant.SERVICE_REJECT, false));
response.setProcessTime(System.currentTimeMillis() - processStartTime);
e.getChannel().write(response);
LoggerUtil.warn("process thread pool is full, reject, active={} poolSize={} corePoolSize={} maxPoolSize={} taskCount={} requestId={}", threadPoolExecutor.getActiveCount(), threadPoolExecutor.getPoolSize(), threadPoolExecutor.getCorePoolSize(), threadPoolExecutor.getMaximumPoolSize(), threadPoolExecutor.getTaskCount(), request.getRequestId());
rejectCounter.incrementAndGet();
}
}
Aggregations