msb protocol synch change
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / util / JedisUtil.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.msb.wrapper.util;
17
18 import java.io.BufferedInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.util.PropertyResourceBundle;
22 import java.util.ResourceBundle;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import redis.clients.jedis.Jedis;
29 import redis.clients.jedis.JedisPool;
30 import redis.clients.jedis.JedisPoolConfig;
31
32
33
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;
40
41         private static JedisPool jedisPool = null;
42         
43         public static String serverIp="127.0.0.1";
44         
45         public static int serverPort=10080;
46         
47         public static String propertiesName="redis.properties";
48             
49         public static String propertiesPath="";
50
51         
52         
53 public static void main(String[] args) {
54         
55 }
56
57         private JedisUtil() {
58                 // private constructor
59
60         }
61         
62         private static void initialPool() {
63                 try {
64                     JedisPoolConfig config = new JedisPoolConfig();
65                         
66 //                      String pathtest=JedisUtil.class.getResource("").getPath();
67 //                      String path ="/"+ pathtest.substring(0, pathtest.indexOf("assembly")).replace("file:/", "") +"assembly/"+defaultWorkspace;
68                 
69                         File propertiesFile = new File(propertiesPath);
70                         
71                            if (propertiesFile.exists()) {
72                                   
73                                  
74                                  BufferedInputStream inputStream =new BufferedInputStream(new FileInputStream(propertiesPath));  
75                                  ResourceBundle bundle =new PropertyResourceBundle(inputStream);
76                                  
77                                  if (bundle == null) {
78                                                 throw new IllegalArgumentException(
79                                                                 "[redis.properties] is not found!");
80                                         }
81                                  
82
83                                         
84                                         String strHost = bundle.getString("redis.host");
85                                         if(StringUtils.isNotEmpty(strHost)){
86                                                 host = strHost;
87                                         }
88                                         String strPort = bundle.getString("redis.port");
89                                         if(StringUtils.isNotEmpty(strPort)){
90                                                 port = Integer.valueOf(strPort);
91                                         }
92                                 
93                                         
94                                         String strTimeout = bundle.getString("redis.connectionTimeout");
95                                         if (StringUtils.isNotEmpty(strTimeout) ){
96                                                 connectionTimeout = Integer.valueOf(strTimeout);
97                                         }
98                                         
99 //                                      serverIp=bundle.getString("server.ip");
100                                         serverPort=Integer.valueOf(bundle.getString("server.port"));
101                                         
102                                         String strDbIndex = bundle.getString("redis.db_index");
103                                         if (StringUtils.isNotEmpty(strDbIndex)) {
104                                                 DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);
105                                         }
106
107                                         String strMaxTotal = bundle.getString("redis.pool.maxTotal");
108                                         if (StringUtils.isNotEmpty(strMaxTotal)) {
109                                                 config.setMaxTotal(Integer.valueOf(strMaxTotal));
110                                         }
111
112                                         String strMaxIdle = bundle.getString("redis.pool.maxIdle");
113                                         if (StringUtils.isNotEmpty(strMaxIdle)) {
114                                                 config.setMaxIdle(Integer.valueOf(strMaxIdle));
115                                         }
116
117                                         String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis");
118                                         if (StringUtils.isNotEmpty(strMaxWaitMillis)) {
119                                                 config.setMaxWaitMillis(Long.valueOf(strMaxWaitMillis));
120                                         }
121
122                                         String strTestOnBorrow = bundle
123                                                         .getString("redis.pool.testOnBorrow");
124                                         if (StringUtils.isNotEmpty(strTestOnBorrow)) {
125                                                 config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
126                                         }
127
128                                         String strTestOnReturn = bundle
129                                                         .getString("redis.pool.testOnReturn");
130                                         if (StringUtils.isNotEmpty(strTestOnReturn)) {
131                                                 config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
132                                         }
133
134                            }
135                         
136                                 LOGGER.info("Redis server info: " + host + ":" + port);
137                                 LOGGER.info("nginx server info: " + serverIp + ":" + serverPort);
138         
139                         
140 //                      ResourceBundle bundle = ResourceBundle.getBundle("conf.redis");
141         
142                         jedisPool = new JedisPool(config, host, port, connectionTimeout);
143                 } catch (Exception e) {
144                         LOGGER.error("Initiate Jedis pool failed!", e);
145                 }
146         }
147         /**
148          * From the connection pool to obtain jedis instance, use the default database index number 0
149          * @return
150          */
151         public synchronized static Jedis borrowJedisInstance() {
152                 if (jedisPool == null) {
153                         initialPool();
154                 }
155                 try {
156                         if (jedisPool != null) {
157                                 Jedis resource = jedisPool.getResource();
158                                 resource.select(DEFAULT_DB_INDEX);
159                                 return resource;
160                         } else {
161                                 return null;
162                         }
163                 } catch (Exception e) {
164                         LOGGER.error("Get Jedis from pool failed!", e);
165                         return null;
166                 }
167         }
168         /**
169          * From the connection pool to obtain jedis instance, using the specified database index number
170          * @return
171          */
172         public synchronized static Jedis borrowJedisInstance(final int dbIndex) {
173                 if (jedisPool == null) {
174                         initialPool();
175                 }
176                 try {
177                         if (jedisPool != null) {
178                                 Jedis resource = jedisPool.getResource();
179                                 resource.select(dbIndex);
180                                 return resource;
181                         } else {
182                                 return null;
183                         }
184                 } catch (Exception e) {
185                         LOGGER.error("Get Jedis from pool failed!", e);
186                         return null;
187                 }
188         }
189         
190         /**
191          *  returned to the pool jedis instance
192          * @param jedis
193          */
194         public static void returnJedisInstance(final Jedis jedis) {
195                 if (jedis != null) {
196                         jedis.close();
197                 }
198         }
199         
200         
201         /** 
202         * @Title getJedis 
203         * @Description TODO(From the connection pool to obtain jedis instance) 
204         * @throws Exception      
205         * @return Jedis    
206         */
207         public static Jedis getJedis() throws Exception{
208              
209   
210                 Jedis jedis = borrowJedisInstance();
211             if (jedis == null) {
212                 throw new Exception("fetch from jedis pool failed,null object!");
213                 
214             }
215             
216             return jedis;
217       
218         }
219
220 }