您的位置:首页 > 运维架构

统计在线用户并进行监控

2011-03-10 17:42 239 查看
一、在线用户数据表

create table CRM_SESSION
(
USERID     NUMBER,
USERNAME   VARCHAR2(50),
SESSIONIDS VARCHAR2(50),
USERSTATE  VARCHAR2(50),
CALLSTATE  VARCHAR2(50)
);
alter table CRM_SESSION
add constraint AK_KEY_1_CRM_SESS unique (USERID)


二、当用户登录时

将用户的数据放入数据库中(代码略)

因为在sessionCreated()方法中,取出的用户信息为空,直接在这一步中直接将用户保存到数据库中。

三、当用户登出时,

1,在action中调用

getSession().invalidate();

2,session监听就会启动。

在sessionDestroyed()方法中,他会删除用户的信息。

例如:

package cn.com.xinli.crm.core;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.com.xinli.crm.ibatis.commquery.SessionID;
import cn.com.xinli.crm.ibatis.sysmanage.SysUser;
import cn.com.xinli.crm.service.commquery.ISessionIDService;
public class UserOnline implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
}
public void sessionDestroyed(HttpSessionEvent event) {
try {
HttpSession session = event.getSession();
SysUser user = (SysUser) session.getAttribute("sessionUser");
WebApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(session
.getServletContext());
ISessionIDService sesService = (ISessionIDService) ctx
.getBean("sesService");
SessionID sid = new SessionID();
sid.setUserid("" + user.getUserId());
sesService.del(sid);
} catch (Exception e) {
e.printStackTrace();
}
}
}


在上面的例子中:可以直接取Spring中的实体Bean.

WebApplicationContext ctx = WebApplicationContextUtils

.getRequiredWebApplicationContext(session.getServletContext());

3,要使HttpSessionListener启作业,还需要在wex.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>CampusCRM</display-name>
<!-- struts2配置 -->
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>300</session-timeout>
</session-config>
<!--Spring相关配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
</context-param>
<!-- spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>cn.com.xinli.crm.core.UserOnline</listener-class>
</listener>
<!--配置定时任务 -->
<servlet>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>


这样就完工了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: