use of com.weibo.api.motan.rpc.DefaultResponse 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.DefaultResponse 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.DefaultResponse 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 {
RpcContext.init(request);
processRequest(ctx, request, processStartTime);
} finally {
RpcContext.destroy();
}
}
});
} catch (RejectedExecutionException rejectException) {
DefaultResponse response = new DefaultResponse();
response.setRequestId(request.getRequestId());
response.setException(new MotanServiceException("process thread pool is full, reject", MotanErrorMsgConstant.SERVICE_REJECT));
response.setProcessTime(System.currentTimeMillis() - processStartTime);
e.getChannel().write(response);
LoggerUtil.debug("process thread pool is full, reject, active={} poolSize={} corePoolSize={} maxPoolSize={} taskCount={} requestId={}", threadPoolExecutor.getActiveCount(), threadPoolExecutor.getPoolSize(), threadPoolExecutor.getCorePoolSize(), threadPoolExecutor.getMaximumPoolSize(), threadPoolExecutor.getTaskCount(), request.getRequestId());
}
}
use of com.weibo.api.motan.rpc.DefaultResponse in project motan by weibocom.
the class NettyResponseFutureTest method testNormal.
@Test
public void testNormal() {
DefaultRequest request = new DefaultRequest();
DefaultResponse defaultResponse = new DefaultResponse();
defaultResponse.setValue("success");
NettyResponseFuture response = new NettyResponseFuture(request, 100, client);
response.onSuccess(defaultResponse);
Object result = response.getValue();
Assert.assertEquals(result, defaultResponse.getValue());
Assert.assertTrue(response.isDone());
}
use of com.weibo.api.motan.rpc.DefaultResponse in project motan by weibocom.
the class NettyResponseFutureTest method testListener.
@Test
public void testListener() {
DefaultRequest request = new DefaultRequest();
NettyResponseFuture response = new NettyResponseFuture(request, 100, client);
final AtomicBoolean result = new AtomicBoolean(false);
response.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess()) {
result.set(true);
} else {
result.set(false);
}
}
});
DefaultResponse defaultResponse = new DefaultResponse();
defaultResponse.setValue(new Object());
response.onSuccess(defaultResponse);
Assert.assertTrue(result.get());
response = new NettyResponseFuture(request, 100, client);
response.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess()) {
result.set(true);
} else {
result.set(false);
}
}
});
response.cancel();
result.set(true);
response.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess()) {
result.set(true);
} else {
result.set(false);
}
}
});
Assert.assertFalse(result.get());
}
Aggregations