Implementing your own Tomcat Session Manager (Java) with Redis

Source: Internet
Author: User
Tags redis serialization redis server
implementing your own Tomcat Session Manager (Java) with Redis

Recently the business expanded to several Tomcat, the front end using the Nginx,rewrite rule set to Ip_hash, but so if there is a tomcat hung, then the user will be landing again, could have endured, but this is always a problem, So I decided to implement a session manager of my own, all the users ' sessions are saved to this one, and get to this server Once the session has changed, it will be saved to the server again. Well, that's probably it.

From the Internet to find an open source project such as: Https://github.com/jcoleman/tomcat-redis-session-manager. The rationale for this project, which is used on Tomcat, is to implement the Tomcat manager interface so that all session accesses are directed to the Redis server. Then, when deployed, simply copy the appropriate jar package to the Tomcat Lib directory, and then modify the Context.xml configuration file in conf to be fully transparent to the Web application. It's a very elegant implementation. So I use this project as the basis for my own session manager implementation.

This project has found many problems after using and testing. Here's a summary: Objects saved in session must implement the serializable interface and must have a default constructor that uses Java serialization, which is slower if the save rule is set to default, Then each time you need to serialize the entire session into a byte array and then get a summary to compare whether the session changes, slow.

OK, we found the problem of open source engineering, then we will modify it. Use the Kryo serialization library to replace the serialization of Java itself, so that the speed is significantly elevated and the object does not need to have a default constructor. Then delete the code of the summary comparison, only need to judge whether to save to Redis by the dirty flag, this requires the code to cooperate to guarantee correctness, for example, save a list in the session, then add a few elements in the list, so because the list does not change, So the manager will judge the session does not change and then do not save, it is wrong, so the need for the code active logo This session is dirty, a line of code can:

Session.setattribute ("__changed__");

Several interesting questions were found in the implementation process: Kryo Library solution serialization appears ClassNotFound exception

This is an interesting question, and the mechanism involved in Tomcat's ClassLoader is a good example of the understanding of Tomcat's ClassLoader. The specific reason is this: the library under Tomcat's lib directory is standardclassloader loaded, but each webapp will have its own classloader, the type is WebappClassLoader, The relationship between the two is WebappClassLoader's parent is standardclassloader. Remember these. The reason is analyzed below.

Because the deployment Redis session Manager is to copy the library to the Lib directory, then Kryo's ClassLoader is Standardclassloader, But the object saved in session is a class under the Lib directory under WebApp, which is loaded by WebappClassLoader, Standardclassloader is not possible to find the class object of WebappClassLoader loaded classes, so throw an exception.

Solution:
Modify the ClassLoader in the Class.forName method in the Kryo library to the ClassLoader of the current thread context. The classloader of the context of the Tomcat thread is WebappClassLoader so that a WebApp class can be loaded. Not Arg constructor method error

or the Kryo error, saying that no default constructor was found.

Workaround: The Kryo library does not require a default constructor, and if there is no default constructor, it uses the Java underlying method to construct the object using bytecode. You need to use the following code to turn on this feature:

Kryo.setinstantiatorstrategy (New Defaultinstantiatorstrategy (New Stdinstantiatorstrategy ()));
Conclusions

The modified manager performance is about 40% higher than it used to be. The code is placed on the GitHub, the interested can go to download the use.
Https://github.com/longlong524/redis_session_manager

Related Article

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.