package com.tejia.lijin.app.util; import java.util.HashMap; import java.util.Map; //重复事件响应过滤器 public class DuplicateEventFilterUtil { private static Map lastAllowTime = new HashMap<>(); //是否允许事件响应 public static synchronized boolean allowEvent(String key) { if (key == null) return true; if (lastAllowTime.get(key) == null) lastAllowTime.put(key, 0L); if (System.currentTimeMillis() - lastAllowTime.get(key) < 500) return false; lastAllowTime.put(key, System.currentTimeMillis()); return true; } public static synchronized boolean allowEvent(String key,int ms) { if (key == null) return true; if (lastAllowTime.get(key) == null) lastAllowTime.put(key, 0L); if (System.currentTimeMillis() - lastAllowTime.get(key) < ms) return false; lastAllowTime.put(key, System.currentTimeMillis()); return true; } }