⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermonitor.java

📁 淘客在线客服管理系统(TaokeOCS V3.2)
💻 JAVA
字号:
package msg;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import java.sql.*;  
import msg.*;
public class UserMonitor implements Runnable{	
	 
	private static long monitorInterval = 1 * 20 * 1000; 	 
	private static UserManager userManager=null;
	private static MsgManager msgManager=null;
 	private static DbConn db=null;
	private static boolean threadsOn = false;
	String sql="";
	ResultSet rs=null;	 
	
	public UserMonitor(){	
	   if(threadsOn==false){	   		 
			userManager=new UserManager();
			msgManager=new MsgManager();
	   		threadsOn=true;
			db=new DbConn(); 
			Thread myself=new Thread(this);
			myself.start();			 		
	   }		 
	}
  	public void run(){			 
			while (threadsOn){  				  
				try{							 
					Thread.sleep(monitorInterval); // 
				}catch (InterruptedException e1) {}   
				
				checkVisitorOnlineAndSendMsg(60);	//单位:秒
				checkUserOnlineAndSendMsg(60);		//单位:秒
			}
	}
	public void checkVisitorOnlineAndSendMsg(int maxSeconds){
		try{			 	
			Calendar now= Calendar.getInstance();
			now.setTimeInMillis(now.getTimeInMillis()-maxSeconds*1000);
			 
			String nowStr=String.valueOf(now.get(Calendar.YEAR))+"-"+String.valueOf(now.get(Calendar.MONTH)+1)+"-"+
						  String.valueOf(now.get(Calendar.DAY_OF_MONTH))+" "+String.valueOf(now.get(Calendar.HOUR_OF_DAY))+":"+
						  String.valueOf(now.get(Calendar.MINUTE))+":"+String.valueOf(now.get(Calendar.SECOND));
			sql="select id from visitor where lastTime<\""+nowStr+"\" and state<>\"OFFLINE\"";
			//Log.log(sql);
			if(db==null) db=new msg.DbConn();if(db.getConnection()==null || db.isClosed())			 
				db.setConnection();				  
			db.setSqlQuery(sql);			
			rs=db.getResult();
			String[] ids=null;
			if(rs!=null && rs.last()){
				ids=new String[rs.getRow()];
				rs.beforeFirst();
			}
			for(int i=0;ids!=null &&rs.next() && i<ids.length;i++)
				ids[i]=rs.getString("id");			 
			if(db.getConnection()!=null && !db.isClosed())
				db.closeConnection();
			for(int i=0;ids!=null && i<ids.length;i++){
				userManager.changeVisitorState(ids[i],"OFFLINE");  //改变状态为离线			 
				msgManager.sendSayMeLogoffMsg(ids[i],"visitor");	
			}  				
		}catch(Exception e){
			closeDbConnection();
			Log.log("Class:UserMonitor method: checkVisitorOnlineAndSendMsg() error:"+e.getMessage()+" sql:"+sql);
		}		
	}
	public void checkUserOnlineAndSendMsg(int maxSeconds){
		try{			 	
			Calendar now= Calendar.getInstance();
			now.setTimeInMillis(now.getTimeInMillis()-maxSeconds*1000);
			 
			String nowStr=String.valueOf(now.get(Calendar.YEAR))+"-"+String.valueOf(now.get(Calendar.MONTH)+1)+"-"+
						  String.valueOf(now.get(Calendar.DAY_OF_MONTH))+" "+String.valueOf(now.get(Calendar.HOUR_OF_DAY))+":"+
						  String.valueOf(now.get(Calendar.MINUTE))+":"+String.valueOf(now.get(Calendar.SECOND));
			sql="select id from user where lastTime<\""+nowStr+"\" and state<>\"OFFLINE\"";
			//Log.log(sql);
			if(db==null) db=new msg.DbConn();if(db.getConnection()==null || db.isClosed())			 
				db.setConnection();				  
			db.setSqlQuery(sql);			
			rs=db.getResult();
			String[] ids=null;
			if(rs!=null && rs.last()){
				ids=new String[rs.getRow()];
				rs.beforeFirst();
			}
			for(int i=0;ids!=null &&rs.next() && i<ids.length;i++)
				ids[i]=rs.getString("id");			 
			if(db.getConnection()!=null && !db.isClosed())
				db.closeConnection();
			for(int i=0;ids!=null && i<ids.length;i++){
				userManager.changeUserState(ids[i],"OFFLINE");
				msgManager.sendSayMeLogoffMsg(ids[i],"stuff");		
				msgManager.tellGroupsImOfflineMsg(ids[i]);	 
				msgManager.tellFriendsImOfflineMsg(ids[i]);				
			}	  				
		}catch(Exception e){
			closeDbConnection();
			Log.log("Class:UserMonitor method: checkUserOnlineAndSendMsg() error:"+e.getMessage()+" sql:"+sql);
		}		
	}
	private void closeDbConnection(){
		try{
			if(db.getConnection()!=null && !db.isClosed())
				db.closeConnection();			
		}catch(Exception e){}
	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -