• ThreadLocal


    try {
                    LockAssert.startLock(lockName);
                    logger.debug("Locked '{}', lock will be held at most until {}", lockName, lockConfig.getLockAtMostUntil());
                    return TaskResult.result(task.call());
                } finally {
                    LockAssert.endLock();
                    lock.get().unlock();
                    if (logger.isDebugEnabled()) {
                        Instant lockAtLeastUntil = lockConfig.getLockAtLeastUntil();
                        Instant now = ClockProvider.now();
                        if (lockAtLeastUntil.isAfter(now)) {
                            logger.debug("Task finished, lock '{}' will be released at {}", lockName, lockAtLeastUntil);
                        } else {
                            logger.debug("Task finished, lock '{}' released", lockName);
                        }
                    }
                }
    /**
     * Copyright 2009-2020 the original author or authors.
     * <p>
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     * <p>
     * http://www.apache.org/licenses/LICENSE-2.0
     * <p>
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package net.javacrumbs.shedlock.core;
    
    import net.javacrumbs.shedlock.support.annotation.NonNull;
    
    /**
     * Asserts lock presence. The Spring ecosystem is so complicated, so one can not be sure that the lock is applied. This class
     * makes sure that the task is indeed locked.
     * <p>
     * If you use AOP with Kotlin, it does not have to work due to final methods, if you use TaskExecutor wrapper, it can be
     * broken by Sleuth,.
     */
    public class LockAssert {
        private static final ThreadLocal<String> currentLockName = ThreadLocal.withInitial(() -> null);
    
        static void startLock(String name) {
            currentLockName.set(name);
        }
    
        static boolean alreadyLockedBy(@NonNull String name) {
            return name.equals(currentLockName.get());
        }
    
        static void endLock() {
            currentLockName.remove();
        }
    
        /**
         * Throws an exception if the lock is not present.
         */
        public static void assertLocked() {
            if (currentLockName.get() == null) {
                throw new IllegalStateException("The task is not locked.");
            }
        }
    
        public static class TestHelper {
            /**
             * If pass is set to true, all LockAssert.assertLocked calls in current thread will pass.
             * To be used in unit tests only
             *
             * <code>
             * LockAssert.TestHelper.makeAllAssertsPass(true)
             * </code>
             */
            public static void makeAllAssertsPass(boolean pass) {
                if (pass) {
                    LockAssert.startLock("net.javacrumbs.shedlock.core.test-lock");
                } else {
                    LockAssert.endLock();
                }
            }
        }
    }

    implementation "net.javacrumbs.shedlock:shedlock-spring:${shedlockVersion}"
    implementation "net.javacrumbs.shedlock:shedlock-provider-mongo:${shedlockVersion}"

    shedlockVersion=4.15.1
  • 相关阅读:
    超酷的元素周期表
    TestLink在线Excel用例转换xml
    我也学习JAVA多线程-join
    request.getSession(true/false)的区别
    nginx location配置详细解释
    RestTemplate--解决中文乱码
    扇贝-每日一句
    Hexo博客系列(三)-将Hexo v3.x个人博客发布到GitLab Pages
    C程序的内存分区(节选自黑马训练营day1)
    CodeBlocks更换界面主题界面、汉化及去掉注释及字符串的下划线(汉化包的链接来自本站的BeatificDevin大神)
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/13958565.html
Copyright © 2020-2023  润新知