package com.zc.business.controller; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.zc.business.domain.DcSwitch; import com.zc.business.service.DcSwitchService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.io.IOException; import java.net.InetAddress; import java.util.Date; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; /** * 交换机Controller * * @author wangjiabao */ @Api(tags = {"交换机"}) @RestController @RequestMapping("/business/switch") public class DcSwitchController extends BaseController { @Resource private DcSwitchService dcSwitchService; /** * 查询交换机 */ @ApiOperation("查询交换机") @GetMapping("/query") public AjaxResult getSwitchList(DcSwitch dcSwitch) { return AjaxResult.success(dcSwitchService.getSwitchList(dcSwitch)); } /** * 根据设备列表查询设备 */ @ApiOperation("根据设备列表查询设备") @GetMapping("/deviceList/{deviceList}") public AjaxResult getDeviceList(@PathVariable String deviceList) { return AjaxResult.success(dcSwitchService.getDeviceList(deviceList)); } /** * 查询所有数据 * * @return */ @GetMapping("/list") public AjaxResult getSwitchListAll() { return AjaxResult.success(dcSwitchService.getSwitchListAll()); } /** * 定时更新交换机网络状态 */ @Scheduled(cron = "0 0/30 * * * ?") public void updateNetWorkStatus() { List switchList = dcSwitchService.getSwitchList(new DcSwitch()); ExecutorService executor = Executors.newFixedThreadPool(100); List collect = switchList.stream() .filter(dcSwitch -> { return dcSwitch.getParentId() != null; }).collect(Collectors.toList()); CountDownLatch latch = new CountDownLatch(collect.size()); collect.forEach(dcSwitch -> { executor.execute(() -> { try { InetAddress inet = InetAddress.getByName(dcSwitch.getSwitchIp()); if (inet.isReachable(5000)) { // 成功 dcSwitch.setNetWorkStatus(1); } else { // 失败 dcSwitch.setNetWorkStatus(0); } dcSwitch.setUpdateTime(new Date()); } catch (IOException e) { e.getMessage(); } finally { latch.countDown(); } }); }); try { latch.await(); // 等待所有线程执行完毕 } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { executor.shutdown(); } // 批量修改 dcSwitchService.updateBatchByNetWorkStatus(collect); } }