排除重复的异步通知 - moyq5/weixin-popular GitHub Wiki
微信的异步通知存在重复通知的情况,为了避免业务重复执行,使用ExpireKey 进行判定。
package weixin.popular.support;
/**
* key 过期
* @author SLYH
*
*/
public interface ExpireKey {
//12 s
public static final Integer DEFAULT_EXPIRE = 12;
/**
* 添加key
* @param key
* @param expire 有效时间(秒)
* @return
*/
public boolean add(String key,int expire);
/**
* 添加key
* @param key
* @return
*/
public boolean add(String key);
/**
* 判断key是否存在
* @param key
* @return
*/
public boolean exists(String key);
}
适用于单机环境
ExpireKey expireKey = new DefaultExpireKey();
适用于集群环境
//配置链接池
JedisPool jedisPool = null;
ExpireKey expireKey = new JedisExpireKey(jedisPool);
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.3</version>
</dependency>
实现ExpireKey 接口即可。 如果你有更好的实现方式请 pull request 代码。