New file |
| | |
| | | package com.dy.rtuMw.server.tasks; |
| | | |
| | | import com.dy.rtuMw.server.rtuData.RtuDataNode; |
| | | import com.dy.rtuMw.server.rtuData.RtuDataCache; |
| | | import com.dy.common.mw.core.CoreTask; |
| | | import com.dy.common.queue.Node; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * 对RTU上行数据进行业务处理 |
| | | */ |
| | | public class FromRtuDataConstantTask extends CoreTask { |
| | | private static final Logger log = LogManager.getLogger(FromRtuDataConstantTask.class.getName()); |
| | | |
| | | /** |
| | | * 在单线程环境中运行 |
| | | */ |
| | | @Override |
| | | public Integer execute() { |
| | | try{ |
| | | dealRtuUpData() ; |
| | | }catch(Exception e){ |
| | | log.error(e); |
| | | } |
| | | return RtuDataCache.size()>0?0:1 ; |
| | | } |
| | | /** |
| | | * 处理上行数据 |
| | | */ |
| | | public void dealRtuUpData() { |
| | | Node first = RtuDataCache.getFirstQueueNode() ; |
| | | if(first != null){ |
| | | Node last = RtuDataCache.getLastQueueNode() ; |
| | | while (last != null){ |
| | | last = this.doDealRtuUpData(first, last); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理缓存的上行数据节点 |
| | | * @param first 第一个节点 |
| | | * @param last 最后一个节点 |
| | | */ |
| | | private Node doDealRtuUpData(Node first, Node last){ |
| | | if(last != null){ |
| | | //在dealNode方法中,可能要把last从队列中移除,这时last.pre为空,所以提前把last.pre取出来 |
| | | Node pre = last.pre ; |
| | | dealNode(last) ; |
| | | if(first != last){ |
| | | return pre ; |
| | | }else{ |
| | | //停止 |
| | | return null ; |
| | | } |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | |
| | | |
| | | //////////////////////////////////////////////// |
| | | // |
| | | //以下实现,采用了递归调用,当队列缓存结点很多时,会产生栈溢出异常 |
| | | // |
| | | //////////////////////////////////////////////// |
| | | /** |
| | | * 处理上行数据 |
| | | */ |
| | | public Integer dealRtuUpData_() { |
| | | Node first = RtuDataCache.getFirstQueueNode() ; |
| | | if(first != null){ |
| | | Integer count = RtuDataCache.size() ; |
| | | Node last = RtuDataCache.getLastQueueNode() ; |
| | | this.doDealRtuUpData_(first, last); |
| | | return count ; |
| | | } |
| | | return null ; |
| | | } |
| | | |
| | | /** |
| | | * 处理缓存的上行数据节点 |
| | | * @param first 第一个节点 |
| | | * @param last 最后一个节点 |
| | | */ |
| | | private void doDealRtuUpData_(Node first, Node last){ |
| | | if(last != null){ |
| | | //在dealNode方法中,可能要把last从队列中移除,这时last.pre为空,所以提前把last.pre取出来 |
| | | Node pre = last.pre ; |
| | | dealNode(last) ; |
| | | if(first != last){ |
| | | doDealRtuUpData(first, pre) ; |
| | | }else{ |
| | | //停止 |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理一个节点 |
| | | * @param node 节点 |
| | | */ |
| | | private void dealNode(Node node){ |
| | | if(node != null && node.obj != null){ |
| | | RtuDataNode obj = (RtuDataNode)node.obj ; |
| | | obj.dealSelf() ; |
| | | RtuDataCache.removeNode(node); |
| | | } |
| | | } |
| | | |
| | | } |