public interface DistributedLock {
boolean getLock(String var1, String var2, int var3);//加锁 void unLock(String var1, String var2);//释放
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.pt.platform.core.redis.lock; import com.pt.platform.core.ehcache.ObtainPropertiesInfo;
import com.pt.platform.core.redis.JedisSentinelPool;
import com.pt.platform.core.redis.lock.DistributedLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;
import redis.clients.util.SafeEncoder; public class RedisLock implements DistributedLock {
private static final Logger logger = LoggerFactory.getLogger(RedisLock.class);
private static final int DEFAULT_EXPIRE_TIME = 60;
private static final String LOCK_KEY_PREFIX = "REDIS_LOCK";
private JedisSentinelPool pool; public RedisLock() {
} public JedisSentinelPool getPool() {
return this.pool;
} public void setPool(JedisSentinelPool pool) {
this.pool = pool;
} public boolean getLock(String module, String bizKey, int expireTime) {
if(module != null && !"".equals(module) && bizKey != null && !"".equals(bizKey)) {
if(this.getPool() != null) {
ShardedJedis jedis = null;
String lockKey = this.getLockKey(module, bizKey); boolean var10;
try {
jedis = (ShardedJedis)this.getPool().getResource();//获取到数据源
long e = System.currentTimeMillis();
long result = jedis.setnx(SafeEncoder.encode(lockKey), SafeEncoder.encode(e + "")).longValue();
if(result == 1L) {
if(expireTime > 0) {
jedis.expire(SafeEncoder.encode(lockKey), expireTime);
if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " locked and expire time:" + expireTime + "s");
}
} else {
jedis.expire(SafeEncoder.encode(lockKey), 60);
if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " locked and expire time:" + 60 + "s");
}
} var10 = true;
return var10;
} if(logger.isDebugEnabled()) {
logger.debug("key:" + lockKey + " has already bean locked");
} var10 = false;
} catch (Exception var14) {
logger.error("lock error", var14);
boolean var7 = false;
return var7;
} finally {
this.getPool().returnResource(jedis);
} return var10;
} else {
logger.error("jedisSentinelPool is null");
return true;
}
} else {
logger.error("parameters is null");
return false;
}
}

//删除key值释放锁
public void unLock(String module, String bizKey) {
if(module == null || "".equals(module) || bizKey == null || "".equals(bizKey)) {
logger.error("parameters is null");
} if(this.getPool() != null) {
ShardedJedis jedis = null;
String lockKey = this.getLockKey(module, bizKey); try {
jedis = (ShardedJedis)this.getPool().getResource();
jedis.del(SafeEncoder.encode(lockKey));
} catch (Exception var9) {
logger.error("unlock error", var9);
} finally {
this.getPool().returnResource(jedis);
}
} else {
logger.error("jedisSentinelPool is null");
} }

//组装key值
private String getLockKey(String module, String bizKey) {
StringBuffer sb = new StringBuffer();
sb.append("REDIS_LOCK").append(":").append(ObtainPropertiesInfo.getValByKey("app.code")).append(":").append(module).append(":").append(bizKey);
return sb.toString();
}
}
  private Map<String ,String> getResult(LaTFundRegDTO dto,UserInfo userInfo){
Map<String,String> map=null;
//加锁
if(redisLock.getLock(FundConstant.REDIS_LOCK_MODEL_FUND, dto.getCreditorRightsNo(), 120)) {
try{
if (FundConstant.FUND_STATUS_3.equals(dto.getFundStatus())) {
service.updateReFund(dto, userInfo);
} else if (FundConstant.FUND_STATUS_1.equals(dto.getFundStatus())) {
service.updatedoFundData(dto, userInfo);
}
}catch(Exception e){
e.printStackTrace();
map=new HashMap<String,String>();
map.put("id",dto.getCreditorRightsNo());
map.put("msg",e.getMessage());
logger.error("",e);
}finally{
redisLock.unLock(FundConstant.REDIS_LOCK_MODEL_FUND,dto.getCreditorRightsNo());
}
}else{
map=new HashMap<String,String>();
map.put("id",dto.getId()+"");
map.put("msg","不允许重复发起放款操作");
logger.error("不允许重复发起放款操作");
}
return map;
}

redis防止重复提交的更多相关文章

  1. 使用redis防止重复提交

    使用redis防止重复提交   其实主要思路是他的https://blog.csdn.net/u013378306/article/details/52944780 主要目前我的情况是,前后端分离的, ...

  2. Java使用Redis实现分布式锁来防止重复提交问题

    如何用消息系统避免分布式事务? - 少年阿宾 - BlogJavahttp://www.blogjava.net/stevenjohn/archive/2018/01/04/433004.html [ ...

  3. 【Redis使用系列】使用Redis做防止重复提交

    前言 在平时的开发中我们都需要处理重复提交的问题,避免业务出错或者产生脏数据,虽然可以通过前端控制但这并不是可以完全避免,最好的方式还是前后端均进行控制,这样的话就可以更有效,尽可能全面的去减少错误的 ...

  4. 浅谈C#在网络波动时防重复提交

    前几天,公司数据库出现了两条相同的数据,而且时间相同(毫秒也相同).排查原因,发现是网络波动造成了重复提交. 由于网络波动而重复提交的例子也比较多: 网络上,防重复提交的方法也很多,使用redis锁, ...

  5. Restful api 防止重复提交

    当前很多网站是前后分离的,前端(android,iso,h5)通过restful API 调用 后端服务器,这就存在一个问题,对于创建操作,比如购买某个商品,如果由于某种原因,手抖,控件bug,网络错 ...

  6. springmvc防止重复提交拦截器

    一.拦截器实现,ResubmitInterceptorHandler.java import org.apache.commons.lang3.StringUtils; import org.spri ...

  7. 防CSRF攻击:一场由重复提交的问题引发的前端后端测试口水战

    重复提交,这是一直以来都会存在的问题,当在网站某个接口调用缓慢的时候就会有可能引起表单重复提交的问题,不论form提交,还是ajax提交都会有这样的问题,最近在某社交app上看到这么一幕,这个团队没有 ...

  8. Token机制,防止web页面重复提交

    1.业务要求:页面的数据只能被点击提交一次 2.发生原因: 由于重复点击或者网络重发,或者nginx重发等情况会导致数据被重复提交 3.解决办法: 集群环境:采用token加redis(redis单线 ...

  9. 由防止表单重复提交引发的一系列问题--servletRequest的复制、body值的获取

    @Time:2019年1月4日 16:19:19 @Author:QGuo   背景:最开始打算写个防止表单重复提交的拦截器:网上见到一种不错的方式,比较合适前后端分离,校验在后台实现: 我在此基础上 ...

随机推荐

  1. git 中.gitignore文件不生效

    .gitignore文件 新增忽略文件并没有生效 新增的忽略文件没有生效,是因为git是有缓存的,而之前的文件在缓存中,并不会清除掉,还会继续提交,所以更新.gitignore文件,要清除缓存文件 g ...

  2. Redis的sentinel(哨兵)部署

    1.准备文件 1.解压redis-4.0.1.tar.gz的redis文件 2.新建目录 redis-cluster以及子目录 master-6379 slave-7000 slave-7001 3. ...

  3. Java菜鸟在IP问题踩坑了

    之前有做过获取客户端公网IP的项目 一般都是 正常的request.getRemoteAddr 或者request.getRemoteHost 可获取到客户端的公网IP, 或者项目部署在有nginx代 ...

  4. 【Linux】kali 安装 python3 和 pip3(亲测有效)

    [Linux]kali 安装 python3 和 pip3 引言:   在使用kali的时候,经常会用到各种工具以及脚本,而大多数脚本都是以python编写的,但是烦就烦在python有2个版本,有些 ...

  5. spring ioc踏出第一步

    spring IOC(容器) AOP(面向切面编程) IOC容器:他的功能就是可以整合像 Structs2 .Hibernate.Mybatis: IOC:控制反转:所谓的控制就是控制资源的获取方法, ...

  6. 执行py文件需要可执行权限吗?

    案例解析 这个问题描述起来有点违反直觉,要执行一个文件难道不应该需要可执行权限吗?让我们先来看一个例子: # module1.py def test(): print ('hello world!') ...

  7. ansible 安装和使用

                                ansible 安装和使用   ## 安装epel 源: rpm -ivh https://dl.fedoraproject.org/pub/e ...

  8. MBAir下安装httprunner2.5.7 har2case 出现zsh: command not found解决方案

    MBAir下python3.8安装httprunner2.5.7 出现zsh: command not found find / -name hrun查找到路径为: /Users/w550856/Li ...

  9. 【Git】5、Git如何提交代码到远程仓库

    提交代码:如何把修改后的代码提交到远程仓库 文章目录 提交代码:如何把修改后的代码提交到远程仓库 1.同步远程代码 2.检查改动文件 3.添加文件到缓存 4.提交代码 5.推送代码 6.我的整个流程 ...

  10. export PATH=$PATH:/usr/local/mysql/bin

    [root@test]# export PATH=$PATH:/usr/local/mysql/bin[root@test]# echo $PATH/usr/local/sbin:/usr/local ...