DistributedLock.java 1.53 KB
package com.brframework.commondistributed.annotation;

import com.brframework.commondistributed.core.DistributedExpressionRootObject;
import com.brframework.commondistributed.exception.LockedException;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 分布式锁
 * @Author xu
 * @Date 2019年9月4日22:37:26
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributedLock {

    /**
     * lock key
     * 示例:
     *  "targetClassName + methodName + #phone"
     *  "targetClassName + methodName + 'simple'"
     *  "targetClassName + methodName + args[0]"
     * 使用spring el表达式,
     * 内置对象包括:
     * @see DistributedExpressionRootObject
     * method -> 当前method对象
     * methodName -> method.getName()
     * args -> 当前方法的参数,可以通过args[index]访问
     * target -> 当前调用对象
     * targetClassName -> target.getClass().getName()
     *
     */
    String key();

    /**
     * 锁被占用,是否等待解锁
     *
     * 不等待解锁时,如果出现锁被占用,将抛出锁占用异常
     * @see LockedException
     */
    boolean waitFor() default true;

    /**
     * waitFor为false,并且出现锁被占用,是否抛出LockedException异常
     * 应用:
     * 分布式定时任务,锁占用的时候不执行即可,不需要抛出异常
     */
    boolean throwException() default true;

}