zhang
2025-05-20 1313906bb1eb983d3beece810035e7fc28d6a92f
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
package com.zy.acs.gateway.listen;
 
import com.alibaba.fastjson.JSON;
import com.zy.acs.common.constant.RedisConstant;
import com.zy.acs.common.domain.AgvProtocol;
import com.zy.acs.common.utils.RedisSupport;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.gateway.config.SystemProperties;
import com.zy.acs.gateway.job.DispatcherPublisher;
import com.zy.acs.gateway.utils.ProtocolUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
 
/**
 * 下行报文监听器
 * 消息队列: redis list
 * Created by vincent on 2019-04-02
 */
@Component
public class MessageListener {
 
    private Thread thread;
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
    private final static Logger log = LoggerFactory.getLogger(MessageListener.class);
 
    @Autowired
    private DispatcherPublisher publisher;
    @Autowired
    private SystemProperties systemProperties;
 
    @PostConstruct
    private void start(){
        thread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                AgvProtocol protocol = redis.pop(RedisConstant.AGV_PATH_DOWN_FLAG);
                if (null != protocol) {
                    if (systemProperties.isPrintPacLog()) {
                        log.info("监听器 >>> {}", JSON.toJSONString(protocol));
                    }
                    if (!Cools.isEmpty(protocol.getAgvNo())) {
                        publisher.publish(ProtocolUtils.installDownProtocol(protocol));
                    }
                }
                // 间隔
                try {
                    Thread.sleep(500);
                } catch (Exception ignore) {}
            }
        });
        thread.start();
    }
 
    @PreDestroy
    public void shutDown(){
        if (thread != null) thread.interrupt();
    }
 
}