简体   繁体   English

强制类为 Tomcat 会话复制实现可序列化

[英]Force classes to implement Serializable for Tomcat Session Replication

I am implementing tomcat session replication for achieving load balancing between cluster however replication needs all objects in session to be Serializable.我正在实现 tomcat 会话复制以实现集群之间的负载平衡,但是复制需要会话中的所有对象都是可序列化的。

There are lots of classes which were not Serilazable and stored in session before cluster and we changed to implement Serilazable however it might happen as future classes are added, someone might miss out Serializable and this would result in error while session replication.有很多类不是 Serilazable 并且在集群之前存储在会话中,我们更改为实现 Serilazable 但是随着未来类的添加可能会发生这种情况,有人可能会错过 Serializable 这将导致会话复制时出错。

I was thinking to extend HttpSession implementation provided by tomcat and override setAttribute to intake Serializable object as parameter however the interface method takes Object as param so it is not possible.我想扩展tomcat提供的HttpSession实现并覆盖setAttribute以引入Serializable对象作为参数,但是接口方法将Object作为参数,所以这是不可能的。

is there way where I can force all classes that would be put into session be Serilazable at compile time ?有什么方法可以强制所有将放入会话的类在编译时可串行化?

We are using spring 4. I wonder think aspect would help ?我们正在使用 spring 4。我想知道方面会有所帮助吗?

I would neither recommend Java Serialization nor session replication .既不推荐 Java 序列化也不推荐会话复制 That notwithstanding:尽管如此:

If the class is extendable that you can subclass it with a class that also implements Serializable .如果该类是可扩展的,则您可以使用一个也实现Serializable类对其进行子类Serializable None of the base classes fields will be serialised, of course.当然,任何基类字段都不会被序列化。 You will need a custom readObject and writeObject to perform these actions.您将需要自定义readObjectwriteObject来执行这些操作。

To do it dynamically, then presumably you wouldd need some filter mechanism to replace objects as they go in and out of the session.要动态执行此操作,您可能需要某种过滤机制来在对象进出会话时替换对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM