HttpSessionActivationListener serialization and deserialization,

Source: Internet
Author: User
Tags object serialization

HttpSessionActivationListener serialization and deserialization,
I. serialization and deserialization

1. What is serialization?

      • The process of converting object bit byte sequences is called serialization (stored on hard disk, persistent)
      • The process of converting byte sequences into bit objects is called deserialization (stored in memory)

2. serialization Purpose

      • Stores the object's byte sequence permanently on the hard disk, usually in a file.
      • Serializes the objects transmitted over the network in bytes to facilitate transmission.

The most common is the Session object in the Web server. When 0.1 million users access the Session object concurrently, 0.1 million Session objects may appear, and the memory may not be enough, therefore, the Web Container first serializes some seesion to the hard disk, and then restores the objects stored in the hard disk to the memory.

2. HttpSessionActivationListener listens to session Object serialization and deserialization 1. httpSessionActivationListener is used to monitor the implementation class itself. When the implementation class object is added to the session (session. after setAttribute (), the session object will be executed before serialization (passivation) and after deserialization (activation.
      • SessionWillPassivate (HttpSessionEvent se) session execution before passivation (session from memory to hard disk)
      • SessionDidActivate (HttpSessionEvent se) session activated and executed (session from hard disk to memory)
2. We configure the context. xml file in the META-INF directory, set the expiration time and session serialization save path, the configuration is as follows:
1 <Context>2   <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">3     <Store className="org.apache.catalina.session.FileStore" directory="d:/session"/>4   </Manager>5 </Context>
      • MaxIdlSwap = "1" indicates that the expiration time is 1 minute.
      • Directory = "d:/session" is the session storage path
Project directory structure: 3. Define a class to implement the HttpSessionActivationListener interface and listen for session serialization. The Code is as follows:
1 public class HttpSessionActivation_Listener implements HttpSessionActivationListener, Serializable {2 3 private static final long serialVersionUID =-strong; 4 private String name; 5 6 public void setName (String name) {7 8 this. name = name; 9 10} 11 12 public String getName () {13 return name; 14} 15 16 public HttpSessionActivation_Listener (String name) {17 this. name = name; 18} 19 // The object is instantiated and saved to the session. If the session fails, it is executed before serialization. (only the objects saved to the session will be listened) 20 public void sessionWillPassivate (HttpSessionEvent se) {21 System. out. println (name + "=> the Session is serialized to the hard disk and the SessionId is:" + se. getSession (). getId (); 22 23} 24 // after the session is deserialized, run 25 public void sessionDidActivate (HttpSessionEvent se) {26 System. out. println (name + "=> the Session is deserialized to (activated) the memory. The SessionId is:" + se. getSession (). getId (); 27} 28}

4. jsp page implementation:

1 <% 2 session. setAttribute ("sessionBean", new HttpSessionActivation_Listener ("Cai xiansen"); 3%>

 

Instantiate the object that implements the HttpSessionActivationListener interface and add it to the session. (only the object added to the session can monitor the dynamic state of the session.) Start the server and run the following result:. if you do not perform any operation after one request to the server, wait for one minute and wait until the session expires. (SessionWillPassivate (HttpSessionEvent se) Listener)

B. Re-request the expired session to be deserialized into the memory. You can continue to use it. (SessionDidActivate (HttpSessionEvent se) Listener)

  

The serialization file can be viewed in the configured Directory: (this is complementary, so it will be different from the above sequence id)

  

 

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.