使用线程读取配置文件配置参数

1、配置文件中配置参数,可以动态增加

#参数
viewContent=432000
queryProductInfoEx=432000
viewContentType=432000

 

2、利用线程同步文件中的配置数据,可以重构成单例模式。

package com.mport.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

/**
 * 配置过效时间
 * 
 * @author Administrator
 * 
 */
public class ParamSynFile {

	private static Logger log = Logger.getLogger(ParamSynFile.class);

	private static long expireTimeLastModifyTime = 0, phoneNumsInWhitePaperLastModifyTime = 0;
	private Map<String, Integer> expireTimeMap = new HashMap<String, Integer>();
	private List<String> phoneNumsInWhitePaper = new ArrayList<String>();
	private ExecutorService threadPool = Executors.newFixedThreadPool(1);
	private String expireTimeLastFilePath = null, phoneNumsInWhitePaperFilePath = null;

	public ParamSynFile() {
		expireTimeLastFilePath = ParamSynFile.class.getClassLoader().getResource("memcache_expire_time.txt").getFile();
		File file = new File(expireTimeLastFilePath);
		// phoneNumsInWhitePaperFilePath = ParamSynFile.class.getClassLoader().getResource("phone_numbers_in_white_paper.txt").getFile();
		// File phoneNumsInWhitePaperFile = new File(phoneNumsInWhitePaperFilePath);
		FileInputStream in = null;
		try {
			in = new FileInputStream(file);
			expireTimeMap = readExpireTimeConfigFile(in);

			// in = new FileInputStream(phoneNumsInWhitePaperFile);
			// phoneNumsInWhitePaper = readNumsInWhitePaperFile(in);

			Thread synFileThread = new Thread(new Runnable() {

				public void run() {
					FileInputStream in = null;
					File expireTimeLastFile = null;
					// File phoneNumsInWhitePaperFile = null;
					while (true) {
						try {
							try {
								// 读取memcache的过期在时间配置参数
								expireTimeLastFile = new File(expireTimeLastFilePath);
								long expireTimeLastModifyTime1 = expireTimeLastFile.lastModified();
								if (expireTimeLastModifyTime == 0 || expireTimeLastModifyTime < expireTimeLastModifyTime1) {
									expireTimeLastModifyTime = expireTimeLastModifyTime1;
									log.info("memcache_expire_time.txt last modify time:" + expireTimeLastModifyTime);
									in = new FileInputStream(expireTimeLastFile);
									expireTimeMap = readExpireTimeConfigFile(in);
									log.info("memcache_expire_time.txt new config: " + expireTimeMap.toString());
								}

								// 读取白名单用户
								// phoneNumsInWhitePaperFile = new File(phoneNumsInWhitePaperFilePath);
								// long phoneNumsInWhitePaperLastModifyTime1 = phoneNumsInWhitePaperFile.lastModified();
								// if (phoneNumsInWhitePaperLastModifyTime == 0 || phoneNumsInWhitePaperLastModifyTime < phoneNumsInWhitePaperLastModifyTime1) {
								// phoneNumsInWhitePaperLastModifyTime = phoneNumsInWhitePaperLastModifyTime1;
								// log.info("phone_numbers_in_white_paper.txt last modify time:" + phoneNumsInWhitePaperLastModifyTime);
								// in = new FileInputStream(phoneNumsInWhitePaperFile);
								// phoneNumsInWhitePaper = readNumsInWhitePaperFile(in);
								// log.info("phone_numbers_in_white_paper.txt new numbers: " + phoneNumsInWhitePaper.toString());
								// }
							} catch (Exception e) {
								e.printStackTrace();
							} finally {
								if (in != null) {
									in.close();
								}
							}
							Thread.sleep(10 * 1000);
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				}
			});
			threadPool.execute(synFileThread);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public ParamSynFile(int i) {

	}

	/**
	 * 读取白名单用户
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	private static List<String> readNumsInWhitePaperFile(InputStream in) throws Exception {
		List<String> phoneNumsInWhitePaper = new ArrayList<String>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String buf = null;
		Pattern pattern = Pattern.compile("(\\d+)");
		while ((buf = reader.readLine()) != null) {
			Matcher matcher = pattern.matcher(buf);
			if (matcher.find() && NumberUtils.isNumber(matcher.group(1))) {
				phoneNumsInWhitePaper.add(matcher.group(1));
			}
		}
		return phoneNumsInWhitePaper;
	}

	/**
	 * 读取memcache的过期在时间配置参数
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	private static Map<String, Integer> readExpireTimeConfigFile(InputStream in) throws Exception {
		Map<String, Integer> expireTimeMap = new Hashtable<String, Integer>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String buf = null;
		Pattern pattern = Pattern.compile("(\\w+)=(\\d+)");
		while ((buf = reader.readLine()) != null) {
			Matcher matcher = pattern.matcher(buf);
			if (matcher.find() && NumberUtils.isNumber(matcher.group(2))) {
				expireTimeMap.put(matcher.group(1), NumberUtils.toInt(matcher.group(2)));
			}
		}
		return expireTimeMap;
	}

	public Integer getExpireTime(String key) {
		Integer exp = expireTimeMap.get(key);
		if (exp == null || exp < 0) {
			exp = 0;
		}
		return exp;
	}

	public List<String> getPhoneNumsInWhitePaper() {
		return phoneNumsInWhitePaper;
	}

	@Override
	protected void finalize() throws Throwable {
		threadPool.shutdown();
		expireTimeMap = null;
		phoneNumsInWhitePaper = null;
		super.finalize();
	}

	public static void main(String[] args) {
		// MemcacheExpireTimeConfig config = MemcacheExpireTimeConfig.getInstance();
		// while(true){
		// System.out.println(config.getExpireTime("viewContent"));
		// System.out.println("PhoneNumsInWhitPapaer size:" + config.getPhoneNumsInWhitPapaer().size());
		// try {
		// Thread.currentThread().sleep(1000);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		// }
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值