编程中的会话复制(Weblogic 和 Java)

发布于 2024-10-16 00:01:27 字数 664 浏览 4 评论 0原文

我想知道调用 session.setAttribute 或 session.getAttribute 的对象是否还需要实现“java.io.Serialized”以支持会话复制?

例如
User 类实现了 Serialized 接口。 但如果调用“session.setAttribute”的类SomeUtilityClass需要实现Serialized接口。


/* Does it also need to implement the Serializable here? */
public class SomeUtilityClass{
    public void test(HttpServletRequest request){
        request.getSession().setAttribute("user", new User());
    }
}

public class User implements Serializable{
        private static final long serialVersionUID = 12345432534456654664L;
        //skip the getter & setter...
}

如果会话复制中省略了变量serialVersionUID,是否会出现任何问题?

提前致谢。

I would like to know if the object calling the session.setAttribute or session.getAttribute should be also needed to implement the "java.io.Serializable" in order to support the session replication?

for example
the class User is implemented the Serializable interface.
But if the class SomeUtilityClass that calling "session.setAttribute" is required to implement the Serializable interface.


/* Does it also need to implement the Serializable here? */
public class SomeUtilityClass{
    public void test(HttpServletRequest request){
        request.getSession().setAttribute("user", new User());
    }
}

public class User implements Serializable{
        private static final long serialVersionUID = 12345432534456654664L;
        //skip the getter & setter...
}

And is there any problems if the variable serialVersionUID has been omitted for the session replication?

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-10-23 00:01:27

只有进入会话的对象才需要可序列化(即实现Serializable接口),

这意味着设置为属性的任何对象,以及这些对象中包含的任何对象。

因此,如果您有一个类

public class User implements Serializable {
      private Department department;

// ...
}

,那么您的 Department 类还需要实现 Serializable

类,这些类可以简单地在会话上进行调用(包括对 setAttribute 的调用)不需要实现 Serialized (包括 SomeUtilityClass

原因是会话中的任何内容都必须发送到其他 WebLogic 节点(可能运行在不同的服务器)。这意味着它需要通过网络发送您的 User 类,因此它需要能够将您的 User 类转换为一系列可以发送的字节,然后在另一面再次将其变回真实物体。

当您实现 Serialized 接口时,您就是在告诉 Java,您已经以可以将类转入这些字节(然后再转回来)的方式构建了类。如果你没有实现Serialized,那么Java不知道是否可以这样做,所以它不会这样做。
注意:类的代码不会被发送,只是发送其中字段的值。该代码已经在另一个节点上,因为它是已经部署到所有节点的应用程序的一部分。

您的实用程序类不需要通过网络。您还没有将该类的对象放入会话中,因此 WebLogic 不需要将其转换为字节(然后再返回_)作为会话复制的一部分。
唯一需要可序列化的是直接放入会话的对象(例如您的User)或间接(如部门示例)。
仅在会话上进行调用的对象不需要可序列化

Only objects that go into the session need to be serializable (i.e. Implement that Serializable interface)

That means any object that is set as an attribute, plus any objects that are contained inside those objects.

So if you have a class

public class User implements Serializable {
      private Department department;

// ...
}

then your Department class also needs to implement Serializable

Classes that simple make calls on the session (include calls to setAttribute) do not need to implement Serializable (that includes SomeUtilityClass)

The reason is that anything that is inside the session has to be sent to the other WebLogic node (that is probably running on a different server). That means that it needs to send your User class over the network, so it needs to be able to turn your User class into a series of bytes, that it can send, and then turn it back into a real object again on the other side.

When you implement the Serializable interface you're telling Java that you've built your class in a way that it's OK for it to turn in into those bytes, (and back again). If you don't implement Serializable then Java doesn't know whether it OK to do that, so it doesn't.
Note: The code for the class isn't being sent around, just the values of the fields inside it. The code is already on the other node, because it's part of your application that is already deployed to all nodes already.

Your utility class, doesn't need to go over the network. You haven't put an object of that class into the session, so WebLogic doesn't need to turn it into bytes (and back again_) as part of session replication.
The only things that need to be Serializable are the objects that get put into the session either directly (like your User) or indirectly (like the Department example).
Objects that simply make calls on the session do not need to be Serializable.

望喜 2024-10-23 00:01:27

如果您的实用程序类没有可跨服务器实例复制的状态,则无需将其序列化。至于 serialVersionUID 是 < a href="https://stackoverflow.com/questions/285793/why-should-i-bother-about-serialversionuid">整个新单独讨论

If your utility class does not have state to be replicated across server instances then there is no need for it to be serializable. As for the serialVersionUID, is a whole new discussion on its own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文