Set up Redis as session store in Apache Tomcat 7
Set up Redis as session store in Apache Tomcat 7
Redis already has components that support the use of Redis as the default session memory for Tomcat directly in Tomcat7, as described below in the configuration process
1. Download Redis from http://redis.io/and follow the Redis server
wget http://download.redis.io/redis-stable.tar.gz
Tar xvzf redis-stable.tar.gz
CD redis-stable
Make
2. Start Redis
CD REDISDIRECTORY/SRC
./redis-server--port 6379
3. Download TOMCAT7 from http://tomcat.apache.org/download-70.cgi and follow tomcat7
4. Download Jedis from Https://github.com/xetorthio/jedis/downloads as a Redis client for Java,
Download the Redis Session Manager plugin for Tomcat from Https://github.com/jcoleman/tomcat-redis-session-manager/downloads,
Download Apache common Pool package from http://commons.apache.org/proper/commons-pool/download_pool.cgi,
Copy these jar packages to the tomcat7 Lib directory
Actually there is a pit, but you are more fortunate, I help you step on first. If you download the latest version of all, it certainly won't start up. I've been trying to match these jar versions for a long time. Can be downloaded in the attachment. I'm using a JDK that's 1.7.
5. Modify the Context.xml file under Tomcat's conf, add or modify the configuration below
Java code
- <valve classname="Com.radiadesign.catalina.session.RedisSessionHandlerValve"/>
- <manager classname="Com.radiadesign.catalina.session.RedisSessionManager"
- host="localhost" <!--optional:defaults to "localhost" --
- port="6379" <!--optional:defaults to "6379" -
- database="0" <!--optional:defaults to "0" --
- Maxinactiveinterval=<!--optional:defaults to "the" (in seconds) and/>
6. After rebooting tomcat, you can see that the session is stored on Redis.
------------------------------------------------------------------------------------------------
Possible issues
There is the possibility of a race condition, would cause seeming invisibility of the session immediately after your W EB application Logs in a user:if the response have finished streaming and the client requests a new page before the valve have been able to complete saving the session into Redis and then the new request won't see the session.
This condition is detected by the session manager and a java.lang.IllegalStateException with the message would be Race condition encountered: attempted to load session[SESSION_ID] which has been created but not yet serialized.
Thrown.
Normally this should is incredibly unlikely (insert joke about programmers and "This should never happen" statements he RE) since the connection to save the session into Redis are almost guaranteed to be faster than the latency between a clien T receiving the response, processing it, and starting a new request.
If you encounter errors and then you can force save the session early (before sending a response to the client) then you can Retrieve the current session, and call to currentSession.manager.save(currentSession)
synchronously eliminate the race condition. Note:this would only work directly if your application have the actual session object directly exposed. Many frameworks (and often even Tomcat) would expose the session in their own wrapper HttpSession implementing class. Able to dig through these layers to expose the actual underlying redissession instance--if so and then using that Instance'll allow your to implement the workaround.
Set up Redis as session store in Apache Tomcat 7