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

📄 billhandlerthread.java

📁 自动导入文件到数据库,用于无法实时入库,需要在特定时间大批量入库的程序
💻 JAVA
字号:
package com.onet.autobill.bill;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import com.onet.autobill.bill.convert.ConvertFilter;
import com.onet.autobill.db.DbService;
import com.onet.autobill.model.MachineConfig;
import com.onet.autobill.util.CmdHelper;
import com.onet.autobill.util.DateInterval;


/**
 * 万纳特自动倒库处理类,处理一台BILL服务器上的所有BILL文件的倒库
 * 
 * @author wangzn, linyue, mengwei 2009-03-05
 */
public class BillHandlerThread implements Callable<Boolean>{

	private static Logger logger = Logger.getLogger(BillHandlerThread.class);
	
	private List<ConvertFilter> convertFilterList; 

	private DbService dbService;

	/**
	 * 临时的存放文件目录
	 */
	private String copyPath;

	/**
	 * 日志转换后文件(倒入数据库文件)存放的目录
	 */
	private String targetPath;
	
	SimpleDateFormat simpledateformat= new SimpleDateFormat ("yyyyMMdd");


	
	private MachineConfig machineConfig;
	
	public BillHandlerThread(MachineConfig mc, String copyPath, String targetPath){
		this.machineConfig = mc;
		this.copyPath = copyPath;
		this.targetPath = targetPath;
	}

	/**
	 * 处理一台BILL服务器上的BILL倒库
	 * @return true 处理成功, false 处理失败
	 */
	public Boolean call(){

		if (CmdHelper.getRemoteServerStatus(machineConfig)) {
			logger.info("开始处理" + machineConfig.getMachineLogPath() + "中的文件");
			
			Collection<String> importFileNames = getImportFile(machineConfig);
			//当是空集的时候,要更新LastImportTime和AutobillNode,解决当文件不存在的时候,没有更新
			//	这2个字段的问题
			if(importFileNames==null||importFileNames.isEmpty()){
				dbService.errNotify(0, machineConfig);
			}
				
			
			for(String importFileName : importFileNames)
			{
				if(importFileName!=null){
					try{
						if(machineConfig.getLogCode()==400){
							MoInfoBillHandler billHandler = new MoInfoBillHandler();
							billHandler.setDbService(dbService);
							billHandler.setConvertFilterList(convertFilterList);
							billHandler.importBillFile2DB(importFileName, machineConfig, copyPath, targetPath);
						}else{
							BaseBillHandler billHandler = new BaseBillHandler();
							billHandler.setDbService(dbService);
							billHandler.setConvertFilterList(convertFilterList);
							billHandler.importBillFile2DB(importFileName, machineConfig, copyPath, targetPath);
						}
					}
					catch(Exception e){
						logger.error("", e);
					}
				}
			}
			
			// 拆除服务器连接
			CmdHelper.shutOffServerLink(machineConfig);	
			logger.info(machineConfig.getMachineLogPath()+"路径bill导入成功");
			return new Boolean(true);
		}
		else{
			logger.error(machineConfig.getMachineLogPath()+"路径bill导入失败");
			return new Boolean(false);
		}
	}
	
	
	/**
	 * 查找当前文件夹,并且过滤,得到要处理的文件名称集合
	 * 
	 * @param handlerPath
	 *            要出理的文件的路径
	 * @param mb
	 *            文件配置
	 * @return 当前要处理的文件集合
	 */
	@SuppressWarnings("unchecked")
	private Collection<String> getImportFile(MachineConfig mc) {
		try{
			logger.debug("过滤" + mc.getMachineLogPath() + "中的文件");
			File findFile = new File(mc.getMachineLogPath());
			String[] fileNames = null;
			Collection<String> intersection = null;
			if (findFile != null && findFile.exists()) {
				fileNames = findFile.list();			
				if (fileNames != null && fileNames.length != 0) {
					logger.debug(mc.getMachineLogPath() + "的文件数为" + fileNames.length);
					List<String> fileNamesPossible = getAllLogFileNames(mc);
					List<String> fileNamesList = new ArrayList<String>();
					CollectionUtils.addAll(fileNamesList, fileNames);
					intersection = CollectionUtils.intersection(fileNamesList, fileNamesPossible);
					logger.debug("过滤条件(时间和前缀)的文件为:" + fileNamesPossible.toString());
					logger.debug("满足过滤条件的文件为:" + intersection.toString());				
				}
			} else {
				logger.warn("给定的路径不正确,没有找到" + mc.getMachineLogPath());
			}
			return intersection;
		}catch(Exception e){
			logger.debug("获取文件名列表失败",e);
			return null;
		}
	}

	/**
	 * 得到当前所有需要导入了文件名称的集合,该集合是一个理想集合,也就是按照日期计算的最大集合
	 * @param mc TODO
	 * 
	 * @return 所有文件名称的集合
	 */
 private List<String> getAllLogFileNames(MachineConfig mc) {

		List<String> fileNameList = new ArrayList<String>();
		String sndFileName = null;
		String recFileName = null;
		String nowDate = simpledateformat.format(new Date());
		
		// 以当前日期为准,得到当前日期与给定日期之间所有的日期的集合
		String startDateStr = simpledateformat.format(mc.getLastImportTime());
		String sndPre = mc.getSndPre();
		String recPre = mc.getRecPre();
		int sendOrRecv = mc.getStatus(); // 0表示收,1表示发,2表示收发
		List<String> dateList = DateInterval.getDiffDateList(startDateStr, nowDate);
		if (!dateList.isEmpty()) {
			for (int i = 0; i < dateList.size(); i++) {
				sndFileName = sndPre + "_" + dateList.get(i) + ".bil";
				recFileName = recPre + "_" + dateList.get(i) + ".bil";
				if (sendOrRecv == 0) {
					fileNameList.add(recFileName);
				} else if (sendOrRecv == 1) {
					fileNameList.add(sndFileName);
				} else {
					fileNameList.add(sndFileName);
					fileNameList.add(recFileName);
				}
			}
		}

		return fileNameList;
	}

	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}
	
	public void setConvertFilterList(List<ConvertFilter> convertFilterList) {
		this.convertFilterList = convertFilterList;
	}
	
}

⌨️ 快捷键说明

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