New file |
| | |
| | | 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) ; |
| | | } |
| | | } |
| | | } |