2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.msb.wrapper.util;
\r
19 import java.io.BufferedInputStream;
\r
20 import java.io.File;
\r
21 import java.io.FileInputStream;
\r
22 import java.util.PropertyResourceBundle;
\r
23 import java.util.ResourceBundle;
\r
25 import org.apache.commons.lang3.StringUtils;
\r
26 import org.slf4j.Logger;
\r
27 import org.slf4j.LoggerFactory;
\r
29 import redis.clients.jedis.Jedis;
\r
30 import redis.clients.jedis.JedisPool;
\r
31 import redis.clients.jedis.JedisPoolConfig;
\r
35 public final class JedisUtil {
\r
36 private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class);
\r
37 private static String host = "127.0.0.1";
\r
38 private static int port = 6379;
\r
39 private static int connectionTimeout = 2000;
\r
40 private static int DEFAULT_DB_INDEX = 0;
\r
42 private static JedisPool jedisPool = null;
\r
44 public static String serverIp="127.0.0.1";
\r
46 public static int serverPort=10080;
\r
48 public static String propertiesName="redis.properties";
\r
50 public static String propertiesPath="";
\r
54 public static void main(String[] args) {
\r
58 private JedisUtil() {
\r
59 // private constructor
\r
63 private static void initialPool() {
\r
65 JedisPoolConfig config = new JedisPoolConfig();
\r
67 // String pathtest=JedisUtil.class.getResource("").getPath();
\r
68 // String path ="/"+ pathtest.substring(0, pathtest.indexOf("assembly")).replace("file:/", "") +"assembly/"+defaultWorkspace;
\r
70 File propertiesFile = new File(propertiesPath);
\r
72 if (propertiesFile.exists()) {
\r
75 BufferedInputStream inputStream =new BufferedInputStream(new FileInputStream(propertiesPath));
\r
76 ResourceBundle bundle =new PropertyResourceBundle(inputStream);
\r
78 if (bundle == null) {
\r
79 throw new IllegalArgumentException(
\r
80 "[redis.properties] is not found!");
\r
85 String strHost = bundle.getString("redis.host");
\r
86 if(StringUtils.isNotEmpty(strHost)){
\r
89 String strPort = bundle.getString("redis.port");
\r
90 if(StringUtils.isNotEmpty(strPort)){
\r
91 port = Integer.valueOf(strPort);
\r
95 String strTimeout = bundle.getString("redis.connectionTimeout");
\r
96 if (StringUtils.isNotEmpty(strTimeout) ){
\r
97 connectionTimeout = Integer.valueOf(strTimeout);
\r
100 // serverIp=bundle.getString("server.ip");
\r
101 serverPort=Integer.valueOf(bundle.getString("server.port"));
\r
103 String strDbIndex = bundle.getString("redis.db_index");
\r
104 if (StringUtils.isNotEmpty(strDbIndex)) {
\r
105 DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);
\r
108 String strMaxTotal = bundle.getString("redis.pool.maxTotal");
\r
109 if (StringUtils.isNotEmpty(strMaxTotal)) {
\r
110 config.setMaxTotal(Integer.valueOf(strMaxTotal));
\r
113 String strMaxIdle = bundle.getString("redis.pool.maxIdle");
\r
114 if (StringUtils.isNotEmpty(strMaxIdle)) {
\r
115 config.setMaxIdle(Integer.valueOf(strMaxIdle));
\r
118 String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis");
\r
119 if (StringUtils.isNotEmpty(strMaxWaitMillis)) {
\r
120 config.setMaxWaitMillis(Long.valueOf(strMaxWaitMillis));
\r
123 String strTestOnBorrow = bundle
\r
124 .getString("redis.pool.testOnBorrow");
\r
125 if (StringUtils.isNotEmpty(strTestOnBorrow)) {
\r
126 config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
\r
129 String strTestOnReturn = bundle
\r
130 .getString("redis.pool.testOnReturn");
\r
131 if (StringUtils.isNotEmpty(strTestOnReturn)) {
\r
132 config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
\r
137 LOGGER.info("Redis server info: " + host + ":" + port);
\r
138 LOGGER.info("nginx server info: " + serverIp + ":" + serverPort);
\r
141 // ResourceBundle bundle = ResourceBundle.getBundle("conf.redis");
\r
143 jedisPool = new JedisPool(config, host, port, connectionTimeout);
\r
144 } catch (Exception e) {
\r
145 LOGGER.error("Initiate Jedis pool failed!", e);
\r
149 * From the connection pool to obtain jedis instance, use the default database index number 0
\r
152 public synchronized static Jedis borrowJedisInstance() {
\r
153 if (jedisPool == null) {
\r
157 if (jedisPool != null) {
\r
158 Jedis resource = jedisPool.getResource();
\r
159 resource.select(DEFAULT_DB_INDEX);
\r
164 } catch (Exception e) {
\r
165 LOGGER.error("Get Jedis from pool failed!", e);
\r
170 * From the connection pool to obtain jedis instance, using the specified database index number
\r
173 public synchronized static Jedis borrowJedisInstance(final int dbIndex) {
\r
174 if (jedisPool == null) {
\r
178 if (jedisPool != null) {
\r
179 Jedis resource = jedisPool.getResource();
\r
180 resource.select(dbIndex);
\r
185 } catch (Exception e) {
\r
186 LOGGER.error("Get Jedis from pool failed!", e);
\r
192 * returned to the pool jedis instance
\r
195 public static void returnJedisInstance(final Jedis jedis) {
\r
196 if (jedis != null) {
\r
204 * @Description TODO(From the connection pool to obtain jedis instance)
\r
205 * @throws Exception
\r
208 public static Jedis getJedis() throws Exception{
\r
211 Jedis jedis = borrowJedisInstance();
\r
212 if (jedis == null) {
\r
213 throw new Exception("fetch from jedis pool failed,null object!");
\r