2 * Copyright 2016 ZTE, Inc. and others.
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.
17 package org.onap.msb.apiroute.wrapper.util;
19 import java.io.BufferedInputStream;
21 import java.io.FileInputStream;
22 import java.io.IOException;
24 import java.util.PropertyResourceBundle;
25 import java.util.ResourceBundle;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import redis.clients.jedis.Jedis;
33 import redis.clients.jedis.JedisPool;
34 import redis.clients.jedis.JedisPoolConfig;
38 public class JedisUtil {
39 private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class);
40 private static String host = "127.0.0.1";
41 private static int port = 6379;
42 private static int connectionTimeout = 2000;
43 private static int DEFAULT_DB_INDEX = 0;
45 private volatile static JedisPool jedisPool = null;
48 public static String propertiesName = "redis.properties";
49 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
53 // private constructor
57 private synchronized static JedisPool initialPool() throws IOException {
59 JedisPoolConfig config = new JedisPoolConfig();
60 config.setMaxTotal(50);
61 config.setMaxIdle(30);
62 config.setMaxWaitMillis(5000);
63 config.setTestOnBorrow(false);
64 config.setTestOnReturn(true);
66 URL urlPath = JedisUtil.class.getResource("/ext/redisConf/redis.properties");
67 if (urlPath != null) {
68 String propertiesPath = urlPath.getPath();
71 File propertiesFile = new File(propertiesPath);
73 if (propertiesFile.exists()) {
76 BufferedInputStream inputStream =
77 new BufferedInputStream(new FileInputStream(propertiesPath));
78 ResourceBundle bundle = new PropertyResourceBundle(inputStream);
81 throw new IllegalArgumentException("[redis.properties] is not found!");
85 // Set up the connection pool basic information
86 String strHost = bundle.getString("redis.host");
87 if (StringUtils.isNotEmpty(strHost)) {
91 // redis port: first read from env
92 if (StringUtils.isNotBlank(System.getenv("APIGATEWAY_REDIS_PORT"))) {
93 port = Integer.parseInt(System.getenv("APIGATEWAY_REDIS_PORT"));
95 String strPort = bundle.getString("redis.port");
96 if (StringUtils.isNotEmpty(strPort)) {
97 port = Integer.parseInt(strPort);
102 String strTimeout = bundle.getString("redis.connectionTimeout");
103 if (StringUtils.isNotEmpty(strTimeout)) {
104 connectionTimeout = Integer.parseInt(strTimeout);
108 String strDbIndex = bundle.getString("redis.db_index");
109 if (StringUtils.isNotEmpty(strDbIndex)) {
110 DEFAULT_DB_INDEX = Integer.parseInt(strDbIndex);
113 String strMaxTotal = bundle.getString("redis.pool.maxTotal");
114 if (StringUtils.isNotEmpty(strMaxTotal)) {
115 config.setMaxTotal(Integer.parseInt(strMaxTotal));
118 String strMaxIdle = bundle.getString("redis.pool.maxIdle");
119 if (StringUtils.isNotEmpty(strMaxIdle)) {
120 config.setMaxIdle(Integer.parseInt(strMaxIdle));
123 String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis");
124 if (StringUtils.isNotEmpty(strMaxWaitMillis)) {
125 config.setMaxWaitMillis(Long.parseLong(strMaxWaitMillis));
128 String strTestOnBorrow = bundle.getString("redis.pool.testOnBorrow");
129 if (StringUtils.isNotEmpty(strTestOnBorrow)) {
130 config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
133 String strTestOnReturn = bundle.getString("redis.pool.testOnReturn");
134 if (StringUtils.isNotEmpty(strTestOnReturn)) {
135 config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
141 StringBuffer redisinfo = new StringBuffer();
142 redisinfo.append("------redis.properties------").append(LINE_SEPARATOR);
143 redisinfo.append("redis.host: ").append(host).append(":").append(port).append(LINE_SEPARATOR);
144 redisinfo.append("redis.connectionTimeout: ").append(connectionTimeout).append(LINE_SEPARATOR);
145 redisinfo.append("redis.pool.maxTotal: ").append(config.getMaxTotal()).append(LINE_SEPARATOR);
146 redisinfo.append("redis.pool.maxIdle: ").append(config.getMaxIdle()).append(LINE_SEPARATOR);
147 redisinfo.append("redis.pool.maxWaitMillis: ").append(config.getMaxWaitMillis())
148 .append(LINE_SEPARATOR);
149 redisinfo.append("redis.pool.testOnBorrow: ").append(config.getTestOnBorrow())
150 .append(LINE_SEPARATOR);
151 redisinfo.append("redis.pool.testOnReturn: ").append(config.getTestOnReturn())
152 .append(LINE_SEPARATOR);
155 LOGGER.info(redisinfo.toString());
156 return new JedisPool(config, host, port, connectionTimeout);
161 * From the connection pool to obtain jedis instance, use the default database index number 0
166 public static Jedis borrowJedisInstance() throws Exception {
167 return borrowJedisInstance(DEFAULT_DB_INDEX);
171 * From the connection pool to obtain jedis instance, using the specified database index number
177 public static Jedis borrowJedisInstance(final int dbIndex) throws Exception {
178 if (jedisPool == null) {
179 synchronized (JedisUtil.class) {
180 if (jedisPool == null) {
181 jedisPool = initialPool();
185 Jedis resource = jedisPool.getResource();
187 if (resource == null) {
188 throw new Exception("fetch from jedis pool failed,null object!");
191 resource.select(dbIndex);
197 * returned to the pool jedis instance
201 public static void returnJedisInstance(final Jedis jedis) {