package com.whyc.video; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.socket.WebSocketSession; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @EnableScheduling @Configuration public class VideoProcessTimer { /**检查是否有不需要的ffmpeg进程,有则关掉,节约资源,10秒钟检查一次*/ @Scheduled(cron = "0/10 * * * * *") public void checkProcess(){ LocalDateTime now = LocalDateTime.now(); System.out.println("当前时间:"+now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); Map clients = WsHandler.clients; Map processMap = WsHandler.processMap; //processMap中的ip,如果clients中不存在,则关闭这个process Set clientIpSessions = clients.keySet(); Set clientIps = clientIpSessions.stream().map(ipSession -> ipSession.split(",")[0]).collect(Collectors.toSet()); processMap.keySet().stream().forEach(ip->{ if(!clientIps.contains(ip)){ Process process = processMap.get(ip); process.destroy(); processMap.remove(ip); } }); } }