| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.springframework.batch.retry.support; |
| 18 | |
| 19 | import org.springframework.batch.repeat.RepeatOperations; |
| 20 | import org.springframework.batch.retry.RetryCallback; |
| 21 | import org.springframework.batch.retry.RetryContext; |
| 22 | import org.springframework.batch.retry.RetryOperations; |
| 23 | |
| 24 | /** |
| 25 | * Global variable support for retry clients. Normally it is not necessary for |
| 26 | * clients to be aware of the surrounding environment because a |
| 27 | * {@link RetryCallback} can always use the context it is passed by the |
| 28 | * enclosing {@link RetryOperations}. But occasionally it might be helpful to |
| 29 | * have lower level access to the ongoing {@link RetryContext} so we provide a |
| 30 | * global accessor here. The mutator methods ({@link #clear()} and |
| 31 | * {@link #register(RetryContext)} should not be used except internally by |
| 32 | * {@link RetryOperations} implementations. |
| 33 | * |
| 34 | * @author Dave Syer |
| 35 | * |
| 36 | */ |
| 37 | public class RetrySynchronizationManager { |
| 38 | |
| 39 | private RetrySynchronizationManager() {} |
| 40 | |
| 41 | private static final ThreadLocal context = new ThreadLocal(); |
| 42 | |
| 43 | /** |
| 44 | * Public accessor for the locally enclosing {@link RetryContext}. |
| 45 | * |
| 46 | * @return the current retry context, or null if there isn't one |
| 47 | */ |
| 48 | public static RetryContext getContext() { |
| 49 | RetryContext result = (RetryContext) context.get(); |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Method for registering a context - should only be used by |
| 55 | * {@link RetryOperations} implementations to ensure that |
| 56 | * {@link #getContext()} always returns the correct value. |
| 57 | * |
| 58 | * @param context the new context to register |
| 59 | * @return the old context if there was one |
| 60 | */ |
| 61 | public static RetryContext register(RetryContext context) { |
| 62 | RetryContext oldContext = getContext(); |
| 63 | RetrySynchronizationManager.context.set(context); |
| 64 | return oldContext; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Clear the current context at the end of a batch - should only be used by |
| 69 | * {@link RepeatOperations} implementations. |
| 70 | * |
| 71 | * @return the old value if there was one. |
| 72 | */ |
| 73 | public static RetryContext clear() { |
| 74 | RetryContext value = getContext(); |
| 75 | RetryContext parent = value == null ? null : value.getParent(); |
| 76 | RetrySynchronizationManager.context.set(parent); |
| 77 | return value; |
| 78 | } |
| 79 | |
| 80 | } |