Search in sources :

Example 1 with MotanFrameworkException

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

the class CompressRpcCodec method decodeResponse.

/**
     * 
     * @param body
     * @param dataType
     * @param requestId
     * @param rpcProtocolVersion rpc协议的版本号,不同版本可能有不同的序列化方式
     * @param serialization
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
private Object decodeResponse(byte[] body, byte dataType, long requestId, byte rpcProtocolVersion, Serialization serialization) throws IOException, ClassNotFoundException {
    ObjectInput input = createInput(getInputStream(body));
    long processTime = input.readLong();
    DefaultResponse response = new DefaultResponse();
    response.setRequestId(requestId);
    response.setProcessTime(processTime);
    if (dataType == MotanConstants.FLAG_RESPONSE_VOID) {
        return response;
    }
    String className = input.readUTF();
    Class<?> clz = ReflectUtil.forName(className);
    Object result = deserialize((byte[]) input.readObject(), clz, serialization);
    if (dataType == MotanConstants.FLAG_RESPONSE) {
        response.setValue(result);
    } else if (dataType == MotanConstants.FLAG_RESPONSE_ATTACHMENT) {
        response.setValue(result);
        Map<String, String> attachment = decodeRequestAttachments(input);
        checkAttachment(attachment);
    } else if (dataType == MotanConstants.FLAG_RESPONSE_EXCEPTION) {
        response.setException((Exception) result);
    } else {
        throw new MotanFrameworkException("decode error: response dataType not support " + dataType, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    response.setRequestId(requestId);
    input.close();
    return response;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ObjectInput(java.io.ObjectInput) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with MotanFrameworkException

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

the class ServiceConfigBean method checkAndConfigExport.

/**
     * 检查是否已经装配export,如果没有则到basicConfig查找
     */
private void checkAndConfigExport() {
    if (StringUtils.isBlank(getExport()) && getBasicServiceConfig() != null && !StringUtils.isBlank(getBasicServiceConfig().getExport())) {
        setExport(getBasicServiceConfig().getExport());
        if (getBasicServiceConfig().getProtocols() != null) {
            setProtocols(new ArrayList<ProtocolConfig>(getBasicServiceConfig().getProtocols()));
        }
    }
    if (CollectionUtil.isEmpty(getProtocols()) && StringUtils.isNotEmpty(getExport())) {
        Map<String, Integer> exportMap = ConfigUtil.parseExport(export);
        if (!exportMap.isEmpty()) {
            List<ProtocolConfig> protos = new ArrayList<ProtocolConfig>();
            for (String p : exportMap.keySet()) {
                ProtocolConfig proto = null;
                try {
                    proto = beanFactory.getBean(p, ProtocolConfig.class);
                } catch (NoSuchBeanDefinitionException e) {
                }
                if (proto == null) {
                    if (MotanConstants.PROTOCOL_MOTAN.equals(p)) {
                        proto = MotanFrameworkUtil.getDefaultProtocolConfig();
                    } else {
                        throw new MotanFrameworkException(String.format("cann't find %s ProtocolConfig bean! export:%s", p, export), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
                    }
                }
                protos.add(proto);
            }
            setProtocols(protos);
        }
    }
    if (StringUtils.isEmpty(getExport()) || CollectionUtil.isEmpty(getProtocols())) {
        throw new MotanFrameworkException(String.format("%s ServiceConfig must config right export value!", getInterface().getName()), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ArrayList(java.util.ArrayList) ProtocolConfig(com.weibo.api.motan.config.ProtocolConfig) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 3 with MotanFrameworkException

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

the class CommandServiceManager method notifyService.

@Override
public void notifyService(URL serviceUrl, URL registryUrl, List<URL> urls) {
    if (registry == null) {
        throw new MotanFrameworkException("registry must be set.");
    }
    URL urlCopy = serviceUrl.createCopy();
    String groupName = urlCopy.getParameter(URLParamType.group.getName(), URLParamType.group.getValue());
    groupServiceCache.put(groupName, urls);
    List<URL> finalResult = new ArrayList<URL>();
    if (commandCache != null) {
        Map<String, Integer> weights = new HashMap<String, Integer>();
        finalResult = discoverServiceWithCommand(refUrl, weights, commandCache);
    } else {
        LoggerUtil.info("command cache is null. service:" + serviceUrl.toSimpleString());
        // 没有命令时,只返回这个manager实际group对应的结果
        finalResult.addAll(discoverOneGroup(refUrl));
    }
    for (NotifyListener notifyListener : notifySet) {
        notifyListener.notify(registry.getUrl(), finalResult);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(com.weibo.api.motan.rpc.URL) NotifyListener(com.weibo.api.motan.registry.NotifyListener)

Example 4 with MotanFrameworkException

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

the class AbstractEndpointFactory method createServer.

@Override
public Server createServer(URL url, MessageHandler messageHandler) {
    HeartbeatFactory heartbeatFactory = getHeartbeatFactory(url);
    messageHandler = heartbeatFactory.wrapMessageHandler(messageHandler);
    synchronized (ipPort2ServerShareChannel) {
        String ipPort = url.getServerPortStr();
        String protocolKey = MotanFrameworkUtil.getProtocolKey(url);
        boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
        if (!shareChannel) {
            // 独享一个端口
            LoggerUtil.info(this.getClass().getSimpleName() + " create no_share_channel server: url={}", url);
            // 如果端口已经被使用了,使用该server bind 会有异常
            return innerCreateServer(url, messageHandler);
        }
        LoggerUtil.info(this.getClass().getSimpleName() + " create share_channel server: url={}", url);
        Server server = ipPort2ServerShareChannel.get(ipPort);
        if (server != null) {
            // can't share service channel
            if (!MotanFrameworkUtil.checkIfCanShallServiceChannel(server.getUrl(), url)) {
                throw new MotanFrameworkException("Service export Error: share channel but some config param is different, protocol or codec or serialize or maxContentLength or maxServerConnection or maxWorkerThread or heartbeatFactory, source=" + server.getUrl() + " target=" + url, MotanErrorMsgConstant.FRAMEWORK_EXPORT_ERROR);
            }
            saveEndpoint2Urls(server2UrlsShareChannel, server, protocolKey);
            return server;
        }
        url = url.createCopy();
        // 共享server端口,由于有多个interfaces存在,所以把path设置为空
        url.setPath("");
        server = innerCreateServer(url, messageHandler);
        ipPort2ServerShareChannel.put(ipPort, server);
        saveEndpoint2Urls(server2UrlsShareChannel, server, protocolKey);
        return server;
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Server(com.weibo.api.motan.transport.Server) HeartbeatFactory(com.weibo.api.motan.transport.HeartbeatFactory)

Example 5 with MotanFrameworkException

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

the class AbstractEndpointFactory method getHeartbeatFactory.

private HeartbeatFactory getHeartbeatFactory(URL url) {
    String heartbeatFactoryName = url.getParameter(URLParamType.heartbeatFactory.getName(), URLParamType.heartbeatFactory.getValue());
    HeartbeatFactory heartbeatFactory = ExtensionLoader.getExtensionLoader(HeartbeatFactory.class).getExtension(heartbeatFactoryName);
    if (heartbeatFactory == null) {
        throw new MotanFrameworkException("HeartbeatFactory not exist: " + heartbeatFactoryName);
    }
    return heartbeatFactory;
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) HeartbeatFactory(com.weibo.api.motan.transport.HeartbeatFactory)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)57 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)11 URL (com.weibo.api.motan.rpc.URL)10 IOException (java.io.IOException)9 Method (java.lang.reflect.Method)8 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 Response (com.weibo.api.motan.rpc.Response)5 TransportException (com.weibo.api.motan.transport.TransportException)4 ArrayList (java.util.ArrayList)4 Serialization (com.weibo.api.motan.codec.Serialization)3 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)3 Registry (com.weibo.api.motan.registry.Registry)3 HeartbeatFactory (com.weibo.api.motan.transport.HeartbeatFactory)3 HashMap (java.util.HashMap)3 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)2 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 Request (com.weibo.api.motan.rpc.Request)2 DeserializableObject (com.weibo.api.motan.serialize.DeserializableObject)2 ChannelFuture (io.netty.channel.ChannelFuture)2