1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
16 import java.io.BufferedInputStream;
18 import java.io.FileInputStream;
19 import java.io.IOException;
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 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 volatile static JedisPool jedisPool = null;
44 public static String propertiesName = "redis.properties";
45 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
49 // private constructor
53 private synchronized static JedisPool initialPool() throws IOException {
55 JedisPoolConfig config = new JedisPoolConfig();
56 config.setMaxTotal(50);
57 config.setMaxIdle(30);
58 config.setMaxWaitMillis(5000);
59 config.setTestOnBorrow(false);
60 config.setTestOnReturn(true);
62 URL urlPath = JedisUtil.class.getResource("/ext/redisConf/redis.properties");
63 if (urlPath != null) {
64 String propertiesPath = urlPath.getPath();
67 File propertiesFile = new File(propertiesPath);
69 if (propertiesFile.exists()) {
72 BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(propertiesPath));
73 ResourceBundle bundle = new PropertyResourceBundle(inputStream);
76 throw new IllegalArgumentException("[redis.properties] is not found!");
80 // Set up the connection pool basic information
81 String strHost = bundle.getString("redis.host");
82 if (StringUtils.isNotEmpty(strHost)) {
86 // redis port: first read from env
87 if (StringUtils.isNotBlank(System.getenv("APIGATEWAY_REDIS_PORT"))) {
88 port = Integer.parseInt(System.getenv("APIGATEWAY_REDIS_PORT"));
90 String strPort = bundle.getString("redis.port");
91 if (StringUtils.isNotEmpty(strPort)) {
92 port = Integer.parseInt(strPort);
97 String strTimeout = bundle.getString("redis.connectionTimeout");
98 if (StringUtils.isNotEmpty(strTimeout)) {
99 connectionTimeout = Integer.parseInt(strTimeout);
103 String strDbIndex = bundle.getString("redis.db_index");
104 if (StringUtils.isNotEmpty(strDbIndex)) {
105 DEFAULT_DB_INDEX = Integer.parseInt(strDbIndex);
108 String strMaxTotal = bundle.getString("redis.pool.maxTotal");
109 if (StringUtils.isNotEmpty(strMaxTotal)) {
110 config.setMaxTotal(Integer.parseInt(strMaxTotal));
113 String strMaxIdle = bundle.getString("redis.pool.maxIdle");
114 if (StringUtils.isNotEmpty(strMaxIdle)) {
115 config.setMaxIdle(Integer.parseInt(strMaxIdle));
118 String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis");
119 if (StringUtils.isNotEmpty(strMaxWaitMillis)) {
120 config.setMaxWaitMillis(Long.parseLong(strMaxWaitMillis));
123 String strTestOnBorrow = bundle.getString("redis.pool.testOnBorrow");
124 if (StringUtils.isNotEmpty(strTestOnBorrow)) {
125 config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
128 String strTestOnReturn = bundle.getString("redis.pool.testOnReturn");
129 if (StringUtils.isNotEmpty(strTestOnReturn)) {
130 config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
136 StringBuffer redisinfo = new StringBuffer();
137 redisinfo.append("------redis.properties------").append(LINE_SEPARATOR);
138 redisinfo.append("redis.host: ").append(host).append(":").append(port).append(LINE_SEPARATOR);
139 redisinfo.append("redis.connectionTimeout: ").append(connectionTimeout).append(LINE_SEPARATOR);
140 redisinfo.append("redis.pool.maxTotal: ").append(config.getMaxTotal()).append(LINE_SEPARATOR);
141 redisinfo.append("redis.pool.maxIdle: ").append(config.getMaxIdle()).append(LINE_SEPARATOR);
142 redisinfo.append("redis.pool.maxWaitMillis: ").append(config.getMaxWaitMillis()).append(LINE_SEPARATOR);
143 redisinfo.append("redis.pool.testOnBorrow: ").append(config.getTestOnBorrow()).append(LINE_SEPARATOR);
144 redisinfo.append("redis.pool.testOnReturn: ").append(config.getTestOnReturn()).append(LINE_SEPARATOR);
147 LOGGER.info(redisinfo.toString());
148 return new JedisPool(config, host, port, connectionTimeout);
153 * From the connection pool to obtain jedis instance, use the default database index number 0
158 public static Jedis borrowJedisInstance() throws Exception {
159 return borrowJedisInstance(DEFAULT_DB_INDEX);
163 * From the connection pool to obtain jedis instance, using the specified database index number
169 public static Jedis borrowJedisInstance(final int dbIndex) throws Exception {
170 if (jedisPool == null) {
171 synchronized (JedisUtil.class) {
172 if (jedisPool == null) {
173 jedisPool = initialPool();
177 Jedis resource = jedisPool.getResource();
179 if (resource == null) {
180 throw new Exception("fetch from jedis pool failed,null object!");
183 resource.select(dbIndex);
189 * returned to the pool jedis instance
193 public static void returnJedisInstance(final Jedis jedis) {