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);
|
}
|
}
|
|
}
|