MinerStoreThread.java 存储线程

MinerStoreThread.java 存储线程

package com.iteye.injavawetrust.miner;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 存储线程
 * @author InJavaWeTrust
 *
 */
public class MinerStoreThread extends Thread {
	
	private static final Log LOG = LogFactory.getLog(MinerStoreThread.class);
	
	private MinerDB mdb = MinerDB.getInstance();
	
	private MinerConfig config = null;
	
	public MinerStoreThread(MinerConfig config) {
		this.config = config;
	}
	
	public void run() {
		while(!MinerMonitorThread.done){
			store();
		}
	}
	
	public synchronized void store(){
		switch(config.getStoreType().getType()) {
		case MinerConstanits.DB : dbStore(); break;
		case MinerConstanits.FILE : fileStore(); break;
		default : return;
		}
	}
	/**
	 * 数据库存储
	 */
	public synchronized void dbStore() {
		Html html = MinerQueue.storePoll();
		if(null == html || MinerUtil.isBlank(html.getHtml())){
			return;
		}
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("html", html.getHtml());
		String title = MinerUtil.fileName(html.getUrl());
		if(title == null || title.length() > 255){
			return;
		}
		map.put("title", title);
		MinerUtil.getHtmlToLocal(map);
		
		MinerBean bean = new MinerBean();
		bean.setId(mdb.getUUID());
		bean.setBatchNo(MinerUtil.getToday());
		String path = (MinerConstanits.HTMLPATH + MinerUtil.getToday()).replaceAll("\\\\", "\\\\\\\\");
		bean.setFilePath(path);
		bean.setFileName(title);
		String insertSql = mdb.getInsertSql(bean);
		mdb.insert(insertSql);
		
		LOG.info("MinerStoreThread 当前线程[" + Thread.currentThread().getName() + "]");
	}
	/**
	 * 文件存储
	 */
	public synchronized void fileStore() {
		Html html = MinerQueue.storePoll();
		if(null == html || MinerUtil.isBlank(html.getHtml())){
			return;
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("html", html.getHtml());
		String title = MinerUtil.fileName(html.getUrl());
		if(title == null || title.length() > 255){
			return;
		}
		map.put("title", title);
		MinerUtil.getHtmlToLocal(map);
		LOG.info("MinerStoreThread 当前线程[" + Thread.currentThread().getName() + "]");
	}

}

 

返回列表

 

posted on 2016-03-27 09:35  木鱼哥  阅读(204)  评论(1编辑  收藏  举报

导航