定时检查session

有时候,会碰到这种情况:需要定时检查session里面的值(如attribute),如心跳通知,这样,就要收集服务器中的所有session,并做相应的操作 ,等等。

思路其实和 博文“如何定时执行数据库查询操作(tomcat+spring)”擦不多,稍微有点差别。

主要就是Listener类,和前面那篇博文 不一样的是 实现的接口,多实现了两个接口:HttpSessionListener, HttpSessionAttributeListener, 就可以监听session、attribute的创建、销毁了。

代码如下:

public class SessionListener implements ServletContextListener,
		HttpSessionListener, HttpSessionAttributeListener {
	private MyThread myThread;
	public static Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		if (myThread == null) {
			myThread = new MyThread();
			System.out.println("**********************");
			System.out.println("线程已经开启!");
			myThread.start();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		if (myThread != null && myThread.isInterrupted()) {
			System.out.println("******************");
			System.out.println("线程已经阻塞!");
			myThread.interrupt();
		}
	}

	@Override
	public void attributeAdded(HttpSessionBindingEvent event) {
		System.out.println("增加属性");
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent event) {
		System.out.println("移除属性");
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent event) {
		System.out.println("替换属性");
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("*****************************");
		sessionMap.put("session" + System.currentTimeMillis(), se.getSession());
		System.out.println("创建session, 时间 :"
				+ se.getSession().getCreationTime());
		System.out.println("hashmap中的session数量:" + sessionMap.size());
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		// 经过测试,不用在该方法写代码,即可删除session
		// se.getSession().invalidate();
		// if (hUserName.containsValue(se.getSession())) {
		// // 如果hashmap中存在需要销毁的session,将该session去掉, 需要根据key来remove
		// hUserName.remove(se.getSession());
		// }
		Collection<HttpSession> colSession = sessionMap.values();
		if (colSession.contains(se.getSession())) {
			// 如果hashmap中存在需要销毁的session,将该session去掉
			colSession.remove(se.getSession());
			se.getSession().invalidate();
		}
		System.out.println("hashmap size:" + sessionMap.size());
		System.out.println("session有值吗?" + se.getSession().getAttributeNames());
		System.out.println("清除session");
	}
}


线程类:

class MyThread extends Thread {
	public void run() {
		while (!this.isInterrupted()) {
			try {
				// 每隔1分钟个性化服务端会在自身session有效期内,向门户服务端发起通知请求
				Thread.sleep(1000 * 60 * 1);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 遍历所有有效的session, 并将session的相关信息发送给门户客户端,看该session是否有效
			Collection<HttpSession> colSession = SessionListener.sessionMap
					.values();
			System.out.println("****************************");
			System.out.println("session size = " + colSession.size());
			for (HttpSession httpSession : colSession) {
			    /**
			     *
			     * session处理代码
			     */ 
			    System.out.println("FUCK TIME :" + System.currentTimeMillis());
		        }
		 }       
	}
}


转载于:https://my.oschina.net/beanGo/blog/296543

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值