+++ /dev/null
-/**\r
-* Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
-*\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
-\r
-package org.openo.msb.wrapper.util;\r
-\r
-import java.io.BufferedInputStream;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.util.PropertyResourceBundle;\r
-import java.util.ResourceBundle;\r
-\r
-import org.apache.commons.lang3.StringUtils;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import redis.clients.jedis.Jedis;\r
-import redis.clients.jedis.JedisPool;\r
-import redis.clients.jedis.JedisPoolConfig;\r
-\r
-\r
-\r
-public final class JedisUtil {\r
- private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class);\r
- private static String host = "127.0.0.1";\r
- private static int port = 6379;\r
- private static int connectionTimeout = 2000;\r
- private static int DEFAULT_DB_INDEX = 0;\r
-\r
- private static JedisPool jedisPool = null;\r
- \r
- public static String serverIp="127.0.0.1";\r
- \r
- public static int serverPort=10080;\r
- \r
- public static String propertiesName="redis.properties";\r
- \r
- public static String propertiesPath="";\r
-\r
- \r
- \r
-public static void main(String[] args) {\r
- \r
-}\r
-\r
- private JedisUtil() {\r
- // private constructor\r
-\r
- }\r
- \r
- private static void initialPool() {\r
- try {\r
- JedisPoolConfig config = new JedisPoolConfig();\r
- \r
-// String pathtest=JedisUtil.class.getResource("").getPath();\r
-// String path ="/"+ pathtest.substring(0, pathtest.indexOf("assembly")).replace("file:/", "") +"assembly/"+defaultWorkspace;\r
- \r
- File propertiesFile = new File(propertiesPath);\r
- \r
- if (propertiesFile.exists()) {\r
- \r
- \r
- BufferedInputStream inputStream =new BufferedInputStream(new FileInputStream(propertiesPath)); \r
- ResourceBundle bundle =new PropertyResourceBundle(inputStream);\r
- \r
- if (bundle == null) {\r
- throw new IllegalArgumentException(\r
- "[redis.properties] is not found!");\r
- }\r
- \r
-\r
- // 设置连接池基本信息\r
- String strHost = bundle.getString("redis.host");\r
- if(StringUtils.isNotEmpty(strHost)){\r
- host = strHost;\r
- }\r
- String strPort = bundle.getString("redis.port");\r
- if(StringUtils.isNotEmpty(strPort)){\r
- port = Integer.valueOf(strPort);\r
- }\r
- \r
- \r
- String strTimeout = bundle.getString("redis.connectionTimeout");\r
- if (StringUtils.isNotEmpty(strTimeout) ){\r
- connectionTimeout = Integer.valueOf(strTimeout);\r
- }\r
- \r
-// serverIp=bundle.getString("server.ip");\r
- serverPort=Integer.valueOf(bundle.getString("server.port"));\r
- \r
- String strDbIndex = bundle.getString("redis.db_index");\r
- if (StringUtils.isNotEmpty(strDbIndex)) {\r
- DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);\r
- }\r
-\r
- String strMaxTotal = bundle.getString("redis.pool.maxTotal");\r
- if (StringUtils.isNotEmpty(strMaxTotal)) {\r
- config.setMaxTotal(Integer.valueOf(strMaxTotal));\r
- }\r
-\r
- String strMaxIdle = bundle.getString("redis.pool.maxIdle");\r
- if (StringUtils.isNotEmpty(strMaxIdle)) {\r
- config.setMaxIdle(Integer.valueOf(strMaxIdle));\r
- }\r
-\r
- String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis");\r
- if (StringUtils.isNotEmpty(strMaxWaitMillis)) {\r
- config.setMaxWaitMillis(Long.valueOf(strMaxWaitMillis));\r
- }\r
-\r
- String strTestOnBorrow = bundle\r
- .getString("redis.pool.testOnBorrow");\r
- if (StringUtils.isNotEmpty(strTestOnBorrow)) {\r
- config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));\r
- }\r
-\r
- String strTestOnReturn = bundle\r
- .getString("redis.pool.testOnReturn");\r
- if (StringUtils.isNotEmpty(strTestOnReturn)) {\r
- config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));\r
- }\r
-\r
- }\r
- \r
- LOGGER.info("Redis server info: " + host + ":" + port);\r
- LOGGER.info("nginx server info: " + serverIp + ":" + serverPort);\r
- \r
- \r
-// ResourceBundle bundle = ResourceBundle.getBundle("conf.redis");\r
- \r
- jedisPool = new JedisPool(config, host, port, connectionTimeout);\r
- } catch (Exception e) {\r
- LOGGER.error("Initiate Jedis pool failed!", e);\r
- }\r
- }\r
- /**\r
- * From the connection pool to obtain jedis instance, use the default database index number 0\r
- * @return\r
- */\r
- public synchronized static Jedis borrowJedisInstance() {\r
- if (jedisPool == null) {\r
- initialPool();\r
- }\r
- try {\r
- if (jedisPool != null) {\r
- Jedis resource = jedisPool.getResource();\r
- resource.select(DEFAULT_DB_INDEX);\r
- return resource;\r
- } else {\r
- return null;\r
- }\r
- } catch (Exception e) {\r
- LOGGER.error("Get Jedis from pool failed!", e);\r
- return null;\r
- }\r
- }\r
- /**\r
- * From the connection pool to obtain jedis instance, using the specified database index number\r
- * @return\r
- */\r
- public synchronized static Jedis borrowJedisInstance(final int dbIndex) {\r
- if (jedisPool == null) {\r
- initialPool();\r
- }\r
- try {\r
- if (jedisPool != null) {\r
- Jedis resource = jedisPool.getResource();\r
- resource.select(dbIndex);\r
- return resource;\r
- } else {\r
- return null;\r
- }\r
- } catch (Exception e) {\r
- LOGGER.error("Get Jedis from pool failed!", e);\r
- return null;\r
- }\r
- }\r
- \r
- /**\r
- * returned to the pool jedis instance\r
- * @param jedis\r
- */\r
- public static void returnJedisInstance(final Jedis jedis) {\r
- if (jedis != null) {\r
- jedis.close();\r
- }\r
- }\r
- \r
- \r
- /** \r
- * @Title getJedis \r
- * @Description TODO(From the connection pool to obtain jedis instance) \r
- * @throws Exception \r
- * @return Jedis \r
- */\r
- public static Jedis getJedis() throws Exception{\r
- \r
- \r
- Jedis jedis = borrowJedisInstance();\r
- if (jedis == null) {\r
- throw new Exception("fetch from jedis pool failed,null object!");\r
- \r
- }\r
- \r
- return jedis;\r
- \r
- }\r
-\r
-}
\ No newline at end of file