一个redis锁

package com.cntaiping.tpi.bxData.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cntaiping.tpi.bxData.dto.GoAgriLog;
import com.cntaiping.tpi.bxData.mapper.GoAgriLogMapper;

@Component
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class Lock {
	private static final Logger logger = LoggerFactory.getLogger(Lock.class);

	@Autowired
	private GoAgriLogMapper goAgriLogMapper;

	
	public boolean tryLock(String id, String type) {
		try {
			GoAgriLog queryLock = goAgriLogMapper.queryLock(id, type);
			if (queryLock != null) {
				int count = goAgriLogMapper.tryLock(id, type);
				if (count > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} catch (Exception e) {
			logger.info("获取" + type + "锁异常!!!!!!!!!!!!!!!!!!!!!!请检查锁状态!!!!!!!!!!!!!!!!!", e);
			throw new RuntimeException("获取" + type + "锁异常!!!!!!!!!!!!!!!!!!!!!!请检查锁状态!!!!!!!!!!!!!!!!!");
		}
	}

	public void unlock(String id, String type) {
		try {
			int count = goAgriLogMapper.unLock(id, type);
			if (count != 1) {
				throw new RuntimeException("释放" + type + "锁异常!!!!!!!!!!!!!!!!!!!!!!请检查锁状态!!!!!!!!!!!!!!!!!");
			}
		} catch (Exception e) {
			logger.info("释放" + type + "锁异常!!!!!!!!!!!!!!!!!!!!!!请检查锁状态!!!!!!!!!!!!!!!!!", e);
			throw new RuntimeException("释放" + type + "锁异常!!!!!!!!!!!!!!!!!!!!!!请检查锁状态!!!!!!!!!!!!!!!!!");
		}

	}

}

  

package com.cntaiping.tpi.bxData.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cntaiping.tpi.bxData.vo.ZnLockProperties;

@Component
public abstract class AbsZnzbService {

	@Autowired
	private Lock lock;

	@Autowired
	private ZnLockProperties znLockProperties;

	private static final Logger logger = LoggerFactory.getLogger(AbsZnzbService.class);

	public void uploadClaimP() {
		String claimLockId = znLockProperties.getClaimLockId();
		String claimLockName = znLockProperties.getClaimLockName();
		// 获取锁
		if (!lock.tryLock(claimLockId, claimLockName)) {
			// 没有拿到直接返回
			logger.info("获取锁失败~~~~~~~~~~~~~~~~~~~~~" + claimLockName);
			return;
		}
		// 拿到锁后执行业务逻辑
		try {
			logger.info("claimlock已经获取~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			uploadClaim();
		} finally {
			// 释放锁
			lock.unlock(claimLockId, claimLockName);
			logger.info("claimlock已经释放~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		}

	}

	abstract void uploadClaim();

	public void uploadCaseP() {
		String caseLockId = znLockProperties.getCaselockId();
		String caseLockName = znLockProperties.getCaselockName();

		// 获取锁
		if (!lock.tryLock(caseLockId, caseLockName)) {
			// 没有拿到直接返回
			logger.info("获取锁失败~~~~~~~~~~~~~~~~~~~~~" + caseLockName);
			return;
		}
		// 拿到锁后执行业务逻辑
		try {
			logger.info("caselock已经获取~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			uploadCase();
		} finally {
			// 释放锁
			lock.unlock(caseLockId, caseLockName);
			logger.info("claimlock已经释放~~~~~~~~~~~~~~~~~~~~~");
		}

	}

	abstract void uploadCase();

	public void checkP() {
		String checkLockId = znLockProperties.getChecklockId();
		String checkLockName = znLockProperties.getChecklockName();
		// 获取锁
		if (!lock.tryLock(checkLockId, checkLockName)) {
			// 没有拿到直接返回
			logger.info("获取锁失败~~~~~~~~~~~~~~~~~~~~~" + checkLockName);
			return;
		}
		// 拿到锁后执行业务逻辑
		try {
			logger.info("checklock已经获取~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			check();
		} finally {
			// 释放锁
			lock.unlock(checkLockId, checkLockName);
			logger.info("checklock已经释放~~~~~~~~~~~~~~~~~~~~~");
		}

	}

	abstract void check();

	public void uploadCancelRejectP() {
		String canceLockId = znLockProperties.getCancellockId();
		String canceLockName = znLockProperties.getCancellockName();
		// 获取锁
		if (!lock.tryLock(canceLockId, canceLockName)) {
			// 没有拿到直接返回
			logger.info("获取锁失败~~~~~~~~~~~~~~~~~~~~~" + canceLockName);
			return;
		}
		// 拿到锁后执行业务逻辑
		try {
			logger.info("canceclaimlock已经获取~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			uploadCancelReject();
		} finally {
			// 释放锁
			lock.unlock(canceLockId, canceLockName);
			logger.info("cancelock已经释放~~~~~~~~~~~~~~~~~~~~~");
		}

	}

	abstract void uploadCancelReject();

}

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
	namespace="com.cntaiping.tpi.bxData.mapper.GoAgriLogMapper">


	<select id="uploadClaim"
		resultType="com.cntaiping.tpi.bxData.dto.TaskVo">

		select * from
		(
		select t1.*,t2.returnCode pma from goAgriLog
		t1,goAgriLog t2
		where
		t1.type='claim' and t1.status='PENDING'
		and
		t2.businessNo=t1.businessNo and t2.businessseqno='000' and
		t2.type='policy' and t2.status = 'S' and
		t2.returncode is not null
		order by t1.createtime desc
		) where rownum < 100

	</select>

	<select id="uploadUpdateClaim"
		resultType="com.cntaiping.tpi.bxData.dto.TaskVo">
		select * from (select t1.* from goAgriLog t1 where t1.type='claim' and
		t1.registno =
		#{registno} order by t1.createtime
		desc)  where rownum < 10 
		
	</select>


	<select id="uploadCase"
		resultType="com.cntaiping.tpi.bxData.dto.TaskVo">
		select * from (
select * from circ.goAgriLog t1 where t1.type='case' and
		t1.status='PENDING' order by t1.createtime desc) where rownum < 100
	</select>

	<select id="uploadCaseStep2"
		resultType="com.cntaiping.tpi.bxData.dto.TaskVo">
		select * from
		(
		select * from goAgriLog where claimno=
		#{claimno} and
		registno= #{registno} and type = 'claim' order by
		updatetime
		desc
		) where rownum < 2
	</select>

	<select id="uploadCancelReject"
		resultType="com.cntaiping.tpi.bxData.dto.TaskVo">
		
		select * from (select t1.*,t2.returnCode pma from goAgriLog t1 ,goAgriLog
		t2 where t1.type='cancel' and
		t1.status='PENDING' and t2.claimno
		=t1.claimno and t2.type = 'claim' and t2.status =
		'SUCCESS' and
		t2.returnCode is not null
		 order by t1.createtime
		desc) where  rownum < 100
		
	</select>



	<select id="uploadSync"
		resultType="com.cntaiping.tpi.bxData.dto.TaskVo">
		
		select * from  (select t1.* from goAgriLog t1 where
		t1.status='SYNC'
		 order by t1.createtime desc) where 
		rownum < 100
		
	</select>

	<select id="uploadCheck"
		resultType="com.cntaiping.tpi.bxData.dto.TaskVo">
		select * from (select * from goAgriLog t1 where t1.type='check' and
		t1.status='PENDING'
		 order by t1.createtime desc) where  rownum < 100
		
	</select>

	<select id="queryLock" resultType="com.cntaiping.tpi.bxData.dto.GoAgriLog">
		select * from goAgriLog where id=#{id} and type=
		#{type} and status='0'
	</select>

	<update id="tryLock">
		update goAgriLog set status='1' where id=#{id} and
		type= #{type} and status='0'
	</update>

	<update id="unLock">
		update goAgriLog set status='0' where id=#{id} and
		type= #{type} and status='1'
	</update>
</mapper>

  

package com.cntaiping.tpi.bxData.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.bxops.phoenix.mapper.EntityMapper;
import com.bxops.phoenix.mapper.annotation.Mapper;
import com.cntaiping.tpi.bxData.dto.TaskVo;
import com.cntaiping.tpi.bxData.dto.GoAgriLog;
import com.cntaiping.tpi.hbnx.dto.insCaseClaim.IfsHbAgeandNumCaseItemDto;

@Mapper
public interface GoAgriLogMapper extends EntityMapper<GoAgriLog> {

	List<TaskVo> uploadClaim();

	List<TaskVo> uploadUpdateClaim(@Param(value = "registno") String registno);

	List<TaskVo> uploadCase();

	TaskVo uploadCaseStep2(@Param(value = "claimno") String claimno, @Param(value = "registno") String registno);

	List<TaskVo> uploadCancelReject();

	List<TaskVo> uploadSync();

	List<TaskVo> uploadCheck();

	GoAgriLog queryLock(@Param(value = "id") String id, @Param(value = "type") String type);

	int tryLock(@Param(value = "id") String id, @Param(value = "type") String type);
	
	int unLock(@Param(value = "id") String id, @Param(value = "type") String type);
}

  

package com.cntaiping.tpi.bxData.vo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;

@ConfigurationProperties(prefix = "znlock")
public class ZnLockProperties {
	private String claimLockId;
	private String claimLockName;
	private String caselockId;
	private String caselockName;
	private String checklockId;
	private String checklockName;
	private String cancellockId;
	private String cancellockName;

	public String getClaimLockId() {
		return claimLockId;
	}

	public void setClaimLockId(String claimLockId) {
		this.claimLockId = claimLockId;
	}

	public String getClaimLockName() {
		return claimLockName;
	}

	public void setClaimLockName(String claimLockName) {
		this.claimLockName = claimLockName;
	}

	public String getCaselockId() {
		return caselockId;
	}

	public void setCaselockId(String caselockId) {
		this.caselockId = caselockId;
	}

	public String getCaselockName() {
		return caselockName;
	}

	public void setCaselockName(String caselockName) {
		this.caselockName = caselockName;
	}

	public String getChecklockId() {
		return checklockId;
	}

	public void setChecklockId(String checklockId) {
		this.checklockId = checklockId;
	}

	public String getChecklockName() {
		return checklockName;
	}

	public void setChecklockName(String checklockName) {
		this.checklockName = checklockName;
	}

	public String getCancellockId() {
		return cancellockId;
	}

	public void setCancellockId(String cancellockId) {
		this.cancellockId = cancellockId;
	}

	public String getCancellockName() {
		return cancellockName;
	}

	public void setCancellockName(String cancellockName) {
		this.cancellockName = cancellockName;
	}

}

  

posted @ 2021-07-12 17:10  步江伍德  阅读(18)  评论(0)    收藏  举报