最開(kāi)始的想法是使用session監(jiān)聽(tīng)器,在session創(chuàng)建時(shí),根據(jù)sessionid為鍵,map為值,加入redis中.,在session銷(xiāo)毀時(shí)刪除此sessionid.
包裝session,重寫(xiě)session中的方法來(lái)設(shè)置值和取得值.
包裝request,將包裝的session作為包裝request的session.
結(jié)果在實(shí)現(xiàn)時(shí),發(fā)現(xiàn)每次創(chuàng)建session時(shí)id都不同而且控制不了,這樣的話(huà)一臺(tái)服務(wù)器死掉,切換到另一臺(tái)服務(wù)器的時(shí)候
原來(lái)存在redis中的數(shù)據(jù)得不到.
后面又想覆蓋掉jsessionid,沒(méi)成功.只能創(chuàng)建了一個(gè)rsessionid來(lái)做,在過(guò)濾器中根據(jù)此鍵來(lái)存入redis,
由于關(guān)閉ofbiz時(shí),session銷(xiāo)毀沒(méi)有執(zhí)行,只能設(shè)置存活時(shí)間.
由于有多個(gè)組件要使用此功能那么在初始化連接時(shí)初始一次就行了.
不過(guò)目前為止還沒(méi)有上線(xiàn),不知道這樣有沒(méi)有問(wèn)題.
主要代碼:
public void init(FilterConfig arg0) throws ServletException { if (redisClient == null) { synchronized (RedisFilter.class) { if (redisClient == null) { redisClient = RedisClient.getRedisClient(); } } } } //當(dāng)關(guān)閉服務(wù)器時(shí)session銷(xiāo)毀沒(méi)有觸發(fā)session監(jiān)聽(tīng)器 PRivate void setRedisAttribute(String id) { Jedis redis = null; try { redis = redisClient.getJedis(); Object redisValue = redis.get(UtilObject.getBytes(id)); if (redisValue == null) { synchronized (RedisFilter.class) { if (redisValue == null) { redis.set(UtilObject.getBytes(id), UtilObject.getBytes(new HashMap<String, Object>())); redis.expire(UtilObject.getBytes(id), 86400); } } } } catch (Exception e) { e.printStackTrace(); } finally { redisClient.returnJedis(redis); } }HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; Cookie[] cookies = request.getCookies(); String sessionId = "", cookiePath = ""; int maxAge = -1; if (cookies != null) { for (Cookie c : cookies) { if (c.getName().equals("RSESSIONID")) { sessionId = c.getValue(); cookiePath = c.getPath(); maxAge = c.getMaxAge(); } } } HttpSession session = request.getSession(); sessionId = UtilValidate.isEmpty(sessionId) ? session.getId() : sessionId; cookiePath = "/"; session.setAttribute("redisSessionId", sessionId); Cookie c1 = new Cookie("RSESSIONID", sessionId); c1.setPath(cookiePath); response.addCookie(c1); arg2.doFilter(request, response);
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注