IDEA之Session的活化和钝化

Session活化和钝化的服务器调优,性能优化方案 ,网上已经很多了,这里我就强调一下 应用场景,总之这个很重要。

应用场景:

1.一般来说,服务器启动后,就不会再关闭了,但是如果逼不得已需要重启,而用户会话还在进行相应的操作,这时就需要使用序列化将session信息保存起来放在硬盘,服务器重启后,又重新加载。这样就保证了用户信息不会丢失,实现永久化保存
2.淘宝每年都会有定时抢购的活动,很多用户会提前登录等待,长时间不进行操作,一致保存在内存中,而到达指定时刻,几十万用户并发访问,就可能会有几十万个session,内存可能吃不消,这时就需要进行对象的活化、钝化,让其在闲置的时候离开内存,将信息保存至硬盘,等要用的时候,就重新加载进内存.
接下来我将给大家,

1、展示session的活化和钝化实现代码;

2、session活化后文件路径的位置;

简单的代码实现:(其实这个代码实现很简单,关键是他的作用!)废话不多说,来代码:
实体类:Customer.java
package com.hello.domian;

import java.io.Serializable;

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;

public class Customer implements HttpSessionActivationListener, Serializable {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    //钝化
    public void sessionWillPassivate(HttpSessionEvent se) {
        System.out.println("customer被钝化了");
    }

    @Override
    //活化
    public void sessionDidActivate(HttpSessionEvent se) {
        System.out.println("customer被活化了");
    }


}
session钝化代码:
package com.hello.domian;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TestCustomerActiveServlet extends HttpServlet {

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      
      HttpSession session = request.getSession();
   
      //将customer放到session中
      Customer customer =new Customer();
      customer.setId("200");
      customer.setName("lucy");
      session.setAttribute("customer", customer);
      System.out.println("customer被放到session域中了");
      
      
   }

   protected void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      doGet(request, response);
   }
}
session活化代码:
package com.hello.domian;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TestCustomerActiveServlet2 extends HttpServlet {

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      //从session域中获得customer
      HttpSession session = request.getSession();
      Customer customer = (Customer) session.getAttribute("customer");
      System.out.println(customer.getName());
   }

   protected void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      doGet(request, response);
   }
}
配置一下content.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
   <!-- maxIdleSwap:session中的对象多长时间不使用就钝化 -->
   <!-- directory:钝化后的对象的文件写到磁盘的哪个目录下 配置钝化的对象文件在 work/catalina/localhost/钝化文件 -->
   <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
      <Store className="org.apache.catalina.session.FileStore" directory="/itheima" />
   </Manager>
</Context>


执行代码后,结果就是:

没错,系统会生成一个 session文件。

但是,你会发现如果你是用IDEA编辑器启动的项目,他生成的 session文件路径和 用eclipse的不一样。
IDEA生成的session不在 tomcat webapp下也不在 tomcat work下面。

然后认真发现 IDEA生成项目的时候有如下信息:

没错,已经找到答案了! 这里 Using CATALINA_BASE C:\Users\kiven\.IntelliJIdea2017.1\system\tomcat\Unnamed_WEB23
沿着这个线索寻找,终于找到了这个:
路径:C:\Users\kiven\.IntelliJIdea2017.1\system\tomcat\Unnamed_WEB23\work\Catalina\localhost\WEB23\itheima


这样就可以了!

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值