为什么以及如何在 Java Web 应用程序中使用序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/894491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why and how is serialization used in Java web applications?
提问by Will Hartung
I've been working on a Java web application where the framework insists that everything is Serializable. I assume that this isn't specific to the framework but to web applications in Java in general and my question is: what is the framework / server / whatever doing that is serializing things? Does it have to do it?
我一直在开发一个 Java Web 应用程序,其中框架坚持认为一切都是可序列化的。我认为这不是特定于框架的,而是特定于 Java 中的 Web 应用程序的,我的问题是:框架/服务器/正在序列化的东西是什么?它必须这样做吗?
Note: I don't know much about web applications or serialization.
注意:我不太了解 Web 应用程序或序列化。
回答by Will Hartung
The typical issue is that during replication, or server shutdown, the internal HTTP Sessions are "Serialized" out to a persistent store so they can be recovered or shared.
典型的问题是在复制或服务器关闭期间,内部 HTTP 会话被“序列化”到持久存储,以便它们可以被恢复或共享。
Serialization is saving the state of an Object, and being able to reconstitute that state at a later time.
序列化是保存对象的状态,并能够在以后重建该状态。
回答by Michael Borgwardt
It's not strictly necessary for most apps, but can help performance in two ways:
对于大多数应用程序来说,这并不是绝对必要的,但可以通过两种方式提高性能:
- User sessions can be serialized to disk between requests. When the time between requests from the same user is much longer than the time it takes to process a request, this can drastically reduce memory usage of the server and enable you to serve far more users.
- If you use load balancing to spread requests over multiple servers, you either need to ensure that all requests by the same user are always served by the same machine (which reduces the flexibility of the load balancer) or you need to be able to move session data between servers via serialization.
- 用户会话可以在请求之间序列化到磁盘。当来自同一用户的请求之间的时间比处理请求所需的时间长得多时,这可以大大减少服务器的内存使用量并使您能够为更多用户提供服务。
- 如果您使用负载平衡将请求分散到多个服务器上,您要么需要确保同一用户的所有请求始终由同一台机器提供服务(这降低了负载平衡器的灵活性),要么您需要能够移动会话服务器之间通过序列化的数据。
回答by Soviut
Serialization is useful in any application environment, not just web applications.
序列化在任何应用程序环境中都很有用,而不仅仅是 Web 应用程序。
One major use for serialization is for transport. You want to copy or move an object to a remote computer, say a UserProfile. You can serialize the UserProfile, send the serialized data (typically XML) and deserialize it to a new object on the receiving end, at which point it can be manipulated as though it were the original object.
序列化的一个主要用途是传输。您想将对象复制或移动到远程计算机,例如 UserProfile。您可以序列化 UserProfile,发送序列化数据(通常是 XML)并将其反序列化为接收端的新对象,此时它可以像原始对象一样进行操作。
Another use is saving state. You may have a game and you want to save the exact state of a game board. You can serialize the Board object which can subsequently be told to serialize each of its Tiles.
另一个用途是保存状态。您可能有一个游戏,并且想要保存游戏板的确切状态。您可以序列化 Board 对象,该对象随后可以被告知序列化其每个 Tiles。
回答by Allain Lalonde
Depending on the web framework, objects in the session might be persisted to disk by using serialization.
根据 Web 框架,会话中的对象可能会使用序列化持久化到磁盘。
回答by Jim Ferrans
If you are using Enterprise Java Beans (EJBs), and have a Stateless Session Bean API, then each SSB instance runs in one thread on one Java Virtual Machine, and its clients run in different threads, and, in general, in different JVMs on different computers. Because you can't pass a reference to a Java object from one JVM to another, the object needs to be serialized into a string, sent over to the other JVM, and then deserialized back into an object. This serialization/deserialization happens to both the arguments coming in and the return value going out.
如果您使用的是 Enterprise Java Bean (EJB),并且有一个无状态会话 Bean API,那么每个 SSB 实例都在一个 Java 虚拟机上的一个线程中运行,并且它的客户端在不同的线程中运行,并且通常在不同的 JVM 中运行不同的电脑。因为您不能将 Java 对象的引用从一个 JVM 传递到另一个 JVM,所以需要将该对象序列化为字符串,发送到另一个 JVM,然后反序列化回一个对象。传入的参数和传出的返回值都会发生这种序列化/反序列化。
If you're using the Java Message Service, the objects you send in each message get serialized into a string, persisted into a database, and then get deserialized by the message receiver at some other time and place.
如果您使用的是 Java 消息服务,那么您在每条消息中发送的对象会被序列化为字符串,持久化到数据库中,然后在其他时间和地点被消息接收者反序列化。
And, as Will Hartung points out, HTTP Session objects in general are shared across JVMs. If you have a cluster of Glassfish Java EE application servers running an ecommerce application, each customer's requests are load-balanced to any available server, and that server must be able to look up the customer's session state (customer name, shopping cart, etc.). It can only get this via serialization. Session state may also be written to disk (eg, a database) for safety.
而且,正如 Will Hartung 指出的那样,HTTP Session 对象通常在 JVM 之间共享。如果您有一组运行电子商务应用程序的 Glassfish Java EE 应用程序服务器,则每个客户的请求都会负载均衡到任何可用的服务器,并且该服务器必须能够查找客户的会话状态(客户名称、购物车等)。 )。它只能通过序列化来获得。为了安全,会话状态也可以写入磁盘(例如,数据库)。

