• 业务提前初始化执行


    一 实现  CommandLineRunner接口

    1. package com.example.gatewayacquisitionsystem;
    2. import com.alibaba.fastjson.JSONArray;
    3. import com.alibaba.fastjson.JSONObject;
    4. import com.example.gatewayacquisitionsystem.config.ConfigProperties;
    5. import lombok.Data;
    6. import org.springframework.boot.CommandLineRunner;
    7. import org.springframework.stereotype.Component;
    8. import org.springframework.web.client.RestTemplate;
    9. import javax.annotation.Resource;
    10. import java.util.concurrent.Executors;
    11. import java.util.concurrent.ScheduledExecutorService;
    12. import java.util.concurrent.TimeUnit;
    13. @Component
    14. public class DeviceStatusRunner implements CommandLineRunner {
    15. @Resource
    16. RestTemplate restTemplate;
    17. @Resource
    18. ConfigProperties configProperties;
    19. public static int deviceNum = 0;
    20. public static int activeDeviceNum = 0;
    21. @Override
    22. public void run(String... args) throws Exception {
    23. ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
    24. service.scheduleWithFixedDelay(new Runnable() {
    25. @Override
    26. public void run() {
    27. JSONObject jsonObject = restTemplate.getForObject(configProperties.getZDataHandleHost() + "gateway/onlineId", JSONObject.class);
    28. assert jsonObject != null;
    29. if (jsonObject.getInteger("code") != 200) {
    30. return;
    31. }
    32. JSONArray gatewayIds = jsonObject.getJSONArray("data");
    33. int deviceNum = 0;
    34. int activeNum = 0;
    35. for (Object id : gatewayIds) {
    36. JSONObject rep = restTemplate.getForObject(configProperties.getZDataHandleHost() + "gateway/status/" + id, JSONObject.class);
    37. if (rep.getInteger("code") == 200) {
    38. deviceNum += rep.getJSONObject("data").getInteger("deviceNum");
    39. activeNum += rep.getJSONObject("data").getInteger("activeDeviceNum");
    40. }
    41. }
    42. DeviceStatusRunner.deviceNum = deviceNum;
    43. DeviceStatusRunner.activeDeviceNum = activeNum;
    44. }
    45. }, 10, 10, TimeUnit.SECONDS);
    46. }
    47. }

    二 不影响主线程,使用断言关键字进行判断  assert

       代码在上面,查看

    
                    
  • 相关阅读:
    【二】2D测量 Metrology——get_metrology_object_fuzzy_param()算子
    企业服务器上云还是下云哪种比较好?-尚云Sunclouds
    模糊搜索利器:Python的thefuzz模块详解
    Java--静态变量
    MySQL 约束条件,关键字练习,其他语句
    ubuntu静态ip地址设置
    python LeetCode 刷题记录 100
    【kubernetes】使用CloudProvider对接云厂商的LB
    Python数据攻略-Pandas时间序列数据处理
    c++(27)STL:容器、算法、迭代器
  • 原文地址:https://blog.csdn.net/weixin_49591538/article/details/126617795