随笔 - 16  文章 - 0  评论 - 11  阅读 - 28260 
 
摘要: 前一篇介绍手持设备开发基本步骤。在CE客户端,我们通常用vs2008 c#客户端,后端有用socket调用存储过程,也有通过http请求的,前篇也有介绍了。一个请求HttpUtil 类。这回合来看看。服务端如果来实现。LoginControllerusing System;using System.... 阅读全文
posted @ 2015-12-24 19:41 meslog 阅读(520) 评论(0) 推荐(0) 编辑
  2016年2月3日

主程序代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 /*
 * CopyRight (c) 2013 北京软秀科技有限公司www.inforwms.com 保留所有权利。<br> * 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......**");
          
         
    }
 
}

 

posted @ 2016-02-03 20:57 meslog 阅读(5550) 评论(0) 推荐(0) 编辑
  2016年1月27日
摘要: 自动立体车库控制应用系统主要控制界面如下 系统主要包含两个部分: 第一部分是系统与PLC系列进行通信,实时采集现场控制信息,控制提升机及运输小车,完成多个设备之间互锁,保证系统安全有效运行。实时采集现场2000多个点信息,同时保存在数据库。 第二部分是管理所有车位信息。高效规划入出库路径,最大化提高 阅读全文
posted @ 2016-01-27 19:25 meslog 阅读(2537) 评论(3) 推荐(0) 编辑
  2016年1月9日
摘要: 1、安装Microsoft AnalysisServies2、创建一个Analysis 项目如下图所示3、根据业务需求,数据源及多维度数据集,编译成功,进行布署。4、发布成功后,通过SQLSERVER企业管理器进行连接,如何在IIS7下创建Analysis Services服务可以baidu一下具体... 阅读全文
posted @ 2016-01-09 23:15 meslog 阅读(1602) 评论(1) 推荐(1) 编辑
  2015年12月31日
摘要: 下面这3个命令是非常好用的日志分析命令,以apache的日志文件access_log为例1访问次数最多的IP TOP10当网络流量突然持续异常时,很有可能是有恶意访问,最快的解决方式就是找出访问量最多的几个ip,暂时禁止其访问,然后再仔细观察# cat access_log | cut -f1 -d... 阅读全文
posted @ 2015-12-31 17:16 meslog 阅读(445) 评论(0) 推荐(0) 编辑
摘要: @RequestMapping(value = "/ManagerData")@ResponseBodypublic List ManagerData(@RequestParam("loadinfo") String loadinfo)//@RequestParam("loadinfo")前台传过来... 阅读全文
posted @ 2015-12-31 17:00 meslog 阅读(270) 评论(0) 推荐(0) 编辑
  2015年12月27日
摘要: java使用redis缓存可以使用jedis框架,jedis操作简单,没有什么复杂的东西需要学习,网上资料很多,随便看看就会了.将spring与redis缓存集成,其实也是使用jedis框架,只不过spring对它进行了一层封装,并将这层封装库命名为spring-data-redis.下面将要使用s... 阅读全文
posted @ 2015-12-27 22:33 meslog 阅读(378) 评论(0) 推荐(0) 编辑
  2015年12月24日
摘要: 简单介绍一下项目背景:大家喜爱的葡萄酒需要经过原料选择→分选→去梗→破碎→消毒→前发酵→压榨→调整酒度→后发酵→贮藏→沉清过滤→装瓶、杀菌这几个过程,保温发酵控制系统是其中重要一环,葡萄酒经过前期工艺后,在发酵罐常期进行发酵,温度必须保证在一定范围,温度高时需要制冷。温度高了需要进行搅拌,不至于结冰... 阅读全文
posted @ 2015-12-24 23:13 meslog 阅读(1304) 评论(2) 推荐(0) 编辑
  2015年12月13日
摘要: S的闭包用法给开发带来了极大的便利,它的使用方式非常自然,以至于很多同学并不很了解闭包,却可以在实际开发中顺畅的使用了例如下面的代码,给button添加一个点击事件,很多人经常这么写,实际上这就是一个闭包要了解闭包,需要先了解下JS变量的作用域变量的作用域无非就是两种:全局变量和局部变量特点是 函数... 阅读全文
posted @ 2015-12-13 00:33 meslog 阅读(1772) 评论(2) 推荐(1) 编辑
摘要: 本文来自朋友圈数据库架构一般从简单到复杂的过程1、一主一从由一台主库和一台从库组成,从库只用作备份和容灾,当主库出现故障时,从库就手动变成主库随着压力的增加,加上了memcached2、一主多从通过添加多个从库来分流查询压力3、随着数据量的增加,读写压力都迅速增加,进行数据库拆分,将数据存放到不同的... 阅读全文
posted @ 2015-12-13 00:18 meslog 阅读(561) 评论(0) 推荐(1) 编辑
点击右上角即可分享
微信分享提示