[問題] 有關session與HttpSession型別轉換

看板java作者 (T-Pure)時間12年前 (2011/12/14 19:00), 編輯推噓3(302)
留言5則, 4人參與, 最新討論串1/1
想請教各位先進 因為目前要寫關於session存取到Redis資料庫的程式 碰到了不少與PersistentManager, session, HttpSession相關的問題 主要碰到的問題在於 如果要將session存至redis內,使用下述的code public void save(Session session) throws IOException { ObjectOutputStream oos = null; ByteArrayOutputStream bos = null; Map<byte[], byte[]> hash = new HashMap<byte[], byte[]>(); bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(new BufferedOutputStream(bos)); ((StandardSession) session).writeObjectData(oos); oos.close(); oos = null; hash.put(ID_FIELD, session.getIdInternal().getBytes()); hash.put(DATA_FIELD, bos.toByteArray()); Jedis jedis = getJedis(); jedis.hmset(session.getIdInternal().getBytes(), hash); closeJedis(jedis); } 即可將資料存到redis 然而在擷取session這邊碰到了不少的麻煩.. 原因在於抓session的地方使用的是 HttpServletRequest request HttpSession session = request.getSession(true); 而HttpSession似乎無法型態強制轉換到session (org.apache.catalina.Session) 會出現的exception為 java.lang.ClassCastException: org.apache.catalina.session.StandardSessionFacade cannot be cast to org.apache.catalina.Session 原先利用修改context.xml讓redis能自動存取session 不過碰到tomcat出現詭異的60 sec cycle (每隔六十秒才會將session存至redis的詭異情形) 因此手動想寫出同樣的功能時碰到這個問題 而又不瞭解PersistentManager是在哪邊call session store相關的API 因此想請教版上的先進是否有相關的經驗 或是有更好的方法能處理 謝謝~~ 補充: 使用環境是tomcat6 jedis 2.0.0 ※ 編輯: LucifarAH 來自: 221.169.119.100 (12/14 20:53)

12/15 11:55, , 1F
==.=== 自己實做想要的 DTO? [逃]
12/15 11:55, 1F

12/15 12:19, , 2F
推直接做DTO XD
12/15 12:19, 2F

12/15 15:38, , 3F
PersistenceManager每processExpiresFrequency * container
12/15 15:38, 3F

12/15 15:39, , 4F
的backgroundProcessorDelay秒才會跑一次檢查,並不詭異
12/15 15:39, 4F
文章代碼(AID): #1Ew86nju (java)