Search in sources :

Example 16 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class ZookeeperRegistry method subscribeService.

@Override
protected void subscribeService(final URL url, final ServiceListener serviceListener) {
    try {
        clientLock.lock();
        ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners == null) {
            serviceListeners.putIfAbsent(url, new ConcurrentHashMap<ServiceListener, IZkChildListener>());
            childChangeListeners = serviceListeners.get(url);
        }
        IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
        if (zkChildListener == null) {
            childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() {

                @Override
                public void handleChildChange(String parentPath, List<String> currentChilds) {
                    serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(parentPath, currentChilds));
                    LoggerUtil.info(String.format("[ZookeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
                }
            });
            zkChildListener = childChangeListeners.get(serviceListener);
        }
        // 防止旧节点未正常注销
        removeNode(url, ZkNodeType.CLIENT);
        createNode(url, ZkNodeType.CLIENT);
        String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        zkClient.subscribeChildChanges(serverTypePath, zkChildListener);
        LoggerUtil.info(String.format("[ZookeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.AVAILABLE_SERVER), url.toFullStr()));
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to subscribe %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Example 17 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class ZookeeperRegistry method discoverService.

@Override
protected List<URL> discoverService(URL url) {
    try {
        String parentPath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        List<String> currentChilds = new ArrayList<String>();
        if (zkClient.exists(parentPath)) {
            currentChilds = zkClient.getChildren(parentPath);
        }
        return nodeChildsToUrls(parentPath, currentChilds);
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to discover service %s from zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ArrayList(java.util.ArrayList)

Example 18 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class ZookeeperRegistry method unsubscribeCommand.

@Override
protected void unsubscribeCommand(URL url, CommandListener commandListener) {
    try {
        clientLock.lock();
        Map<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
        if (dataChangeListeners != null) {
            IZkDataListener zkDataListener = dataChangeListeners.get(commandListener);
            if (zkDataListener != null) {
                zkClient.unsubscribeDataChanges(ZkUtils.toCommandPath(url), zkDataListener);
                dataChangeListeners.remove(commandListener);
            }
        }
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to unsubscribe command %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkDataListener(org.I0Itec.zkclient.IZkDataListener)

Example 19 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class MockDefaultRpcCodec method decode.

@Override
public Object decode(Channel channel, String remoteIp, byte[] buffer) throws IOException {
    Object result = codec.decode(channel, remoteIp, buffer);
    if (result instanceof Response) {
        DefaultResponse object = (DefaultResponse) result;
        byte flag = buffer[3];
        byte dataType = (byte) (flag & MASK);
        boolean isResponse = (dataType != MotanConstants.FLAG_REQUEST);
        if (object.getException() == null) {
            if (isResponse && object.getValue().equals("error")) {
                DefaultResponse response = (DefaultResponse) object;
                response.setException(new MotanFrameworkException("decode error: response dataType not support " + dataType, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR));
                return response;
            } else {
                throw new MotanFrameworkException(MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
            }
        }
        return object;
    }
    return result;
}
Also used : Response(com.weibo.api.motan.rpc.Response) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 20 with MotanFrameworkException

use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.

the class YarMessageHandlerWarpper method handle.

@Override
public Object handle(Channel channel, Object message) {
    FullHttpRequest httpRequest = (FullHttpRequest) message;
    String uri = httpRequest.getUri();
    // should not be null
    int index = uri.indexOf("?");
    String requestPath = uri;
    Map<String, String> attachments = null;
    if (index > -1) {
        requestPath = uri.substring(0, index);
        if (index != uri.length() - 1) {
            attachments = getAttachMents(uri.substring(index + 1, uri.length()));
        }
    }
    YarResponse yarResponse = null;
    String packagerName = "JSON";
    try {
        ByteBuf buf = httpRequest.content();
        final byte[] contentBytes = new byte[buf.readableBytes()];
        buf.getBytes(0, contentBytes);
        YarRequest yarRequest = new AttachmentRequest(YarProtocol.buildRequest(contentBytes), attachments);
        yarRequest.setRequestPath(requestPath);
        yarResponse = (YarResponse) orgHandler.handle(channel, yarRequest);
    } catch (Exception e) {
        LoggerUtil.error("YarMessageHandlerWarpper handle yar request fail.", e);
        yarResponse = YarProtocolUtil.buildDefaultErrorResponse(e.getMessage(), packagerName);
    }
    byte[] responseBytes;
    try {
        responseBytes = YarProtocol.toProtocolBytes(yarResponse);
    } catch (IOException e) {
        throw new MotanFrameworkException("convert yar response to bytes fail.", e);
    }
    FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes));
    httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
    httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, httpResponse.content().readableBytes());
    if (HttpHeaders.isKeepAlive(httpRequest)) {
        httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE);
    } else {
        httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.CLOSE);
    }
    return httpResponse;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) YarRequest(com.weibo.yar.YarRequest) YarResponse(com.weibo.yar.YarResponse) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) AttachmentRequest(com.weibo.api.motan.protocol.yar.AttachmentRequest)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)41 URL (com.weibo.api.motan.rpc.URL)7 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 ArrayList (java.util.ArrayList)6 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)5 IOException (java.io.IOException)5 Method (java.lang.reflect.Method)4 Registry (com.weibo.api.motan.registry.Registry)3 Response (com.weibo.api.motan.rpc.Response)3 HeartbeatFactory (com.weibo.api.motan.transport.HeartbeatFactory)3 ObjectInput (java.io.ObjectInput)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Serialization (com.weibo.api.motan.codec.Serialization)2 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)2 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)2 TransportException (com.weibo.api.motan.transport.TransportException)2 Map (java.util.Map)2 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)2