1.创建监听的类,实现servlet监听的接口ServletContextListener,启动后创建定时执行方法
/FlexAppToJava/src/com/service/userList/UserCheckLinster.java
代码如下:
package com.service.userList;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class UserCheckLinster implements ServletContextListener {
private java.util.Timer timer = null ;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
timer = new java.util.Timer(true) ;
timer.schedule(new MyTask(event.getServletContext()), 0, 5000);
}
}
2.执行任务的类文件
/FlexAppToJava/src/com/service/userList/MyTask.java
代码如下:
package com.service.userList;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TimerTask;
import javax.servlet.ServletContext;
public class MyTask extends TimerTask {
private static boolean isRunning = false;
private ServletContext context = null;
private HashMap<String, Date> onLinehashMap=UserListData.getInstance().onLineUserMap;
private HashMap<String, Date> downLineUserMap=UserListData.getInstance().downLineUserMap;
private List<User> allUserList=UserListData.getInstance().allUserList;
public MyTask(ServletContext servletContext) {
this.context = servletContext ;
}
@Override
public void run() {
if (! isRunning){
isRunning = true ;
checkUserList();
updateAllList();
isRunning = false;
}else{
}
}
//-----------自定义方法--------------
private void checkUserList(){
Date nowDate=new Date();
Set<String> keySet=onLinehashMap.keySet();
for(String userName:keySet){
Date lastDate=onLinehashMap.get(userName);
long between=nowDate.getTime()-lastDate.getTime();
if(between>1000*60*1){
//
onLinehashMap.remove(userName);
downLineUserMap.put(userName, nowDate);
}
}
}
private void updateAllList(){
List<User> newUserList=new ArrayList<User>();
//
Set<String> keySet=onLinehashMap.keySet();
for(String userName:keySet){
User user=new User();
user.setUserName(userName);
user.setDataTime(onLinehashMap.get(userName));
user.setIsOnline(true);
newUserList.add(user);
}
//
keySet=downLineUserMap.keySet();
for(String userName:keySet){
User user=new User();
user.setUserName(userName);
user.setDataTime(onLinehashMap.get(userName));
user.setIsOnline(false);
newUserList.add(user);
}
allUserList.clear();
allUserList.addAll(newUserList);
}
}
3.存放缓存数据的类
/FlexAppToJava/src/com/service/userList/UserListData.java
代码如下:
package com.service.userList;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* 数据类
* @author acer
*/
public class UserListData {
private static UserListData userListData;
public HashMap<String,Date> onLineUserMap=new HashMap<String,Date>();
public HashMap<String,Date> downLineUserMap=new HashMap<String,Date>();
public List<User> allUserList=new ArrayList<User>();
private UserListData(){
}
public static UserListData getInstance(){
if(userListData==null){
userListData=new UserListData();
}
return userListData;
}
}
4.javaBean类
/FlexAppToJava/src/com/service/userList/User.java
代码如下:
package com.service.userList;
import java.util.Date;
public class User {
private String userName;
private Date dataTime;
private Boolean isOnline;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getDataTime() {
return dataTime;
}
public void setDataTime(Date dataTime) {
this.dataTime = dataTime;
}
public Boolean getIsOnline() {
return isOnline;
}
public void setIsOnline(Boolean isOnline) {
this.isOnline = isOnline;
}
}
5.修改web.xml文件,添加
<listener>
<listener-class>com.service.userList.UserCheckLinster</listener-class>
</listener>