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
package com.dy.pipIrrRemote.msCenter;
 
import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw;
import com.dy.pipIrrGlobal.util.Org;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
 
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
 
/**
 * @Author: liurunyu
 * @Date: 2025/2/13 9:47
 * @Description
 */
@Slf4j
@Component
public class Register2MwMsCenterListener extends Web2RtuMw implements ApplicationListener<ApplicationReadyEvent> {
    @Autowired
    private Environment env;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    protected ResourceLoader resourceLoader;
 
    @Value("${mw.mwMsCenterReceiveUrl_rm}")
    protected String mwMsCenterReceiveUrl;//消息接收网址
 
    private static final String mwParamName = "msReceiverWebUrl" ;
 
    private List<Org.OrgVo> orgs;
 
    private Timer timer;
 
    /**
     * SpringBoot容器已经准备好了,执行下面方法
     *
     * @param event 事件
     */
    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
        try {
            //等1秒,等待com.alibaba.druid.pool.DruidDataSource实始化完成
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                this.start(event);
            } catch (Exception e) {
                log.error("向通信中间件注册消息接收者出错", e);
            }
        }
    }
 
    /**
     * 执行业务逻辑
     * @param event
     * @throws Exception
     */
    private void start(ApplicationReadyEvent event) throws Exception {
        if(mwMsCenterReceiveUrl == null || mwMsCenterReceiveUrl.trim().equals("")){
            throw new Exception("通信中间件消息接收网址未配置");
        }
        orgs = super.get(resourceLoader);
        if(orgs != null && orgs.size() > 0) {
            this.timer = new Timer();
            this.timer.schedule(new TimerTask() {
                public void run() {
                    register(event) ;
                }
            }, 100 , 5 * 60 * 1000);
        }
    }
 
    /**
     * 向通信中间件注册消息接收者
     * @param event
     */
    private void register(ApplicationReadyEvent event){
        for (Org.OrgVo vo : this.orgs){
            String rqUrl = this.get2MwRequestUrl(this.env, vo.tag, ContextRegisterMsReceiverWebUrl) ;
            sendGetRequest2Mw(restTemplate, rqUrl, mwParamName, mwMsCenterReceiveUrl) ;
        }
    }
}