1: @Override 2: public <T> Invoker<T> refer(final Class<T> type, final URL url) throws RpcException { 3: try { 4: 5: GenericObjectPoolConfig config = new GenericObjectPoolConfig(); 6: config.setTestOnBorrow(url.getParameter("test.on.borrow", true)); 7: config.setTestOnReturn(url.getParameter("test.on.return", false)); 8: config.setTestWhileIdle(url.getParameter("test.while.idle", false)); 9: if (url.getParameter("max.idle", 0) > 0) 10: config.setMaxIdle(url.getParameter("max.idle", 0)); 11: if (url.getParameter("min.idle", 0) > 0) 12: config.setMinIdle(url.getParameter("min.idle", 0)); 13: if (url.getParameter("max.active", 0) > 0) 14: config.setMaxTotal(url.getParameter("max.active", 0)); 15: if (url.getParameter("max.total", 0) > 0) 16: config.setMaxTotal(url.getParameter("max.total", 0)); 17: if (url.getParameter("max.wait", 0) > 0) 18: config.setMaxWaitMillis(url.getParameter("max.wait", 0)); 19: if (url.getParameter("num.tests.per.eviction.run", 0) > 0) 20: config.setNumTestsPerEvictionRun(url.getParameter("num.tests.per.eviction.run", 0)); 21: if (url.getParameter("time.between.eviction.runs.millis", 0) > 0) 22: config.setTimeBetweenEvictionRunsMillis(url.getParameter("time.between.eviction.runs.millis", 0)); 23: if (url.getParameter("min.evictable.idle.time.millis", 0) > 0) 24: config.setMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0)); 25: 26: final JedisPool jedisPool = new JedisPool(config, url.getHost(), url.getPort(DEFAULT_PORT), 27: url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)); 28: 29: 30: final int expiry = url.getParameter("expiry", 0); 31: final String get = url.getParameter("get", "get"); 32: final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set"); 33: final String delete = url.getParameter("delete", Map.class.equals(type) ? "remove" : "delete"); 34: 35: 36: return new AbstractInvoker<T>(type, url) { 37: 38: @Override 39: protected Result doInvoke(Invocation invocation) { 40: Jedis resource = null; 41: try { 42: 43: resource = jedisPool.getResource(); 44: 45: if (get.equals(invocation.getMethodName())) { 46: if (invocation.getArguments().length != 1) { 47: throw new IllegalArgumentException("The redis get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); 48: } 49: 50: byte[] value = resource.get(String.valueOf(invocation.getArguments()[0]).getBytes()); 51: if (value == null) { 52: return new RpcResult(); 53: } 54: 55: ObjectInput oin = getSerialization(url).deserialize(url, new ByteArrayInputStream(value)); 56: 57: return new RpcResult(oin.readObject()); 58: 59: } else if (set.equals(invocation.getMethodName())) { 60: if (invocation.getArguments().length != 2) { 61: throw new IllegalArgumentException("The redis set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); 62: } 63: 64: byte[] key = String.valueOf(invocation.getArguments()[0]).getBytes(); 65: ByteArrayOutputStream output = new ByteArrayOutputStream(); 66: ObjectOutput value = getSerialization(url).serialize(url, output); 67: value.writeObject(invocation.getArguments()[1]); 68: 69: resource.set(key, output.toByteArray()); 70: if (expiry > 1000) { 71: resource.expire(key, expiry / 1000); 72: } 73: 74: return new RpcResult(); 75: } else if (delete.equals(invocation.getMethodName())) { 76: if (invocation.getArguments().length != 1) { 77: throw new IllegalArgumentException("The redis delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url); 78: } 79: 80: resource.del(String.valueOf(invocation.getArguments()[0]).getBytes()); 81: 82: return new RpcResult(); 83: } else { 84: throw new UnsupportedOperationException("Unsupported method " + invocation.getMethodName() + " in redis service."); 85: } 86: } catch (Throwable t) { 87: RpcException re = new RpcException("Failed to invoke redis service method. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url + ", cause: " + t.getMessage(), t); 88: if (t instanceof TimeoutException || t instanceof SocketTimeoutException) { 89: re.setCode(RpcException.TIMEOUT_EXCEPTION); 90: } else if (t instanceof JedisConnectionException || t instanceof IOException) { 91: re.setCode(RpcException.NETWORK_EXCEPTION); 92: } else if (t instanceof JedisDataException) { 93: re.setCode(RpcException.SERIALIZATION_EXCEPTION); 94: } 95: throw re; 96: } finally { 97: 98: if (resource != null) { 99: try { 100: jedisPool.returnResource(resource); 101: } catch (Throwable t) { 102: logger.warn("returnResource error: " + t.getMessage(), t); 103: } 104: } 105: } 106: } 107: 108: @Override 109: public void destroy() { 110: 111: super.destroy(); 112: 113: try { 114: jedisPool.destroy(); 115: } catch (Throwable e) { 116: logger.warn(e.getMessage(), e); 117: } 118: } 119: 120: }; 121: } catch (Throwable t) { 122: throw new RpcException("Failed to refer redis service. interface: " + type.getName() + ", url: " + url + ", cause: " + t.getMessage(), t); 123: } 124: }
|