zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Appium 服务命令行参数
    DC 输入 输出 时钟 PVT设置
    .synopsys_dc.setup编写
    Excel VBA编程常用语句300句
    C# 泛型单例工厂
    C# Winform与JS交互
    SQL分析“聚集索引、非聚集索引”区别
    C# ClassHelper动态创建程序集和类, 添加/删除类属性
    从30个角度对比 PostgreSQL 和 MySQL
    C# 常用类和命名空间
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/13958565.html
Copyright © 2011-2022 走看看