分布式锁切面类
/** * Redis缓存分布式锁切面类 * created by huangbaidong * created date 2017-03-30 */ @Aspect @Component public class RedisCacheDistributedLockAspect { public static final Logger LOG = Logger.getLogger(RedisCacheDistributedLockAspect.class); @Value("${time.server.port}") private Integer port; @Value("${time.server.host}") private String host; @Resource private RedisUtil redisUtil; protected RedisBasedDistributedLock payingOrderLock = null; private static final String PAYING_ORDER_LOCK_KEY = "paying.order.lock"; private static final long LOCK_EXPIRE = 5 * 1000; /** * 初始化锁信息 */ @PostConstruct public void init() { try { //时间服务器地址信息 SocketAddress addr = new InetSocketAddress(host, port); //初始化锁 payingOrderLock = new RedisBasedDistributedLock(redisUtil, PAYING_ORDER_LOCK_KEY, LOCK_EXPIRE, addr); } catch (IOException e) { e.printStackTrace(); } } /** * 在需要同步的方法前后加锁 * @param jp * @return * @throws Throwable */ @Around("execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.add*(..))" + "|| execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.remove*(..))" + "|| execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.update*(..))" + "|| execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.load*(..))") public Object payingOrderSyncLock(ProceedingJoinPoint jp) throws Throwable { Object o = null; if(payingOrderLock.tryLock(3, TimeUnit.SECONDS)) { try { o = jp.proceed(jp.getArgs()); } finally { payingOrderLock.unlock(); } } return o; } }