package com.orangefunction.tomcat.redissessions;
import java.io.IOException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Loader;
import org.apache.catalina.Session;
import org.apache.catalina.Valve;
import org.apache.catalina.session.ManagerBase;
import org.apache.catalina.util.LifecycleSupport;
import org.apache.catalina.util.SessionConfig;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.Protocol;
import redis.clients.util.Pool;
public class RedisSessionManager extends ManagerBase implements Lifecycle {
enum SessionPersistPolicy {
DEFAULT,
SAVE_ON_CHANGE,
ALWAYS_SAVE_AFTER_REQUEST;
static SessionPersistPolicy fromName(String name) {
for (SessionPersistPolicy policy : SessionPersistPolicy.values()) {
if (policy.name().equalsIgnoreCase(name)) {
return policy;
}
}
throw new IllegalArgumentException("Invalid session persist policy [" + name + "]. Must be one of " + Arrays.asList(SessionPersistPolicy.values())+ ".");
}
}
protected byte[] NULL_SESSION = "null".getBytes();
private final Log log = LogFactory.getLog(RedisSessionManager.class);
protected String host = "localhost";
protected int port = 6379;
protected int database = 0;
protected String password = null;
protected int timeout = Protocol.DEFAULT_TIMEOUT;
protected String sentinelMaster = null;
Set<String> sentinelSet = null;
protected Pool<Jedis> connectionPool;
protected JedisPoolConfig connectionPoolConfig = new JedisPoolConfig();
protected RedisSessionHandlerValve handlerValve;
protected ThreadLocal<RedisSession> currentSession = new ThreadLocal<>();
protected ThreadLocal<SessionSerializationMetadata> currentSessionSerializationMetadata = new ThreadLocal<>();
protected ThreadLocal<String> currentSessionId = new ThreadLocal<>();
protected ThreadLocal<Boolean> currentSessionIsPersisted = new ThreadLocal<>();
protected Serializer serializer;
protected static String name = "RedisSessionManager";
protected String serializationStrategyClass = "com.orangefunction.tomcat.redissessions.JavaSerializer";
protected EnumSet<SessionPersistPolicy> sessionPersistPoliciesSet = EnumSet.of(SessionPersistPolicy.DEFAULT);
/**
* The lifecycle event support for this component.
*/
protected LifecycleSupport lifecycle = new LifecycleSupport(this);
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getDatabase() {
return database;
}
public void setDatabase(int database) {
this.database = database;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setSerializationStrategyClass(String strategy) {
this.serializationStrategyClass = strategy;
}
public String getSessionPersistPolicies() {
StringBuilder policies = new StringBuilder();
for (Iterator<SessionPersistPolicy> iter = this.sessionPersistPoliciesSet.iterator(); iter.hasNext();) {
SessionPersistPolicy policy = iter.next();
policies.append(policy.name());
if (iter.hasNext()) {
policies.append(",");
}
}
return policies.toString();
}
public void setSessionPersistPolicies(String sessionPersistPolicies) {
String[] policyArray = sessionPersistPolicies.split(",");
EnumSet<SessionPersistPolicy> policySet = EnumSet.of(SessionPersistPolicy.DEFAULT);
for (String policyName : policyArray) {
SessionPersistPolicy policy = SessionPersistPolicy.fromName(policyName);
policySet.add(policy);
}
this.sessionPersistPoliciesSet = policySet;
}
public boolean getSaveOnChange() {
return this.sessionPersistPoliciesSet.contains(SessionPersistPolicy.SAVE_ON_CHANGE);
}
public boolean getAlwaysSaveAfterRequest() {
return this.sessionPersistPoliciesSet.contains(SessionPersistPolicy.ALWAYS_SAVE_AFTER_REQUEST);
}
public String getSentinels() {
StringBuilder sentinels = new StringBuilder();
for (Iterator<String> iter = this.sentinelSet.iterator(); iter.hasNext();) {
sentinels.append(iter.next());
if (iter.hasNext()) {
sentinels.append(",");
}
}
return sentinels.toString();
}
public void setSentinels(String sentinels) {
if (null == sentinels) {
sentinels = "";
}
String[] sentinelArray = sentinels.split(",");
this.sentinelSet = new HashSet<String>(Arrays.asList(sentinelArray));
}
public Set<String> getSentinelSet() {
return this.sentinelSet;
}
public String getSentinelMaster() {
return this.sentinelMaster;
}
public void setSentinelMaster(String master) {
this.sentinelMaster = master;
}
@Override
public int getRejectedSessions() {
// Essentially do nothing.
return 0;
}
public void setRejectedSessions(int i) {
// Do nothing.
}
protected Jedis acquireConnection() {
Jedis jedis = connectionPool.getResource();
if (getDatabase() != 0) {
jedis.select(getDatabase());
}
return jedis;
}
protected void returnConnection(Jedis jedis, Boolean error) {
if (error) {
connectionPool.returnBrokenResource(jedis);
} else {
connectionPool.returnResource(jedis);
}
}
protected void returnConnection(Jedis jedis) {
returnConnection(jedis, false);
}
@Override
public void load() throws ClassNotFoundException, IOException {
}
@Override
public void unload() throws IOException {
}
/**
* Add a lifecycle event listener to this component.
*
* @param listener The listener to add
*/
@Override
public void addLifecycleListener(LifecycleListener listener) {
lifecycle.addLifecycleListener(listener);
}
/**
* Get the lifecycle listeners associated with this lifecycle. If this
* Lifecycle has no listeners registered, a zero-length array is returned.
*/
@Override
public LifecycleListener[] findLifecycleListeners() {
return lifecycle.findLifecycleListeners();
}
/**
* Remove a lifecycle event listener from this component.
*
* @param listener The listener to remove
*/
@Override
public void removeLifecycleListener(LifecycleListener listener) {
lifecycle.removeLifecycleListener(listener);
}
/**
* Start this component and implement the requirements
* of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
@Override
protected synchronized void startInternal() throws LifecycleException {
super.startInternal();
setState(LifecycleState.STARTING);
Boolean attachedToValve = false;
for (Valve valve : getContainer().getPipeline().getValves()) {
if (valve instanceof RedisSessionHandlerValve) {
this.handlerValve = (RedisSessionHandlerValve) valve;
this.handlerValve.setRedisSessionManager(this);
log.info("Attached to RedisSessionHandlerValve");
attachedToValve = true;
break;
}
}
if (!attachedToValve) {
String error = "Unab
没有合适的资源?快使用搜索试试~ 我知道了~
Tomcat Session Redis 共享
共28个文件
class:8个
java:6个
prefs:3个
5星 · 超过95%的资源 需积分: 10 366 下载量 117 浏览量
2015-09-20
22:45:20
上传
评论 7
收藏 65KB ZIP 举报
温馨提示
基于 Redis 的Tomcat Session共享。 对Git项目tomcat-redis-session-manager 做了简单修改。 使用方法,可以参考博客: http://blog.csdn.net/catoop/article/details/48603891
资源推荐
资源详情
资源评论
收起资源包目录
tomcat-redis-session-manager.zip (28个子文件)
tomcat-redis-session-manager
.project 580B
src
test
java
main
java
com
orangefunction
tomcat
redissessions
SessionSerializationMetadata.java 1KB
JavaSerializer.java 3KB
Serializer.java 509B
RedisSession.java 3KB
RedisSessionHandlerValve.java 864B
RedisSessionManager.java 26KB
target
maven-status
maven-compiler-plugin
testCompile
default-testCompile
inputFiles.lst 0B
compile
default-compile
createdFiles.lst 551B
inputFiles.lst 719B
generated-sources
annotations
classes
com
orangefunction
tomcat
redissessions
SessionSerializationMetadata.class 2KB
RedisSessionHandlerValve.class 1KB
RedisSessionManager.class 27KB
Serializer.class 706B
RedisSessionManager$SessionPersistPolicy.class 2KB
DeserializedSessionContainer.class 739B
JavaSerializer.class 6KB
RedisSession.class 4KB
META-INF
MANIFEST.MF 107B
maven
com.orangefunction.tomcat.redissessions
tomcat-redis-session-manager
pom.properties 291B
pom.xml 2KB
maven-archiver
pom.properties 155B
test-classes
tomcat-redis-session-manager-1.2.jar 23KB
surefire
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 430B
org.eclipse.core.resources.prefs 119B
pom.xml 2KB
.classpath 1024B
共 28 条
- 1
catoop
- 粉丝: 7827
- 资源: 106
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页