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

📄 crefreshdatathread.java

📁 刷新数据的线程,有利于熟悉java线程的使用
💻 JAVA
字号:
package com.cnc.push;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import com.lgm.framework.util.*;

public class CRefreshDataThread extends Thread {
		
	private static CRefreshDataThread m_thread = null;
	private static int     m_max_num  = 10000  ;
	private static String  m_cur_day  = null   ;
	private static int     m_run_time = 042500 ;
	private static boolean m_run_flag = false  ;
	
	
	public static void print_msg(String sMsg){
		String s_cur_time = CommFunc.getCurDateTime();
		System.out.println(s_cur_time);
		System.out.println(sMsg);
		
	}
	
	public static synchronized void startThread(){
		if(m_thread==null) {
			m_thread = new CRefreshDataThread();
			m_thread.start();
		}
		m_thread.resume();
	}
	
	/**
	* 线程主函数
	*/
	public void run(){
		Connection conn      = null;
		Statement  st        = null;
		
		String s_cur_time    = null;
		String s_cur_day     = null;
		String s_start_date  = null;
		String s_end_date    = null;
		int    i_cur_time    = 0   ;
		boolean bRunFlag     = false;
		boolean bTrace       = false;
		try{
			while( true ){
				i_cur_time = 0   ;
				bRunFlag   = false;
				s_cur_time = CommFunc.getCurTime("");
				s_cur_day  = CommFunc.getCurDate();
				
				if(m_cur_day==null || m_cur_day.equals(""))  bRunFlag     = true;
				if(m_cur_day==null || !m_cur_day.equals(s_cur_day)){
					m_run_flag = false    ;
					m_cur_day  = s_cur_day;
				}
				try{
					i_cur_time = Integer.parseInt(s_cur_time);
				}catch(Exception e1){
					print_msg("Convert Time String to Integer Error:" + e1.getMessage());
				}
				
				
				if(!m_run_flag && i_cur_time>=m_run_time ){
					bRunFlag = true;	
				}
				if(!bRunFlag){
					Thread.sleep(30000);
					continue;
				}
				//设置用户资料为脏标志
				CCacheInfo.setUserDirtyFlag(true);
				
				try{
					s_start_date= CommFunc.getCurDateTime();
					conn = DbFunc.getConnection(true);
					st   = conn.createStatement();
					refreshData    ( st  , bTrace);
					refreshUserRole( st  , true  );
					
					//设置用户资料为正常标志
					CCacheInfo.setUserDirtyFlag(false);
				}catch(Exception e1){
					print_msg("Update User DirtyFlag= False" + e1.getMessage());
				}finally{
					try{if(st!=null) st.close();}catch(Exception e){}
					DbFunc.free(conn,1);
					st   = null;		
					conn = null;
				}
				s_end_date = CommFunc.getCurDateTime();
				m_run_flag = true;
				print_msg("CRefreshDataThread-->Refresh Time Spam : "+s_start_date+ " - " + s_end_date);
				this.sleep(1000);
			}
		}catch(Exception ex){
			System.out.println("CRefreshDataThread Write log file Error:" + ex.getMessage());
		}
		return;
	}
	
	//取得用户角色信息
	public static void refreshUserRole(Statement  st,boolean bTrace){
		
		ResultSet  rs        = null;
		String     strSQL    = null;
		String     user_id   = null;
		String     pppoe_acct= null;
		String     role_id   = null;
		String     is_awoke  = null;
		try{
			strSQL  = "select sr.user_id ,tnxa.pppoe_acct, nvl(sr.role_id,'1') role_id ,tr.is_awoke ,tr.priority \n"
					+ "from tbm_staff_role sr,tbm_role tr,trm_net_xdsl_ask tnxa  \n"
					+ "where sr.role_id = tr.role_id  \n"
					+ "and   sr.user_id = tnxa.user_id \n"
					+ "order by sr.user_id,tr.priority desc  \n"
					;
			if(bTrace) print_msg("CRefreshDataThread-->\n"+strSQL);

			if(st!=null) rs     =  st.executeQuery( strSQL);
			if(rs != null && rs.next()) {
				user_id    = rs.getString("user_id");
				pppoe_acct = rs.getString("pppoe_acct");
				role_id    = rs.getString("role_id");
				is_awoke   = rs.getString("is_awoke");
				
				if(role_id==null) role_id = "1";
				CCacheInfo.setUserRole(pppoe_acct,role_id);
			}
			if(rs!=null) rs.close();
			rs = null;
		}catch(Exception e1){
			if(bTrace) print_msg("CRefreshDataThread-->refresh User Role" + e1.getMessage());
		}finally{
			try{if(rs!=null) rs.close();}catch(Exception e){}
			rs  = null;
		}
			
	}
	//取得用户基本信息
	public static void refreshData(Statement  st,boolean bTrace){
		
		ResultSet  rs     = null;
		ResultSetMetaData rsdm = null;
		ArrayList  aList  = null;
		String     strSQL = null;
		String     strSelSQL = null;
		int        iExistNum = 0;
		int        iStartIdx = 0;
		int        iEndIdx   = 0;
		int        iCurIdx   = 0;
		int jLoop    = 0;
		int jLoopNum = 0;	
		long iTotalMemory = 0;
		long iFreeMemory  = 0;


		HashMap hashInfo   = null;
		String f_name      = null;
		String f_value     = null;
		String s_start_date= null;
		String s_end_date  = null;
		String acct_name   = null;
		strSelSQL =   "  select \n"
					+ "    rownum     row_idx ,\n"
					+ "    tnu.user_id        ,\n"
					+ "    tnu.user_seq       ,\n"
					+ "    tnu.ord_no         ,\n"
					+ "    tnu.bureau_no      ,\n"
					+ "    tnb.bureau_name    ,\n"					
					+ "    tnu.user_branch_no ,\n"
					+ "    tnu.region_no      ,\n"
					+ "    tnu.cust_id        ,\n"
					+ "    tnxa.pppoe_acct    ,\n"
					+ "    tnu.user_name      ,\n"
					+ "    tnu.num            ,\n"
					+ "    tnu.num_state      ,\n"
					+ "    tnu.prod_no        ,\n"
					+ "    tnu.prod_no  prod_name ,\n"
					+ "    tnu.user_type      ,\n"
					+ "    tnu.user_credit    ,\n"
					+ "    tnu.finish_flag    ,\n"
					+ "    to_char(tnu.open_date,'yyyy-mm-dd') open_date,\n"
					+ "    to_char(tnu.eff_date ,'yyyy-mm-dd') eff_date,\n"
					+ "    to_char(nvl(tnxa.expire_date,tnu.exp_date),'yyyy-mm-dd') exp_date , \n"
					+ "    to_char(tnu.join_date,'yyyy-mm-dd') join_date,\n"
					+ "    toi.owe_charge     ,\n"
					+ "    tnu.addr_detail    ,\n"
					+ "    tnu.state          ,\n"
					+ "    tnu.oper_date      ,\n"
					+ "    tnu.card_type      ,\n"
					+ "    tnu.card_no        ,\n"
					+ "    tnu.bill_tel       ,\n"
					+ "    tnu.city_no        ,\n"
					+ "    tnu.out_date       ,\n"
					+ "    tnu.contract_id    ,\n"
					+ "    tnu.prepay_flag    ,\n"
					+ "    tnu.stop_state     ,\n"
					+ "    tnu.bill_date      ,\n"
					+ "    '1'  role_id       ,\n"
					+ "    to_char(nvl(tnxa.expire_date,tnu.exp_date),'yyyy-mm-dd') expire_date , \n"
					+ "    to_char(tnxa.pay_date ,'yyyy-mm-dd') pay_date , \n"
					+ "    nvl(tnxa.pay_amt/100,0) pay_amt , \n"
					+ "    ceil(nvl(tnxa.expire_date,tnu.exp_date)-sysdate) span_days, \n"
					+ "    to_char(si.last_push_date,'yyyy-mm-dd') last_push_date,\n"
					+ "    to_char(si.last_push_date,'yyyy-mm-dd HH24:MI:SS') last_push_date_name,\n"
					+ "    ceil(decode(si.last_push_date,null,-1,sysdate-si.last_push_date)) push_days \n"
					+ "  from trm_net_user tnu ,trm_net_xdsl_ask tnxa,trm_net_bureau tnb ,\n"
					+ "       trm_net_owe_item toi,tbm_staff_info si \n"
					+ "  where tnu.user_id  =  tnxa.user_id \n"
					+ "  and   tnu.user_id  =  toi.user_id   (+)   \n"
					+ "  and   to_char(tnu.user_id)=  si.user_id  (+)  \n"
					+ "  and   tnu.bureau_no=  tnb.bureau_no (+) \n"
					;
		try{
			try{
				strSQL  = "select count(*) exist_num \n"
					+ "from trm_net_user tnu ,trm_net_xdsl_ask tnxa,trm_net_bureau tnb  \n"
					+ "where tnu.user_id  =  tnxa.user_id \n"
					+ "and   tnu.bureau_no=  tnb.bureau_no (+) "
					;
				print_msg("CRefreshDataThread-->\n"+strSQL);

				if(st!=null) rs     =  st.executeQuery( strSQL);
				if(rs != null && rs.next()) {
					iExistNum = rs.getInt("exist_num");
				}
				if(rs!=null) rs.close();
				rs = null;
			}catch(Exception e1){
				if(bTrace) print_msg("CRefreshDataThread-->refreshData" + e1.getMessage());
			}finally{
				try{if(rs!=null) rs.close();}catch(Exception e){}
				rs  = null;
			}
			
			
			print_msg("CRefreshDataThread-->Data RecordCount="+iExistNum);
			
			iStartIdx = 0;
			iCurIdx   = 0;
			//iExistNum = 15000;
			hashInfo  = new HashMap();
			while (iStartIdx<iExistNum){
				iEndIdx = iStartIdx + m_max_num;
				s_start_date= CommFunc.getCurDateTime();
				strSQL  = "select * from ( \n"
						+ strSelSQL
						+ ")ta \n"
						+ "where row_idx>" + iStartIdx+ " \n" 
						+ "and   row_idx<="+ iEndIdx  + "\n";
					iStartIdx = iEndIdx ;
					jLoopNum  = 0;
					try{
						if(iCurIdx==0 || bTrace) print_msg("CRefreshDataThread-->"+strSQL);
						
						if(st!=null) rs     =  st.executeQuery( strSQL);
						if(rs!=null) rsdm   =  rs.getMetaData();
						if(rsdm!=null) jLoopNum = rsdm.getColumnCount();
						
						while(rs != null && rs.next()) {
							hashInfo.clear();
							iCurIdx ++;
							for (jLoop=1;jLoop<=jLoopNum;jLoop++){
								f_name  = rsdm.getColumnName(jLoop);
								f_value = rs.getString(jLoop); 
								if(f_name==null) continue;
								f_name = f_name.toLowerCase();
								hashInfo.put(f_name,f_value);
								
							}
							acct_name = (String)hashInfo.get("pppoe_acct");
							if(acct_name==null) continue;
							acct_name = acct_name.trim();
							if(acct_name.equals("")) continue;
							CCacheInfo.putUserInfo(acct_name,hashInfo);
						}
						if(rs!=null) rs.close();
						rs = null;
					}catch(Exception e1){
						if(bTrace) print_msg("CRefreshDataThread-->refreshData-->Get User Num" + e1.getMessage());
					}finally{
						try{if(rs!=null) rs.close();}catch(Exception e){}
						rs  = null;
					}
					s_end_date  = CommFunc.getCurDateTime();
					iTotalMemory = Runtime.getRuntime().totalMemory()/1024;
					iFreeMemory  = Runtime.getRuntime().freeMemory()/1024;
					print_msg("[" + s_start_date + "]-[" + s_end_date + "] Thread.activeCount="+Thread.activeCount() 
							+ "\n iCurIdx=" + iCurIdx 
							+ " TotalMemory=" + String.valueOf(iTotalMemory)
							+ " FreeMemory=" + String.valueOf(iFreeMemory)
							+ " iStartIdx=" + iStartIdx 
							+ " iEndIdx="   + iEndIdx 
							+ " iExistNum="   + iExistNum 
							);
					if(iEndIdx>iExistNum) break;
			}
		}finally{
			try{if(rs!=null) rs.close();}catch(Exception e){}
			if(hashInfo!=null) hashInfo.clear();
			
			hashInfo = null;
			rs       = null;
		}
		
	}
	

   public static void main(String[] args)
   {
	   CRefreshDataThread s = new CRefreshDataThread();
	  
	   System.out.println("");
	/*   Date date = new Date();
	   
	   SimpleDateFormat _SimpleDateTimeFormat   =   new   SimpleDateFormat("yyyyMMddHHMMSS");  
      String fileName = logSrcPath+_SimpleDateTimeFormat.format(date).toString()+".txt";
	   System.out.println("**\n"+fileName);*/
   }
}

⌨️ 快捷键说明

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