zhubaomin
2025-04-07 9fb370afff71f4d5659d8904e8751479280d5b48
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
package com.dy.rtuMw.server.tasks;
 
import com.dy.common.mw.core.CoreTask;
import com.dy.common.queue.Node;
import com.dy.rtuMw.server.forMs.MsObj4Ding;
import com.dy.rtuMw.server.forMs.SendMsCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
/**
 * @Author: liurunyu
 * @Date: 2024/7/31 18:45
 * @Description
 */
public class SendMsConstantTask  extends CoreTask {
 
    private static final Logger log = LogManager.getLogger(SendMsConstantTask.class.getName());
 
    /**
     * 在单线程环境中运行
     */
    @Override
    public Integer execute() {
        try{
            //log.info("当前消息队列中还有结点数量:" + SendMsCache.size());
            dealMs() ;
        }catch(Exception e){
            log.error(e);
        }
        return SendMsCache.size()>0?0:1 ;
    }
 
    /**
     * 处理下行命令
     */
    public void dealMs() {
        Node first = SendMsCache.getFirstQueueNode() ;
        if(first != null){
            Node last = SendMsCache.getLastQueueNode() ;
            while (last != null){
                last = this.doDealMs(first, last);
            }
        }
    }
    /**
     * 处理缓存的下行节点
     * @param first 第一个节点
     * @param last 最后一个节点
     */
    private Node doDealMs(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 ;
        }
    }
 
    /**
     * 处理一个节点
     * @param node 节点
     */
    private void dealNode(Node node){
        if(node != null && node.obj != null){
            MsObj4Ding obj = (MsObj4Ding)node.obj ;
            boolean removeNode = obj.dealSelf() ;
            if(removeNode){
                SendMsCache.removeNode(node);
            }
        }
    }
 
 
}