use of com.weibo.api.motan.exception.MotanFrameworkException 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;
}
});
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class NettyDecoder method decode.
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
if (buffer.readableBytes() <= MotanConstants.NETTY_HEADER) {
return null;
}
buffer.markReaderIndex();
short type = buffer.readShort();
if (type != MotanConstants.NETTY_MAGIC_TYPE) {
buffer.resetReaderIndex();
throw new MotanFrameworkException("NettyDecoder transport header not support, type: " + type);
}
byte messageType = (byte) buffer.readShort();
long requestId = buffer.readLong();
int dataLength = buffer.readInt();
// FIXME 如果dataLength过大,可能导致问题
if (buffer.readableBytes() < dataLength) {
buffer.resetReaderIndex();
return null;
}
if (maxContentLength > 0 && dataLength > maxContentLength) {
LoggerUtil.warn("NettyDecoder transport data content length over of limit, size: {} > {}. remote={} local={}", dataLength, maxContentLength, ctx.getChannel().getRemoteAddress(), ctx.getChannel().getLocalAddress());
Exception e = new MotanServiceException("NettyDecoder transport data content length over of limit, size: " + dataLength + " > " + maxContentLength);
if (messageType == MotanConstants.FLAG_REQUEST) {
Response response = buildExceptionResponse(requestId, e);
channel.write(response);
throw e;
} else {
throw e;
}
}
byte[] data = new byte[dataLength];
buffer.readBytes(data);
try {
String remoteIp = getRemoteIp(channel);
return codec.decode(client, remoteIp, data);
} catch (Exception e) {
if (messageType == MotanConstants.FLAG_REQUEST) {
Response resonse = buildExceptionResponse(requestId, e);
channel.write(resonse);
return null;
} else {
Response resonse = buildExceptionResponse(requestId, e);
return resonse;
}
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ConsulClientWrapper method init.
@PostConstruct
void init() {
try {
String[] arr = registryUrl.split(":");
String host = arr[0];
int port = Integer.parseInt(arr[1]);
consulClient = new ConsulClient(host, port);
} catch (Exception e) {
throw new MotanFrameworkException("Fail to connect consul, cause: " + e.getMessage());
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ZookeeperRegistry method doUnregister.
@Override
protected void doUnregister(URL url) {
try {
serverLock.lock();
removeNode(url, ZkNodeType.AVAILABLE_SERVER);
removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
} catch (Throwable e) {
throw new MotanFrameworkException(String.format("Failed to unregister %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
} finally {
serverLock.unlock();
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ZookeeperRegistry method unsubscribeService.
@Override
protected void unsubscribeService(URL url, ServiceListener serviceListener) {
try {
clientLock.lock();
Map<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
if (childChangeListeners != null) {
IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
if (zkChildListener != null) {
zkClient.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener);
childChangeListeners.remove(serviceListener);
}
}
} catch (Throwable e) {
throw new MotanFrameworkException(String.format("Failed to unsubscribe service %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
} finally {
clientLock.unlock();
}
}
Aggregations