/** * 根据方法的服务质量过滤,优先保留服务质量good的clients,数量低于least时加入服务质量normal的clients * * @param clientList * @param request * @param least 最少保留个数 * @return */ public List<Client> getQualityPreferClients( List<Client> clientList, InvocationRequest request, float least) { // 筛选good,normal,bad clients // 直接进行服务质量路由,先只保留服务质量good的,如果不够(比如少于1个),加入服务质量normal的 if (!CollectionUtils.isEmpty(addrReqUrlQualities)) { String requestUrl = getRequestUrl(request); Map<RequrlQuality, List<Client>> filterQualityClientsMap = new HashMap<RequrlQuality, List<Client>>(); for (RequrlQuality reqQuality : RequrlQuality.values()) { filterQualityClientsMap.put(reqQuality, new ArrayList<Client>()); } for (Client client : clientList) { if (addrReqUrlQualities.containsKey(client.getAddress())) { ConcurrentHashMap<String, Quality> reqUrlQualities = addrReqUrlQualities.get(client.getAddress()); if (reqUrlQualities.containsKey(requestUrl)) { Quality quality = reqUrlQualities.get(requestUrl); switch (quality.getQuality()) { case REQURL_QUALITY_GOOD: filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_GOOD).add(client); break; case REQURL_QUALITY_NORNAL: filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_NORNAL).add(client); break; case REQURL_QUALITY_BAD: filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_BAD).add(client); break; default: // never be here break; } } } } List<Client> filterQualityClients = new ArrayList<Client>(); filterQualityClients.addAll(filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_GOOD)); if (filterQualityClients.size() < least) { filterQualityClients.addAll( filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_NORNAL)); } return filterQualityClients; } return clientList; }
private void connectionException(Client client, Object attachment, Throwable e) { logger.info("exception while connecting to " + client, e); if (attachment == null) { return; } Object[] msg = (Object[]) attachment; if (msg[0] instanceof InvokerContext) { InvokerContext invokerContext = (InvokerContext) msg[0]; InvocationRequest request = invokerContext.getRequest(); if (request.getMessageType() == Constants.MESSAGE_TYPE_SERVICE && msg[1] != null) { try { Callback callback = (Callback) msg[1]; if (client != null) { client.write(request, callback); } else { logger.error("no client found with service:" + request.getServiceName()); } } catch (Throwable ex) { logger.error("", ex); } logger.error("", e); } } }