2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.msb.wrapper.util;
18 import java.io.BufferedInputStream;
20 import java.io.FileInputStream;
21 import java.util.PropertyResourceBundle;
22 import java.util.ResourceBundle;
24 import org.apache.commons.lang3.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import redis.clients.jedis.Jedis;
29 import redis.clients.jedis.JedisPool;
30 import redis.clients.jedis.JedisPoolConfig;
34 public final class JedisUtil {
35 private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class);
36 private static String host = "127.0.0.1";
37 private static int port = 6379;
38 private static int connectionTimeout = 2000;
39 private static int DEFAULT_DB_INDEX = 0;
41 private static JedisPool jedisPool = null;
43 public static String serverIp="127.0.0.1";
45 public static int serverPort=10080;
47 public static String propertiesName="redis.properties";
49 public static String propertiesPath="";
53 public static void main(String[] args) {
58 // private constructor
62 private static void initialPool() {
64 JedisPoolConfig config = new JedisPoolConfig();
66 // String pathtest=JedisUtil.class.getResource("").getPath();
67 // String path ="/"+ pathtest.substring(0, pathtest.indexOf("assembly")).replace("file:/", "") +"assembly/"+defaultWorkspace;
69 File propertiesFile = new File(propertiesPath);
71 if (propertiesFile.exists()) {
74 BufferedInputStream inputStream =new BufferedInputStream(new FileInputStream(propertiesPath));
75 ResourceBundle bundle =new PropertyResourceBundle(inputStream);
78 throw new IllegalArgumentException(
79 "[redis.properties] is not found!");
84 String strHost = bundle.getString("redis.host");
85 if(StringUtils.isNotEmpty(strHost)){
88 String strPort = bundle.getString("redis.port");
89 if(StringUtils.isNotEmpty(strPort)){
90 port = Integer.valueOf(strPort);
94 String strTimeout = bundle.getString("redis.connectionTimeout");
95 if (StringUtils.isNotEmpty(strTimeout) ){
96 connectionTimeout = Integer.valueOf(strTimeout);
99 // serverIp=bundle.getString("server.ip");
100 serverPort=Integer.valueOf(bundle.getString("server.port"));
102 String strDbIndex = bundle.getString("redis.db_index");
103 if (StringUtils.isNotEmpty(strDbIndex)) {
104 DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);
107 String strMaxTotal = bundle.getString("redis.pool.maxTotal");
108 if (StringUtils.isNotEmpty(strMaxTotal)) {
109 config.setMaxTotal(Integer.valueOf(strMaxTotal));
112 String strMaxIdle = bundle.getString("redis.pool.maxIdle");
113 if (StringUtils.isNotEmpty(strMaxIdle)) {
114 config.setMaxIdle(Integer.valueOf(strMaxIdle));
117 String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis");
118 if (StringUtils.isNotEmpty(strMaxWaitMillis)) {
119 config.setMaxWaitMillis(Long.valueOf(strMaxWaitMillis));
122 String strTestOnBorrow = bundle
123 .getString("redis.pool.testOnBorrow");
124 if (StringUtils.isNotEmpty(strTestOnBorrow)) {
125 config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
128 String strTestOnReturn = bundle
129 .getString("redis.pool.testOnReturn");
130 if (StringUtils.isNotEmpty(strTestOnReturn)) {
131 config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
136 LOGGER.info("Redis server info: " + host + ":" + port);
137 LOGGER.info("nginx server info: " + serverIp + ":" + serverPort);
140 // ResourceBundle bundle = ResourceBundle.getBundle("conf.redis");
142 jedisPool = new JedisPool(config, host, port, connectionTimeout);
143 } catch (Exception e) {
144 LOGGER.error("Initiate Jedis pool failed!", e);
148 * From the connection pool to obtain jedis instance, use the default database index number 0
151 public synchronized static Jedis borrowJedisInstance() {
152 if (jedisPool == null) {
156 if (jedisPool != null) {
157 Jedis resource = jedisPool.getResource();
158 resource.select(DEFAULT_DB_INDEX);
163 } catch (Exception e) {
164 LOGGER.error("Get Jedis from pool failed!", e);
169 * From the connection pool to obtain jedis instance, using the specified database index number
172 public synchronized static Jedis borrowJedisInstance(final int dbIndex) {
173 if (jedisPool == null) {
177 if (jedisPool != null) {
178 Jedis resource = jedisPool.getResource();
179 resource.select(dbIndex);
184 } catch (Exception e) {
185 LOGGER.error("Get Jedis from pool failed!", e);
191 * returned to the pool jedis instance
194 public static void returnJedisInstance(final Jedis jedis) {
203 * @Description TODO(From the connection pool to obtain jedis instance)
207 public static Jedis getJedis() throws Exception{
210 Jedis jedis = borrowJedisInstance();
212 throw new Exception("fetch from jedis pool failed,null object!");