Set up Redis as session store in Apache Tomcat 7

Source: Internet
Author: User
Tags download redis apache tomcat redis server

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
  1. <valve classname="Com.radiadesign.catalina.session.RedisSessionHandlerValve"/>
  2. <manager classname="Com.radiadesign.catalina.session.RedisSessionManager"
  3. host="localhost" <!--optional:defaults to "localhost" --
  4. port="6379" <!--optional:defaults to "6379" -
  5. database="0" <!--optional:defaults to "0" --
  6. 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.

    • Jar.zip (181.3 KB)

Set up Redis as session store in Apache Tomcat 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.