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

📄 gatherimpl.java

📁 一个信息采集系统...对刚接触JSP和JAVA的人来说还有用
💻 JAVA
字号:
package com.briup.inf.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import com.briup.exception.GatherException;
import com.briup.inf.Config;
import com.briup.inf.Gather;
import com.briup.pojo.BIDR;

/**
 * class GatherImpl
 * 
 * @author Jimmy Zhou
 * @Date 2008-1-15 上午10:52:17
 */
public class GatherImpl implements Gather {

	// 定义实例变量保存文件路径
	private String data_source_file_url = "";

	private String data_bak_file_url = "";

	private String data_position_file_url = "";

	// 计费的记录
	private Collection bidrs = new ArrayList();

	// 具体采集服务器的IP
	private String labIP;

	// 上下线记录, key为用户名和IP连接之后的字符串,
	private Map loginBidrs = new HashMap();

	public GatherImpl(Properties pro) {
		data_source_file_url = pro.getProperty("data_source_file_url");
		data_bak_file_url = pro.getProperty("data_bak_file_url");
		data_position_file_url = pro.getProperty("data_position_file_url");

		try {
			labIP = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public Collection doGather() throws GatherException {
		// 如果文件不存在,就创建一个
		createFiles();
		// 取备份的数据
		Map bakDatas = getBakDatas();
		if (bakDatas != null) {
			loginBidrs.putAll(bakDatas);
		}
		RandomAccessFile raf = null;
		// 取定位的数据
		long position = getPosition();
		System.out.println("[GetPosition:]" + position);
		try {
			raf = new RandomAccessFile(data_source_file_url, "rw");
			raf.seek(position);
			String dataStr = null;
			while ((dataStr = raf.readLine()) != null) {
				String[] datas = dataStr.split(":");
				if (datas.length == 9 && datas[0] != null && datas[4] != null
						&& datas[5] != null && datas[8] != null) {
					String key = datas[0] + datas[8];
					if ("7".equals(datas[4])) {
						// 已经有用户登录
						if (loginBidrs.containsKey(key)) {
							BIDR bidr = (BIDR) loginBidrs.get(key);
							bidr.setLoginNum(bidr.getLoginNum() + 1);
						} else {
							// 有7没8时候,logoutData为空
							BIDR bidr = new BIDR(datas[0], datas[8], new Date(
									Long.parseLong(datas[5] + "000")), null,
									labIP, 0);
							loginBidrs.put(key, bidr);
						}
					}
					if ("8".equals(datas[4])) {
						BIDR bidr = (BIDR) loginBidrs.get(key);
						if (bidr != null) {
							// 计出登出时间时,只需要将最早登录和最晚登出的记录插入即可。
							bidr.setLoginNum(bidr.getLoginNum() - 1);
							if (bidr.getLoginNum() == 0) {
								bidr.setLogoutDate(new Date(Long
										.parseLong(datas[5] + "000")));
								bidr.setTimeDuration((bidr.getLogoutDate()
										.getTime() - bidr.getLoginDate()
										.getTime())
										/ (60 * 1000));
								bidrs.add(bidr);
								// 移除map中的值
								loginBidrs.remove(key);
							}
						}
					}
				}
			}
			// 保存此次此采集结束位置
			savePosition(raf.length());
		} catch (Exception e) {
			e.printStackTrace();
			throw new GatherException(e);
		} finally {
			if (raf != null) {
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		// 判断是否还有7没有匹配,人为产生8,7,进行分割计费
		if (!loginBidrs.isEmpty()) {
			Iterator iter = loginBidrs.values().iterator();
			while (iter.hasNext()) {
				BIDR bidr = (BIDR) iter.next();
				bidr.setLogoutDate(new Date(System.currentTimeMillis()));
				bidr.setTimeDuration((bidr.getLogoutDate().getTime() - bidr
						.getLoginDate().getTime())
						/ (60 * 1000));
				bidrs.add(bidr);

				// 构建新对象进行备份
				BIDR newBidr = new BIDR(bidr.getLoginName(), bidr.getLoginIp(),
						bidr.getLogoutDate(), null, bidr.getLabIp(), 0);
				loginBidrs
						.put(bidr.getLoginName() + bidr.getLoginIp(), newBidr);
			}
			// 备份有7没8的数据
			saveBakData(loginBidrs);
		}
		return bidrs;
	}

	private void createFiles() {
		File filePosition = new File(data_position_file_url);
		File fileBak = new File(data_bak_file_url);
		if (!filePosition.exists()) {
			try {
				filePosition.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (!fileBak.exists()) {
			try {
				fileBak.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void saveBakData(Map loginDatas) {
		try {
			if (loginDatas != null) {
				ObjectOutputStream oos = new ObjectOutputStream(
						new FileOutputStream(data_bak_file_url));
				oos.writeObject(loginDatas);
				oos.flush(); // 把缓存中的数据序列化到文件中
				oos.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Map getBakDatas() {
		Map bakDatas = new HashMap();
		File fileBak = new File(data_bak_file_url);
		if (fileBak.length() >= 1) {
			try {
				ObjectInputStream ois = new ObjectInputStream(
						new FileInputStream(data_bak_file_url));
				bakDatas = (Map) ois.readObject();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			bakDatas = null;
		}
		return bakDatas;
	}

	public void savePosition(long position) {
		try {
			RandomAccessFile raf = new RandomAccessFile(data_position_file_url,
					"rwd");
			raf.writeLong(position);
			raf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private long getPosition() {
		File filePosition = new File(data_position_file_url);
		long position = 0L;
		if (filePosition.length() > 0) {
			try {
				RandomAccessFile raf = new RandomAccessFile(
						data_position_file_url, "r");
				position = raf.readLong();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			position = 0L;
		}
		return position;
	}

	public static void main(String[] agrs) {
		Properties pro = new Properties();
		/*
		 * pro.getProperty("data_source_file_url");
		 * pro.getProperty("data_bak_file_url");
		 * pro.getProperty("data_position_file_url"); GatherImpl g = new
		 * GatherImpl(pro); g.getPosition(); g.savePosition(10);
		 * System.out.println(g.getPosition());
		 */
		pro.setProperty("config_file_path", "src/com/briup/config.xml");
		Config config = ConfigImpl.newInstance(pro);
		try {
			Gather g = config.getGather();
			Collection str = g.doGather();
			System.out.println("[strSize]:" + str.size());
			Iterator iter = str.iterator();
			BIDR bidr = null;
			while (iter.hasNext()) {
				bidr = (BIDR) iter.next();
				System.out.println(bidr.getTimeDuration());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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