Junjie
2025-05-22 32b593115da09714624f3803fc43a6add07da391
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
39
40
41
42
43
package com.zy.common.service;
 
import com.zy.common.model.WatchModel;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class WatchService {
 
    private HashMap<String, WatchModel> map = new HashMap<>();
 
    public boolean push(String key, String msg) {
        map.put(key, new WatchModel(key, msg));
        return true;
    }
 
    public HashMap<String, WatchModel> getMap() {
        return map;
    }
 
    public List<WatchModel> getList() {
        return new ArrayList<>(map.values());
    }
 
    public synchronized void clearTimeOutData() {
        ArrayList<String> removeKey = new ArrayList<>();
        for (Map.Entry<String, WatchModel> entry : this.map.entrySet()) {
            WatchModel watchModel = entry.getValue();
            if (System.currentTimeMillis() - watchModel.getTime() > 1000 * 10) {
                removeKey.add(entry.getKey());
            }
        }
 
        for (String key : removeKey) {
            map.remove(key);
        }
    }
 
}