whycxzp
2021-04-19 3ce5138c7cb230c531c3e0ee43a6e9cb361719a1
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
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<String, WebSocketSession> clients = WsHandler.clients;
        Map<String, Process> processMap = WsHandler.processMap;
 
        //processMap中的ip,如果clients中不存在,则关闭这个process
        Set<String> clientIpSessions = clients.keySet();
        Set<String> 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);
            }
        });
    }
}