ArrivalThread.java 3.5 KB
package com.bsth.vehicle.gpsdata;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bsth.util.DateUtils;
import com.bsth.util.db.DBUtils_MS;
import com.bsth.vehicle.directive.service.DirectiveService;
import com.bsth.vehicle.gpsdata.arrival.MatchService;
import com.bsth.vehicle.gpsdata.buffer.ArrivalDataBuffer;
import com.bsth.vehicle.gpsdata.entity.ArrivalInfo;
import com.bsth.websocket.handler.RealControlSocketHandler;

/**
 * 
 * @ClassName: GpsArrivalStationThread 
 * @Description: TODO(定时从数据库加载到离站数据) 
 * @author PanZhao
 * @date 2016年6月27日 上午10:58:13 
 *
 */	
@Component
public class ArrivalThread extends Thread{

	Logger logger = LoggerFactory.getLogger(this.getClass());
	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	
	@Autowired
	DirectiveService directiveService;
	
	@Autowired
	RealControlSocketHandler socketHandler;
	
	Long diff = 1000 * 60 * 20L;
	
	@Override
	public void run() {
		List<ArrivalInfo> list = null;
		try {
			list = loadData();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//缓存
		ArrivalDataBuffer.putAll(list);
		
		//有新到离站数据的线路
		Set<String> newSet = new HashSet<>();
		for(ArrivalInfo arr : list)
			newSet.add(arr.getLineCode());
		
		//启动对应的匹配线程
		for(String lineCode : newSet){
			MatchService.addService(lineCode);
		}
	}
	
	
	/**
	 * @throws ParseException 
	 * 
	 * @Title: loadData 
	 * @Description: TODO(从数据库加载到离站信息) 
	 * @return List<ArrivalInfo>    返回类型 
	 * @throws
	 */
	private List<ArrivalInfo> loadData() throws ParseException{
		Calendar cal = Calendar.getInstance();
		//周数,表分区字段
		int weeks_year = cal.get(Calendar.WEEK_OF_YEAR);
		//按时间标记增量加载
		if(null == ArrivalDataBuffer.markTime){
			//第一次从当天0点开始
			ArrivalDataBuffer.markTime = DateUtils.getTimesmorning() * 1000L;
		}
		
		Long t = System.currentTimeMillis();
		String sql = "select * from bsth_c_arrival_info where weeks_year=? and create_timestamp > ? and create_timestamp <=? order by create_date";
		
		List<ArrivalInfo> list = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBUtils_MS.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, weeks_year/*30*/);
			ps.setLong(2, ArrivalDataBuffer.markTime/*1461361377000L*/);
			ps.setLong(3, t/*30*/);
			
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				list.add(new ArrivalInfo(rs.getString("device_id"), rs.getLong("ts"), rs.getString("line_id")
						, rs.getInt("up_down"), rs.getString("stop_no"), rs.getInt("in_out"), rs.getLong("create_date")
						, rs.getInt("weeks_year")));
			}
			
			logger.info("获取 到离站 数量:" + list.size() + ", st: " + ArrivalDataBuffer.markTime + " , et: " + t);
			//时间戳标记
			ArrivalDataBuffer.markTime = t;
			
		} catch (Exception e) {
			logger.error("", e);
		}finally {
			DBUtils_MS.close(rs, ps, conn);
		}
		return list;
	}
}