springboot 用監(jiān)聽器統(tǒng)計在線人數(shù)案例分析
本文在springboot 的項目,用HttpSessionListener 監(jiān)聽器(監(jiān)聽器的其中一種) 統(tǒng)計在線人數(shù),實質(zhì)是統(tǒng)計session 的數(shù)量。
思路很簡單,但是有個細節(jié)沒處理好,讓我調(diào)試了大半天,才把bug搞好。
先寫個HttpSessionListener 監(jiān)聽器。count 是session的數(shù)量(人數(shù)),session 創(chuàng)建的時候,會觸發(fā)監(jiān)聽器的sessionCreated 方法,session銷毀的時候,會觸發(fā)監(jiān)聽器的sessionDestroyed 方法。 在監(jiān)聽器中計算完人數(shù)count,把他放進servletContext(可以理解為一個倉庫,任意請求可以存儲和獲取里面的屬性)。
注意監(jiān)聽器加上@WebListener,這樣就不用配置。
@WebListener
public class OnLineCount implements HttpSessionListener {
public int count=0;//記錄session的數(shù)量
//監(jiān)聽session的創(chuàng)建,synchronized 防并發(fā)bug
public synchronized void sessionCreated(HttpSessionEvent arg0) {
System.out.println("【HttpSessionListener監(jiān)聽器】count++ 增加");
count++;
arg0.getSession().getServletContext().setAttribute("count", count);
}
@Override
public synchronized void sessionDestroyed(HttpSessionEvent arg0) {//監(jiān)聽session的撤銷
System.out.println("【HttpSessionListener監(jiān)聽器】count-- 減少");
count--;
arg0.getSession().getServletContext().setAttribute("count", count);
}
}
接著寫一個查詢session 數(shù)量的controller,我開始的時候是像下面這樣寫的,是錯誤的!
從servletContext 中取出count ,把count返回前端。
@RequestMapping("/count")
@ResponseBody
public String count(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
Object count=httpServletRequest.getServletContext().getAttribute("count");
return "count : "+count;
}
這樣是錯誤的,測試你會發(fā)現(xiàn),頁面看到count 是null ,因為沒有創(chuàng)建session,沒有觸發(fā)監(jiān)聽器的統(tǒng)計方法。于是改一下:
@Controller
public class IndexController {
@RequestMapping("/count")
@ResponseBody
public String count(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
HttpSession session = httpServletRequest.getSession();
Object count=session.getServletContext().getAttribute("count");
return "count : "+count;
}
}
HttpSession session = httpServletRequest.getSession(); 作用:該用戶如果沒有sesision則創(chuàng)建session ,有則取得session不創(chuàng)建。
改成這樣測試,看起來是對的,但是有個問題。一個瀏覽器對應一個session,你打開2個瀏覽器,看到count是2 ,是對的。但是你關了一個瀏覽器,再打開,應該是2不變才對,但是變成3 了,原因是session銷毀的方法沒有執(zhí)行,重新打開時,服務器找不到用戶原來的session ,重新創(chuàng)建了一個session,于是有3個session了,但是瀏覽器只有2個,也就是模擬應該是只有2個人在線上。
有2個方法可以解決這個問題,一個是在關閉網(wǎng)頁的時候,前端去調(diào)用一個方法把session銷毀。另一個更好的方法是,讓服務器記得原來那個session,即把原來的sessionId 記錄在瀏覽器,下次打開時,把這個sessionId發(fā)送過去,這樣服務器就不會重新創(chuàng)建。
代碼修改如下:
@Controller
public class IndexController {
@RequestMapping("/count")
@ResponseBody
public String number(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
try{ //把sessionId記錄在瀏覽器
Cookie c = new Cookie("JSESSIONID", URLEncoder.encode(httpServletRequest.getSession().getId(), "utf-8"));
c.setPath("/");
//先設置cookie有效期為2天,不用擔心,session不會保存2天
c.setMaxAge( 48*60 * 60);
httpServletResponse.addCookie(c);
}catch (Exception e){
e.printStackTrace();
}
HttpSession session = httpServletRequest.getSession();
Object count=session.getServletContext().getAttribute("count");
return "count : "+count;
}
}
總結(jié)
以上所述是小編給大家介紹的springboot 用監(jiān)聽器統(tǒng)計在線人數(shù)案例分析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Java面試題沖刺第十九天--數(shù)據(jù)庫(4)
這篇文章主要為大家分享了最有價值的三道關于數(shù)據(jù)庫的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-08-08
Maven編譯遇到Process terminated問題(四種情況全部解決)
這篇文章主要介紹了Maven編譯遇到Process terminated問題(四種情況全部解決),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Java使用icepdf將pdf文件按頁轉(zhuǎn)成圖片
這篇文章主要為大家詳細介紹了Java使用icepdf將pdf文件按頁轉(zhuǎn)成圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
elasticsearch的靈魂唯一master選舉機制原理分析
這篇文章主要為大家介紹了elasticsearch的靈魂唯一master選舉機制原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
Maven項目執(zhí)行生命周期相關操作時出現(xiàn)錯誤:does not match a
當pom文件中的gav標簽格式錯誤,如出現(xiàn)中文或空格,會導致與有效的id模式不匹配錯誤,gav標簽應僅包含數(shù)字、字母和下劃線,解決方法是修改標簽中的中文為英文,刪除多余空格,并刷新pom文件,例如,將中文"測試"改為英文"test"2024-09-09

