經過Netty通訊,採集設備現場GPS數據,並存放在redis服務器。

主程序代碼以下:java

/*
 /*
 * CopyRight (c) 2013 北京軟秀科技有限公司www.inforwms.com 保留全部權利。 * mail:meslog@qq.com
 */
 */
package com.softshow.product.digi;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InvalidPropertiesFormatException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.util.internal.ConcurrentHashMap;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.softshow.product.cache.DataCache;
import com.softshow.product.db.DatabaseDataManager;
import com.softshow.product.handler.IdentifyChannelDecoder;
import com.softshow.product.handler.InitInfoChannelDecoder;
import com.softshow.product.helper.DateUtils;
import com.softshow.product.helper.GlobalTimer;
import com.softshow.product.helper.HttpClientHelper;
import com.softshow.product.helper.Log;
import com.softshow.product.model.CarInfo;
import com.softshow.product.timer.AutoUpgradTimer;
import com.softshow.product.timer.AutoOrderTimer;
import com.softshow.product.timer.WSAutoOrderTimer;

/**
 * 服務統一管理
 * @author <a href="mailto:meslog@qq.com">meslog</a>
 * @version 1.0.0.2013-10-22
 * 
 */
public class ServerManager {
	private final String cfgPath = "/server.cfg";
	private final List<TrackerServer> serverList = new LinkedList<TrackerServer>();
	private final static ConcurrentMap<Integer,TrackerServer> trackerServerMap = new ConcurrentHashMap<Integer,TrackerServer>();//lizhao
	
	public static ConcurrentMap<Integer,TrackerServer> getTrackerServerMap(){//lizhao
		return trackerServerMap;
	}
	
	public void addTrackerServer(TrackerServer trackerServer) {
		serverList.add(trackerServer);
	}

	private boolean loggerEnabled;

	public boolean isLoggerEnabled() {
		return loggerEnabled;
	}

	private static DataManager dataManager;
	
	public static DataManager getDataManager() {
		return dataManager;
	}

	private static Properties properties;

	public Properties getProperties() {
		return properties;
	}

	private static JedisConnectionFactory redisFactory;

	public static JedisConnectionFactory getRedisFactory() {
		return redisFactory;
	}

	/**
	 * 初始化日誌、緩存、數據庫和Netty服務
	 */
	public void init() throws IOException, ClassNotFoundException, SQLException {
		loadProperties();//加載配置,初始化常量
		
		initLogger(properties);//初始化日誌
		
		dataManager = new DatabaseDataManager(properties);//初始化數據庫鏈接
		Log.info(Constants.SERVER_MAIN, "**database connection initialized......**");
		
		redisFactory = initRedis(properties);//初始化redis緩存		
		initDataCache();//加載redis緩存
		Log.info(Constants.SERVER_MAIN, "**redis load compeleted......**");
		
		initELSServer("fourfaith");//初始化Netty服務
	}
	
	private void loadProperties() throws InvalidPropertiesFormatException, IOException{
		//加載配置文件
		properties = new Properties();
		InputStream in = getClass().getResourceAsStream(cfgPath);
		properties.loadFromXML(in);
		in.close();
		
		//打印版本信息
		Constants.VERSION_CODE = properties.getProperty("connection.version");
		Log.info(Constants.SERVER_MAIN, "===============================system version:["+Constants.VERSION_CODE+"]");
		
		//初始化常量
		initConstant(properties);
	}
	
	/**
	 * 天天定時清理緩存數據
	 */
	private void scheduleDailyGpsCache() {
		final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.afterPropertiesSet();
		Timer timer = new java.util.Timer(true);
		final List<CarInfo> carList = DataCache.getAllCar();
		TimerTask task = new TimerTask() {
			public void run() {
				for (CarInfo car : carList) {
					redisTemplate.delete(Constants.CACHE.TODAY_GPS_COLLECTION_PREFIX + "." + car.getId());
				}
			}
		};
		timer.scheduleAtFixedRate(task, DateUtils.getTomorrowTimeByHour(0), 24 * 60 * 60 * 1000);
	}

	private JedisConnectionFactory initRedis(Properties prop) {
		redisFactory = new JedisConnectionFactory();
		redisFactory.setUsePool(true);
		redisFactory.setPort(Integer.parseInt(prop.getProperty("redis.port")));
		redisFactory.setHostName(prop.getProperty("redis.serverUrl"));
		redisFactory.setDatabase(Integer.parseInt(prop.getProperty("redis.database")));
		redisFactory.afterPropertiesSet();
		return redisFactory;
	}

	/**
	 * 啓動Netty服務和相關定時線程
	 */
	public void start() {
		//啓動Netty Server
		for (Object server : serverList) {
			((TrackerServer) server).start();
		}
		
		//啓動定時器
		startTimer();
	}

	/**
	 * Stop
	 */
	public void stop() {
		for (Object server : serverList) {
			((TrackerServer) server).stop();
		}

		// Release resources
		GlobalChannelFactory.release();
		GlobalTimer.release();
		try {
			redisFactory.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Destroy
	 */
	public void destroy() {
		serverList.clear();
	}

	/**
	 * Initialize logger
	 */
	private void initLogger(Properties properties) throws IOException {
		loggerEnabled = Boolean.valueOf(properties.getProperty("logger.enable"));

		if (loggerEnabled) {
			String serverMainName = properties.getProperty("logger.servermain");
			initLog(serverMainName,Constants.SERVER_MAIN);
			String serverGpsName = properties.getProperty("logger.servergps");
			initLog(serverGpsName,Constants.GPS_LOGGER_SERVER);
			String serverHeartName = properties.getProperty("logger.serverheart");
			initLog(serverHeartName,Constants.HEART_LOGGER_SERVER);
			 
		}
	}
	
	/**
	 * 初始化常量
	 * @param properties
	 * @throws IOException
	 */
	private void initConstant(Properties props) throws IOException {
		String webPrefix = "http://" + props.getProperty("webIP");
		String ctx = props.getProperty("webContext");
		if(ctx!=null && ctx.trim().length()>0){
			webPrefix += "/"+ctx;
		}		
		Constants.query_Order_url = webPrefix + Constants.query_Order_url;
		Constants.setRange_url = webPrefix + Constants.setRange_url;
		Constants.getDeviceParameter_url = webPrefix + Constants.getDeviceParameter_url;
		
		HttpClientHelper.MapbarAPIAddr = props.getProperty("mapBarAPIAddress");
	}

	private boolean isProtocolEnabled(Properties properties, String protocol) {
		String enabled = properties.getProperty(protocol + ".enable");
		if (enabled != null) {
			return Boolean.valueOf(enabled);
		}
		return false;
	}

	private void initServer(String protocol) throws SQLException {//lizhao
		if (isProtocolEnabled(properties, protocol)) {
			TrackerServer trackerServer = new TrackerServer(this, new ServerBootstrap(), protocol) {
				@Override
				protected void addSpecificHandlers(ChannelPipeline pipeline) {
					//進行了編碼和解碼.以前的handler處理字節,以後的handler處理字符串。順序不可亂。
					pipeline.addLast("stringDecoder", new StringDecoder());
					pipeline.addLast("stringEncoder", new StringEncoder());
					pipeline.addLast("identifyDeviceDecoder", new IdentifyChannelDecoder(ServerManager.this));
					pipeline.addLast("initInfoChannelDecoder", new InitInfoChannelDecoder(ServerManager.this));
				}
			};
			trackerServerMap.put(trackerServer.getPort(), trackerServer);
			serverList.add(trackerServer);
		}
	}

	/**
	 * 
	 * Description:啓動時初始化數據緩存
	 */
	private void initDataCache() {
		new DataCache(dataManager);
	}
	
	/**
	 * 初始化日誌
	 * @param name
	 * @param sgin
	 * @throws IOException
	 */
	private void initLog(String name,String sgin)throws IOException{
		if (name != null) {
			FileHandler file = new FileHandler(name,500 * 1024 * 1024, 10, true);	
			// Simple formatter
			file.setFormatter(new Formatter() {
				private final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
				private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				@Override
				public String format(LogRecord record) {
					StringBuffer line = new StringBuffer();
					dateFormat.format(new Date(record.getMillis()), line, new FieldPosition(0));
					line.append(" ");
					line.append(record.getSourceClassName());
					line.append(".");
					line.append(record.getSourceMethodName());
					line.append(" ");
					line.append(record.getLevel().getName());
					line.append(": ");
					line.append(formatMessage(record));
					line.append(LINE_SEPARATOR);
					return line.toString();
				}
			});
			Log.getLogger(sgin).addHandler(file);//文件輸出
			Log.getLogger(sgin).addHandler(new ConsoleHandler());//控制檯輸出
		}
	}
	
	/**
	 * 啓動定時任務
	 * @param dataManager
	 */
	public void startTimer(){
		// webservice定時獲取訂單
		ScheduledExecutorService executorWSOrder = Executors.newScheduledThreadPool(1);
		executorWSOrder.scheduleWithFixedDelay(new WSAutoRecieveOrderTimer(dataManager), 10, 300, TimeUnit.SECONDS);
		Log.info(Constants.SERVER_MAIN, "**Timer[receive order] running per 300s......**");

		// 定時訂單
		ScheduledExecutorService deliverOrder = Executors.newScheduledThreadPool(2);
		deliverOrder.scheduleWithFixedDelay(new AutoSendOrderTimer(dataManager), 10, 180, TimeUnit.SECONDS);
		Log.info(Constants.SERVER_MAIN, "**Timer[auto send order] running per 180s......**");
		
		// GPS緩存定時清理
		scheduleDailyGpsCache();
		Log.info(Constants.SERVER_MAIN, "**Timer[GPS data cleaner] running per day......**");
		
		// 定時檢查升級是否超時
		ScheduledExecutorService deviceUpgrade = Executors.newScheduledThreadPool(3);
		deviceUpgrade.scheduleWithFixedDelay(new AutoCheckUpgradTimer(dataManager), 20, 60, TimeUnit.SECONDS);
		Log.info(Constants.SERVER_MAIN, "**Timer[auto check upgrade device] running per 60s......**");
		 
		
	}

}
相關文章
相關標籤/搜索