use of com.weibo.api.motan.transport.Channel 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.Channel 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.transport.Channel 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.transport.Channel 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;
}
});
}
Aggregations