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 map = new HashMap<>(); public boolean push(String key, String msg) { map.put(key, new WatchModel(key, msg)); return true; } public HashMap getMap() { return map; } public List getList() { return new ArrayList<>(map.values()); } public synchronized void clearTimeOutData() { ArrayList removeKey = new ArrayList<>(); for (Map.Entry 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); } } }