spring-retry RetrySynchronizationManager 源码

  • 2022-08-16
  • 浏览 (357)

spring-retry RetrySynchronizationManager 代码

文件路径:/src/main/java/org/springframework/retry/support/RetrySynchronizationManager.java

/*
 * Copyright 2006-2022 the original author or authors.
 *
 * 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
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.springframework.retry.support;

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryOperations;

/**
 * Global variable support for retry clients. Normally it is not necessary for clients to
 * be aware of the surrounding environment because a {@link RetryCallback} can always use
 * the context it is passed by the enclosing {@link RetryOperations}. But occasionally it
 * might be helpful to have lower level access to the ongoing {@link RetryContext} so we
 * provide a global accessor here. The mutator methods ({@link #clear()} and
 * {@link #register(RetryContext)} should not be used except internally by
 * {@link RetryOperations} implementations.
 *
 * @author Dave Syer
 *
 */
public final class RetrySynchronizationManager {

	private RetrySynchronizationManager() {
	}

	private static final ThreadLocal<RetryContext> context = new ThreadLocal<>();

	/**
	 * Public accessor for the locally enclosing {@link RetryContext}.
	 * @return the current retry context, or null if there isn't one
	 */
	public static RetryContext getContext() {
		RetryContext result = context.get();
		return result;
	}

	/**
	 * Method for registering a context - should only be used by {@link RetryOperations}
	 * implementations to ensure that {@link #getContext()} always returns the correct
	 * value.
	 * @param context the new context to register
	 * @return the old context if there was one
	 */
	public static RetryContext register(RetryContext context) {
		RetryContext oldContext = getContext();
		RetrySynchronizationManager.context.set(context);
		return oldContext;
	}

	/**
	 * Clear the current context at the end of a batch - should only be used by
	 * {@link RetryOperations} implementations.
	 * @return the old value if there was one.
	 */
	public static RetryContext clear() {
		RetryContext value = getContext();
		RetryContext parent = value == null ? null : value.getParent();
		RetrySynchronizationManager.context.set(parent);
		return value;
	}

}

相关信息

spring-retry 源码目录

相关文章

spring-retry Args 源码

spring-retry DefaultRetryState 源码

spring-retry RetrySimulation 源码

spring-retry RetrySimulator 源码

spring-retry RetryTemplate 源码

spring-retry RetryTemplateBuilder 源码

0  赞