Through Netty communication, acquisition of equipment on-site GPS data, and storage in the Redis server.

Source: Internet
Author: User
Tags dateformat jboss redis server

The main program code is as follows:

/*/* Copyright (c) 2013 Beijing Soft Show Technology Co., Ltd. www.inforwms.com All rights reserved.
* mail:[email protected] */*/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;/** * Service Unified Management * @author <a href= "mailto:[email protected]" > meslog</a> * @version 1.0.0.2013-10-22 * */public class Servermanager {private final String Cfgpath= "/server.cfg";p rivate final list<trackerserver> serverlist = new linkedlist<trackerserver> ();p rivate Final static concurrentmap<integer,trackerserver> Trackerservermap = new Concurrenthashmap<integer, Trackerserver> ();//lizhaopublic static concurrentmap<integer,trackerserver> Gettrackerservermap () {// Lizhaoreturn 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;} /** * Initialize log, cache, database and Netty services */public void Init () throws IOException, ClassNotFoundException, SQLException {loadProperties ();//load configuration, Initialize constant initLogger (properties);//Initialize log DataManager = new Databasedatamanager (properties);//Initialize Database connection Log.info (constants.server_ MAIN, "**database connection initialized......**"); redisfactory = Initredis (properties);//Initialize Redis cache Initdatacache () ;//Load Redis cache Log.info (Constants.server_main, "**redis load compeleted......**"); Initelsserver ("Fourfaith");// Initialize Netty service}private void LoadProperties () throws Invalidpropertiesformatexception, ioexception{//load config file properties = New Properties (); InputStream in = GetClass (). getResourceAsStream (Cfgpath);p roperties.loadfromxml (in); In.close ();// Print version Information Constants.version_code = Properties.getproperty ("connection.version"); Log.info (Constants.server_main, "===============================system version:[" +Constants.VERSION_CODE+ "]");// Initializes the constant Initconstant (properties);} /** * Periodically cleans up cache data */private void Scheduledailygpscache () {final redistemplate<string, object> redistemplate = new Red Istemplate<> (); 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.TOD Ay_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;} /** * Start Netty Service and related timed thread */public void start () {//Start Netty serverfor (Object server:serverlist) {((trackerserver) server). s Tart ();} Start Timer starttimer ();} /** * Stop */public void Stop () {for (Object server:serverlist) {((trackerserver) server). Stop (); Release resourcesglobalchannelfactory.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.value Of (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); }}/** * Initialize Constants * @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 {//lizhaoif (Isprotocolenabled (Properties, protocol)) { Trackerserver trackerserver = new Trackerserver (This, new Serverbootstrap (), protocol) {@Overrideprotected void Addspecifichandlers (Channelpipeline Pipeline) {//IS encoded and decoded. Before the handler handles the bytes, after the handler processing string. The order is not chaotic. Pipeline.addlast ("Stringdecoder", New Stringdecoder ());p Ipeline.addLast ("Stringencoder", New Stringencoder ());p ipeline.addlast ("Identifydevicedecoder", New Identifychanneldecoder ( servermanager.this));p ipeline.addlast ("Initinfochanneldecoder", New Initinfochanneldecoder (ServerManager.this));}}; Trackerservermap.put (Trackerserver.getport (), trackerserver); Serverlist.add (Trackerserver);}} /** * * Description: Initialize data cache at startup */private void Initdatacache () {new Datacache (DataManager);} /** * Init log * @param name * @param sgin * @throws ioexception */private void Initlog (String name,string sgin) throws Ioexce PTION{IF (name! = null) {Filehandler file = new Filehandler (name,500 * 1024x768 * 1024x768, ten, true);//Simple FORMATTERFILE.SETF Ormatter (New Formatter () {private final String Line_separator = System.getproperty ("Line.separator", "\ n");p rivate Final DateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); @Overridepublic String Format (logrecord Record) {StringBuffer line = new StringBuffer ();d Ateformat.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);//Document Output Log.getlogger (sgin). AddHandler (New Consolehandler ());//console Output}}/** * Start a timed task * @param dataManager */public void Starttimer () {//WebService timing Get order Scheduledexecutorservice Executorwsorder = Executors.newscheduledthreadpool (1); Executorwsorder.schedulewithfixeddelay (New Wsautorecieveordertimer ( DataManager), ten, timeunit.seconds); Log.info (Constants.server_main, "**timer[receive order" running per 300s......** ");//timed Order Scheduledexecutorservice Deliverorder = Executors.newscheduledthreadpool (2);d Eliverorder.schedulewithfixeddelay (New AutoSendOrderTimer ( DataManager), ten, timeunit.seconds); Log.info (Constants.server_main, "**timer[auto send order" running per180s......** ");//GPS Cache timed Cleanup Scheduledailygpscache (); Log.info (Constants.server_main, "**TIMER[GPS data cleaner] running per day......**");// Timed check to see if the upgrade timed out scheduledexecutorservice Deviceupgrade = Executors.newscheduledthreadpool (3); Deviceupgrade.schedulewithfixeddelay (New Autocheckupgradtimer (DataManager), timeunit.seconds); Log.info (Constants.server_main, "**timer[auto Check upgrade device] running per 60s......**"); }}

Through Netty communication, acquisition of equipment on-site GPS data, and storage in the Redis server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.