Skip to content

Commit f45e1b2

Browse files
committed
application config system property support changes
1 parent b73c1b7 commit f45e1b2

19 files changed

+518
-210
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>tomcat-cluster-redis-session-manager</groupId>
66
<artifactId>tomcat-cluster-redis-session-manager</artifactId>
7-
<version>3.0.3</version>
7+
<version>3.0.4</version>
88
<packaging>jar</packaging>
99

1010
<name>tomcat-cluster-redis-session-manager</name>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package tomcat.request.session.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/** author: Ranjith Manickam @ 5 Feb' 2020 */
9+
@Target(ElementType.FIELD)
10+
@Retention(RetentionPolicy.RUNTIME)
11+
public @interface Property {
12+
String name() default "";
13+
14+
String defaultValue() default "";
15+
16+
PropertyType type() default PropertyType.STRING;
17+
18+
enum PropertyType {
19+
STRING,
20+
BOOLEAN,
21+
INTEGER,
22+
LONG
23+
}
24+
}

src/main/java/tomcat/request/session/SessionConstants.java renamed to src/main/java/tomcat/request/session/constant/SessionConstants.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
package tomcat.request.session;
1+
package tomcat.request.session.constant;
22

33
/** author: Ranjith Manickam @ 12 Jul' 2018 */
44
public interface SessionConstants {
55
byte[] NULL_SESSION = "null".getBytes();
6-
String CATALINA_BASE = "catalina.base";
7-
String CONF = "conf";
8-
String SESSION_PERSISTENT_POLICIES = "session.persistent.policies";
96

107
enum SessionPolicy {
118
DEFAULT, SAVE_ON_CHANGE, ALWAYS_SAVE_AFTER_REQUEST;

src/main/java/tomcat/request/session/data/cache/DataCacheConstants.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/java/tomcat/request/session/data/cache/DataCacheFactory.java

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,24 @@
22

33
import tomcat.request.session.data.cache.impl.StandardDataCache;
44
import tomcat.request.session.data.cache.impl.redis.RedisCache;
5-
6-
import java.util.Properties;
5+
import tomcat.request.session.model.Config;
76

87
/** author: Ranjith Manickam @ 3 Dec' 2018 */
98
public class DataCacheFactory {
109

11-
private final Properties properties;
10+
private final Config config;
1211
private final int sessionExpiryTime;
1312

14-
public DataCacheFactory(Properties properties, int sessionExpiryTime) {
15-
this.properties = properties;
13+
public DataCacheFactory(Config config, int sessionExpiryTime) {
14+
this.config = config;
1615
this.sessionExpiryTime = sessionExpiryTime;
1716
}
1817

1918
/** To get data cache. */
2019
public DataCache getDataCache() {
21-
if (Boolean.parseBoolean(getProperty(this.properties, DataCacheConstants.LB_STICKY_SESSION_ENABLED))) {
22-
return new StandardDataCache(this.properties, this.sessionExpiryTime);
20+
if (this.config.getLbStickySessionEnabled()) {
21+
return new StandardDataCache(this.config, this.sessionExpiryTime);
2322
}
24-
return new RedisCache(this.properties);
25-
}
26-
27-
/**
28-
* To get property with the specified key in this properties list.
29-
*
30-
* @param properties - properties list.
31-
* @param key - search key.
32-
* @return - Returns the property value.
33-
*/
34-
public static String getProperty(Properties properties, String key) {
35-
return getProperty(properties, key, null);
36-
}
37-
38-
/**
39-
* To get property with the specified key in this properties list.
40-
*
41-
* @param properties - properties list.
42-
* @param key - search key.
43-
* @param defaultValue - default value.
44-
* @return - - Returns the property value.
45-
*/
46-
public static String getProperty(Properties properties, String key, String defaultValue) {
47-
String[] keyValue = key.split(":");
48-
return properties.getProperty(keyValue[0], (keyValue.length > 1 && defaultValue == null) ? keyValue[1] : defaultValue);
23+
return new RedisCache(this.config);
4924
}
5025
}

src/main/java/tomcat/request/session/data/cache/impl/StandardDataCache.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55
import tomcat.request.session.data.cache.DataCache;
6-
import tomcat.request.session.data.cache.DataCacheConstants;
7-
import tomcat.request.session.data.cache.DataCacheFactory;
86
import tomcat.request.session.data.cache.impl.redis.RedisCache;
7+
import tomcat.request.session.model.Config;
98

109
import java.io.Serializable;
1110
import java.util.Date;
1211
import java.util.Map;
13-
import java.util.Properties;
1412
import java.util.concurrent.ConcurrentHashMap;
1513
import java.util.concurrent.Executor;
1614
import java.util.concurrent.Executors;
@@ -32,15 +30,15 @@ public class StandardDataCache extends RedisCache {
3230
private final Executor expiryJobExecutor;
3331
private final Executor dataSyncJobExecutor;
3432

35-
public StandardDataCache(Properties properties, int sessionExpiryTime) {
36-
super(properties);
33+
public StandardDataCache(Config config, int sessionExpiryTime) {
34+
super(config);
3735
this.sessionExpiryTime = sessionExpiryTime;
3836
this.sessionData = new ConcurrentHashMap<>();
3937
this.expiryJob = new Date().getTime();
4038
this.dataSyncJob = new Date().getTime();
4139
this.processDataSync = false;
42-
this.expiryJobTriggerInterval = TimeUnit.MINUTES.toMillis(Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.SESSION_EXPIRY_JOB_INTERVAL)));
43-
this.dataSyncJobTriggerInterval = TimeUnit.MINUTES.toMillis(Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.SESSION_DATA_SYNC_JOB_INTERVAL)));
40+
this.expiryJobTriggerInterval = TimeUnit.MINUTES.toMillis(config.getRedisSessionExpiryJobInterval());
41+
this.dataSyncJobTriggerInterval = TimeUnit.MINUTES.toMillis(config.getRedisSessionDataSyncJobInterval());
4442
this.expiryJobExecutor = Executors.newSingleThreadExecutor();
4543
this.dataSyncJobExecutor = Executors.newSingleThreadExecutor();
4644
}

src/main/java/tomcat/request/session/data/cache/impl/redis/RedisCache.java

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22

33
import redis.clients.jedis.HostAndPort;
44
import redis.clients.jedis.JedisPoolConfig;
5-
import redis.clients.jedis.Protocol;
65
import tomcat.request.session.data.cache.DataCache;
7-
import tomcat.request.session.data.cache.DataCacheConstants;
8-
import tomcat.request.session.data.cache.DataCacheConstants.RedisConfigType;
9-
import tomcat.request.session.data.cache.DataCacheFactory;
6+
import tomcat.request.session.model.Config;
7+
import tomcat.request.session.model.Config.RedisConfigType;
108

119
import java.util.ArrayList;
1210
import java.util.Collection;
1311
import java.util.HashSet;
1412
import java.util.List;
15-
import java.util.Properties;
1613
import java.util.Set;
1714

1815
/** author: Ranjith Manickam @ 12 Jul' 2018 */
1916
public class RedisCache implements DataCache {
2017

2118
private DataCache dataCache;
2219

23-
public RedisCache(Properties properties) {
24-
initialize(properties);
20+
public RedisCache(Config config) {
21+
initialize(config);
2522
}
2623

2724
/** {@inheritDoc} */
@@ -54,73 +51,51 @@ public Long delete(String key) {
5451
return dataCache.delete(key);
5552
}
5653

57-
private void initialize(Properties properties) {
58-
RedisConfigType configType;
59-
if (Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_CLUSTER_ENABLED))) {
60-
configType = RedisConfigType.CLUSTER;
61-
} else if (Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_SENTINEL_ENABLED))) {
62-
configType = RedisConfigType.SENTINEL;
63-
} else {
64-
configType = RedisConfigType.DEFAULT;
65-
}
66-
67-
String hosts = DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_HOSTS, String.format("%s:%s", Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT));
68-
Collection<?> nodes = getJedisNodes(hosts, configType);
69-
70-
String password = DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_PASSWORD);
71-
password = (password != null && !password.isEmpty()) ? password : null;
72-
73-
int database = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_DATABASE));
74-
75-
int timeout = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TIMEOUT));
76-
timeout = Math.max(timeout, Protocol.DEFAULT_TIMEOUT);
77-
78-
JedisPoolConfig poolConfig = getPoolConfig(properties);
79-
switch (configType) {
54+
private void initialize(Config config) {
55+
Collection<?> nodes = getJedisNodes(config.getRedisHosts(), config.getRedisConfigType());
56+
JedisPoolConfig poolConfig = getPoolConfig(config);
57+
switch (config.getRedisConfigType()) {
8058
case CLUSTER:
81-
dataCache = new RedisClusterManager((Set<HostAndPort>) nodes, password, timeout, poolConfig);
59+
this.dataCache = new RedisClusterManager((Set<HostAndPort>) nodes,
60+
config.getRedisPassword(),
61+
config.getRedisTimeout(),
62+
poolConfig);
8263
break;
8364
case SENTINEL:
84-
String masterName = String.valueOf(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_SENTINEL_MASTER));
85-
dataCache = new RedisSentinelManager((Set<String>) nodes, masterName, password, database, timeout, poolConfig);
65+
this.dataCache = new RedisSentinelManager((Set<String>) nodes,
66+
config.getRedisSentinelMaster(),
67+
config.getRedisPassword(),
68+
config.getRedisDatabase(),
69+
config.getRedisTimeout(),
70+
poolConfig);
8671
break;
8772
default:
88-
dataCache = new RedisStandardManager(((List<String>) nodes).get(0), Integer.parseInt(((List<String>) nodes).get(1)), password, database, timeout, poolConfig);
73+
this.dataCache = new RedisStandardManager(((List<String>) nodes).get(0),
74+
Integer.parseInt(((List<String>) nodes).get(1)),
75+
config.getRedisPassword(),
76+
config.getRedisDatabase(),
77+
config.getRedisTimeout(),
78+
poolConfig);
8979
break;
9080
}
9181
}
9282

9383
/**
9484
* To get redis pool config.
9585
*
96-
* @param properties - Redis data cache properties.
86+
* @param config - Application config.
9787
* @return - Returns the redis pool config.
9888
*/
99-
private JedisPoolConfig getPoolConfig(Properties properties) {
89+
private JedisPoolConfig getPoolConfig(Config config) {
10090
JedisPoolConfig poolConfig = new JedisPoolConfig();
101-
int maxActive = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_MAX_ACTIVE));
102-
poolConfig.setMaxTotal(maxActive);
103-
104-
boolean testOnBorrow = Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_ONBORROW));
105-
poolConfig.setTestOnBorrow(testOnBorrow);
106-
107-
boolean testOnReturn = Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_ONRETURN));
108-
poolConfig.setTestOnReturn(testOnReturn);
109-
110-
int maxIdle = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_MAX_IDLE));
111-
poolConfig.setMaxIdle(maxIdle);
112-
113-
int minIdle = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_MIN_IDLE));
114-
poolConfig.setMinIdle(minIdle);
115-
116-
boolean testWhileIdle = Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_WHILEIDLE));
117-
poolConfig.setTestWhileIdle(testWhileIdle);
118-
119-
int testNumPerEviction = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_NUMPEREVICTION));
120-
poolConfig.setNumTestsPerEvictionRun(testNumPerEviction);
121-
122-
long timeBetweenEviction = Long.parseLong(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TIME_BETWEENEVICTION));
123-
poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEviction);
91+
poolConfig.setMaxTotal(config.getRedisMaxActive());
92+
poolConfig.setTestOnBorrow(config.getRedisTestOnBorrow());
93+
poolConfig.setTestOnReturn(config.getRedisTestOnReturn());
94+
poolConfig.setMaxIdle(config.getRedisMaxIdle());
95+
poolConfig.setMinIdle(config.getRedisMinIdle());
96+
poolConfig.setTestWhileIdle(config.getRedisTestWhileIdle());
97+
poolConfig.setNumTestsPerEvictionRun(config.getRedisTestNumPerEviction());
98+
poolConfig.setTimeBetweenEvictionRunsMillis(config.getRedisTimeBetweenEviction());
12499
return poolConfig;
125100
}
126101

src/main/java/tomcat/request/session/data/cache/impl/redis/RedisClusterManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class RedisClusterManager extends RedisManager {
1515
private final JedisCluster cluster;
1616

1717
private static final int NUM_RETRIES = 30;
18-
private static final int DEFAULT_MAX_REDIRECTIONS = 5;
19-
private static final long FAILIURE_WAIT_TIME = 4000L;
18+
private static final int DEFAULT_MAX_RE_DIRECTIONS = 5;
19+
private static final long FAILURE_WAIT_TIME = 4000L;
2020

2121
RedisClusterManager(Set<HostAndPort> nodes,
2222
String password,
2323
int timeout,
2424
JedisPoolConfig poolConfig) {
25-
super(null, FAILIURE_WAIT_TIME);
26-
this.cluster = new JedisCluster(nodes, timeout, Protocol.DEFAULT_TIMEOUT, DEFAULT_MAX_REDIRECTIONS, password, poolConfig);
25+
super(null, FAILURE_WAIT_TIME);
26+
this.cluster = new JedisCluster(nodes, timeout, Protocol.DEFAULT_TIMEOUT, DEFAULT_MAX_RE_DIRECTIONS, password, poolConfig);
2727
}
2828

2929
/** {@inheritDoc} */

src/main/java/tomcat/request/session/data/cache/impl/redis/RedisManager.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
import redis.clients.jedis.exceptions.JedisConnectionException;
77
import redis.clients.jedis.util.Pool;
88
import tomcat.request.session.data.cache.DataCache;
9-
import tomcat.request.session.data.cache.DataCacheConstants;
109

1110
/** author: Ranjith Manickam @ 12 Jul' 2018 */
1211
abstract class RedisManager implements DataCache {
1312

1413
private static final int NUM_RETRIES = 3;
1514
private static final Logger LOGGER = LoggerFactory.getLogger(RedisManager.class);
1615

16+
private static final String REDIS_CONN_FAILED_RETRY_MSG = "Jedis connection failed, retrying...";
17+
1718
private final Pool<Jedis> pool;
18-
private final long failiureWaitTime;
19+
private final long failureWaitTime;
1920

20-
RedisManager(Pool<Jedis> pool, long failiureWaitTime) {
21+
RedisManager(Pool<Jedis> pool, long failureWaitTime) {
2122
this.pool = pool;
22-
this.failiureWaitTime = failiureWaitTime;
23+
this.failureWaitTime = failureWaitTime;
2324
}
2425

2526
/** {@inheritDoc} */
@@ -119,12 +120,12 @@ public Long delete(String key) {
119120
* @param ex - jedis exception.
120121
*/
121122
void handleException(int tries, RuntimeException ex) {
122-
LOGGER.error(DataCacheConstants.REDIS_CONN_FAILED_RETRY_MSG + tries);
123+
LOGGER.error(REDIS_CONN_FAILED_RETRY_MSG + tries);
123124
if (tries == NUM_RETRIES) {
124125
throw ex;
125126
}
126127
try {
127-
Thread.sleep(this.failiureWaitTime);
128+
Thread.sleep(this.failureWaitTime);
128129
} catch (InterruptedException e) {
129130
Thread.currentThread().interrupt();
130131
}

src/main/java/tomcat/request/session/data/cache/impl/redis/RedisSentinelManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
/** author: Ranjith Manickam @ 3 Dec' 2018 */
99
class RedisSentinelManager extends RedisManager {
1010

11-
private static final long FAILIURE_WAIT_TIME = 2000L;
11+
private static final long FAILURE_WAIT_TIME = 2000L;
1212

1313
RedisSentinelManager(Set<String> nodes,
1414
String masterName,
1515
String password,
1616
int database,
1717
int timeout,
1818
JedisPoolConfig poolConfig) {
19-
super(new JedisSentinelPool(masterName, nodes, poolConfig, timeout, password, database), FAILIURE_WAIT_TIME);
19+
super(new JedisSentinelPool(masterName, nodes, poolConfig, timeout, password, database), FAILURE_WAIT_TIME);
2020
}
2121
}

0 commit comments

Comments
 (0)