多线程记录本地数据库日志

静态实例BusiLog类

import org.apache.log4j.Logger;

import com.bean.LogInfoBean;
import com.common.jdbc.interfaces.JdbcOperation;
import com.common.web.HintInfoHunterThread;
import com.manager.LogManager;
@SuppressWarnings("static-access")
public class BusiLog {
	//正常日志标识
	public static final String SUCCESS = "0";
	//异常常日志标识
	public static final String EXCEPTION = "1";
	// 单实例
	private static BusiLog busiLog;
	// 数据库操作对象
	private static JdbcOperation jdbcOperation;
	//构造方法
	private BusiLog()
	{
		busiLog = this;												         busiLog.logManager = this.logManager;		
	}
	// 持久化
	private static LogManager logManager;
	/***
	 * 日志对象
	 */
	private static final Logger LOGGER = Logger.getLogger(BusiLog.class);
	public void busiLogInit() {
			}
	public static void log(LogInfoBean logInfoBean ){
		try{
			//取得单例线程池
			ThreadPool threadPool = ThreadPool.getInstance();
			//创建一个新的线程实例
			HintInfoHunterThread hunterThread = new HintInfoHunterThread(logInfoBean,logManager);
			//用线程池中的线程运行线程实例
			threadPool.executorService.execute(hunterThread);
		}catch(Exception e){
			LOGGER.error(e.getMessage());
		}
	}
	/**
	 * 返回异常对象Throwable详细信息
	 * @param e Throwable
	 */
	public static String getExceptionInfo(Throwable e)
	{
		StringBuffer excepMesSb=new StringBuffer();
		excepMesSb.append(e.toString());
		StackTraceElement [] messages=e.getStackTrace();
		for(int i=0;i<messages.length;i++){
			excepMesSb.append("\n"+messages[i].toString());
		}
		Exception causeE=(Exception)e.getCause();
		if(null!=causeE){
			excepMesSb.append("\n"+causeE.toString());
			StackTraceElement [] causeEMes=causeE.getStackTrace();
			for(int i=0;i<causeEMes.length;i++){	
				excepMesSb.append("\n"+causeEMes[i].toString());
			}
		}
		return excepMesSb.toString();
	}
	public void setLogManager(LogManager logManager) {
		this.logManager = logManager;
	}
	public void setJdbcOperation(JdbcOperation jdbcOperation)
	{
		this.jdbcOperation = jdbcOperation;
	}
}

线程池单例

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 
 * 单例创建线程池
 * 
 * @author zKF44102
 * @version 2012-1-16
 * @see ThreadPool
 * @since
 */
public class ThreadPool
{
	
	private static ThreadPool tdl = null;
	public ExecutorService executorService = null;
	
	private ThreadPool(){
		executorService= Executors.newFixedThreadPool(20);
	}
	
	public static synchronized ThreadPool getInstance(){
		if(tdl == null){
			tdl = new ThreadPool();
		}
		return tdl;
	}
	
}

BEAN对象LogInfoBean

数据库JDBC连接池JdbcOperation

数据入库线程对象HintInfoHunterThread

import java.util.Date;

import org.apache.log4j.Logger;

import com.bean.LogInfoBean;
import com.manager.LogManager;


/**
 * 用于记录日志的新线程
 */
public class HintInfoHunterThread extends Thread
{
	private LogInfoBean logBeans;
	
	private LogManager logManager;
	
	private final Logger logger = Logger.getLogger(getClass());

	public HintInfoHunterThread(LogInfoBean logBean,LogManager logManage)
	{
		super.setName("loginfo-insert-"+(new Date()).getTime());
		this.logBeans = logBean;
		this.logManager = logManage;
	}

	public void run ()
	{
		logger.info("--------------**ThreadName:"+Thread.currentThread().getName()+"---------[start]----------------------------");
		if (null != logBeans){
			try{
				logManager.addLog(logBeans);
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		logger.info("--------------**ThreadName:"+Thread.currentThread().getName()+"---------[end]----------------------------");
		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值