1: @Override 2: public <T> Invoker<T> refer(final Class<T> type, final URL url) throws RpcException { 3: try { 4: 5: String address = url.getAddress(); 6: String backup = url.getParameter(Constants.BACKUP_KEY); 7: if (backup != null && backup.length() > 0) { 8: address += "," + backup; 9: } 10: MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(address)); 11: final MemcachedClient memcachedClient = builder.build(); 12: 13: 14: final int expiry = url.getParameter("expiry", 0); 15: final String get = url.getParameter("get", "get"); 16: final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set"); 17: final String delete = url.getParameter("delete", Map.class.equals(type) ? "remove" : "delete"); 18: return new AbstractInvoker<T>(type, url) { 19: 20: @Override 21: protected Result doInvoke(Invocation invocation) throws Throwable { 22: try { 23: 24: if (get.equals(invocation.getMethodName())) { 25: if (invocation.getArguments().length != 1) { 26: throw new IllegalArgumentException("The memcached get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); 27: } 28: return new RpcResult(memcachedClient.get(String.valueOf(invocation.getArguments()[0]))); 29: 30: } else if (set.equals(invocation.getMethodName())) { 31: if (invocation.getArguments().length != 2) { 32: throw new IllegalArgumentException("The memcached set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); 33: } 34: memcachedClient.set(String.valueOf(invocation.getArguments()[0]), expiry, invocation.getArguments()[1]); 35: return new RpcResult(); 36: 37: } else if (delete.equals(invocation.getMethodName())) { 38: if (invocation.getArguments().length != 1) { 39: throw new IllegalArgumentException("The memcached delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); 40: } 41: memcachedClient.delete(String.valueOf(invocation.getArguments()[0])); 42: return new RpcResult(); 43: 44: } else { 45: throw new UnsupportedOperationException("Unsupported method " + invocation.getMethodName() + " in memcached service."); 46: } 47: } catch (Throwable t) { 48: RpcException re = new RpcException("Failed to invoke memcached service method. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url + ", cause: " + t.getMessage(), t); 49: if (t instanceof TimeoutException || t instanceof SocketTimeoutException) { 50: re.setCode(RpcException.TIMEOUT_EXCEPTION); 51: } else if (t instanceof MemcachedException || t instanceof IOException) { 52: re.setCode(RpcException.NETWORK_EXCEPTION); 53: } 54: throw re; 55: } 56: } 57: 58: @Override 59: public void destroy() { 60: 61: super.destroy(); 62: 63: try { 64: memcachedClient.shutdown(); 65: } catch (Throwable e) { 66: logger.warn(e.getMessage(), e); 67: } 68: } 69: 70: }; 71: } catch (Throwable t) { 72: throw new RpcException("Failed to refer memcached service. interface: " + type.getName() + ", url: " + url + ", cause: " + t.getMessage(), t); 73: } 74: }
|