|
1 | 1 | /**
|
2 | 2 | * Copyright 2009-2017 the original author or authors.
|
3 | 3 | *
|
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
5 |
| - * the License. You may obtain a copy of the License at |
| 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 |
6 | 7 | *
|
7 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
8 | 9 | *
|
9 |
| - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
10 |
| - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
11 |
| - * specific language governing permissions and limitations under the License. |
| 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. |
12 | 15 | */
|
13 | 16 | package net.javacrumbs.shedlock.spring;
|
14 | 17 |
|
15 |
| -import static java.time.Instant.now; |
16 |
| -import static java.time.temporal.ChronoUnit.MILLIS; |
17 |
| -import static java.util.Objects.requireNonNull; |
18 |
| - |
19 |
| -import java.lang.reflect.Method; |
20 |
| -import java.time.Duration; |
21 |
| -import java.time.Instant; |
22 |
| -import java.time.temporal.ChronoUnit; |
23 |
| -import java.time.temporal.TemporalAmount; |
24 |
| -import java.util.Optional; |
25 |
| - |
| 18 | +import net.javacrumbs.shedlock.core.LockConfiguration; |
| 19 | +import net.javacrumbs.shedlock.core.LockConfigurationExtractor; |
| 20 | +import net.javacrumbs.shedlock.core.SchedulerLock; |
26 | 21 | import org.slf4j.Logger;
|
27 | 22 | import org.slf4j.LoggerFactory;
|
28 | 23 | import org.springframework.aop.support.AopUtils;
|
29 | 24 | import org.springframework.context.EmbeddedValueResolverAware;
|
30 | 25 | import org.springframework.core.annotation.AnnotationUtils;
|
31 | 26 | import org.springframework.scheduling.support.ScheduledMethodRunnable;
|
| 27 | +import org.springframework.util.StringUtils; |
32 | 28 | import org.springframework.util.StringValueResolver;
|
33 | 29 |
|
34 |
| -import net.javacrumbs.shedlock.core.LockConfiguration; |
35 |
| -import net.javacrumbs.shedlock.core.LockConfigurationExtractor; |
36 |
| -import net.javacrumbs.shedlock.core.SchedulerLock; |
| 30 | +import java.lang.reflect.Method; |
| 31 | +import java.time.Duration; |
| 32 | +import java.time.Instant; |
| 33 | +import java.time.temporal.ChronoUnit; |
| 34 | +import java.time.temporal.TemporalAmount; |
| 35 | +import java.util.Optional; |
| 36 | + |
| 37 | +import static java.time.Instant.now; |
| 38 | +import static java.time.temporal.ChronoUnit.MILLIS; |
| 39 | +import static java.util.Objects.requireNonNull; |
37 | 40 |
|
38 | 41 | /**
|
39 |
| - * Extracts configuration form Spring scheduled task. Is able to extract information from <ol> <li>Annotation based |
40 |
| - * scheduler</li> </ol> |
| 42 | + * Extracts configuration form Spring scheduled task. Is able to extract information from |
| 43 | + * <ol> |
| 44 | + * <li>Annotation based scheduler</li> |
| 45 | + * </ol> |
41 | 46 | */
|
42 | 47 | public class SpringLockConfigurationExtractor
|
43 | 48 | implements
|
@@ -112,49 +117,42 @@ SchedulerLock findAnnotation(ScheduledMethodRunnable task) {
|
112 | 117 | }
|
113 | 118 |
|
114 | 119 | TemporalAmount getLockAtMostFor(SchedulerLock annotation) {
|
115 |
| - long valueFromAnnotation = annotation.lockAtMostFor(); |
116 |
| - if (valueFromAnnotation < 0) { |
117 |
| - String lockAtMostForString = annotation.lockAtMostForString(); |
118 |
| - if (lockAtMostForString.length() > 0) { |
119 |
| - if (this.embeddedValueResolver != null) { |
120 |
| - lockAtMostForString = this.embeddedValueResolver |
121 |
| - .resolveStringValue(lockAtMostForString); |
122 |
| - } |
123 |
| - if (lockAtMostForString != null && lockAtMostForString.length() > 0) { |
124 |
| - try { |
125 |
| - valueFromAnnotation = Long.valueOf(lockAtMostForString); |
126 |
| - } catch (NumberFormatException nfe) { |
127 |
| - logger.error("Unparseable lockAtMostFor", nfe); |
128 |
| - } |
129 |
| - } |
130 |
| - } |
131 |
| - } |
132 |
| - return valueFromAnnotation >= 0 |
133 |
| - ? Duration.of(valueFromAnnotation, MILLIS) |
134 |
| - : defaultLockAtMostFor; |
| 120 | + return getValue( |
| 121 | + annotation.lockAtMostFor(), |
| 122 | + annotation.lockAtMostForString(), |
| 123 | + this.defaultLockAtMostFor, |
| 124 | + "lockAtMostForString" |
| 125 | + ); |
135 | 126 | }
|
136 | 127 |
|
137 | 128 | Duration getLockAtLeastFor(SchedulerLock annotation) {
|
138 |
| - long valueFromAnnotation = annotation.lockAtLeastFor(); |
139 |
| - if (valueFromAnnotation < 0) { |
140 |
| - String lockAtLeastForString = annotation.lockAtLeastForString(); |
141 |
| - if (this.embeddedValueResolver != null) { |
142 |
| - lockAtLeastForString = this.embeddedValueResolver |
143 |
| - .resolveStringValue(lockAtLeastForString); |
| 129 | + return getValue( |
| 130 | + annotation.lockAtLeastFor(), |
| 131 | + annotation.lockAtLeastForString(), |
| 132 | + this.defaultLockAtLeastFor, |
| 133 | + "lockAtLeastForString" |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + private Duration getValue(long valueFromAnnotation, String stringValueFromAnnotation, TemporalAmount defaultValue, final String paramName) { |
| 138 | + if (valueFromAnnotation >= 0) { |
| 139 | + return Duration.of(valueFromAnnotation, MILLIS); |
| 140 | + } else if (StringUtils.hasText(stringValueFromAnnotation)) { |
| 141 | + if (embeddedValueResolver != null) { |
| 142 | + stringValueFromAnnotation = embeddedValueResolver.resolveStringValue(stringValueFromAnnotation); |
144 | 143 | }
|
145 |
| - if (lockAtLeastForString != null && lockAtLeastForString.length() > 0) { |
146 |
| - try { |
147 |
| - valueFromAnnotation = Long.valueOf(lockAtLeastForString); |
148 |
| - } catch (NumberFormatException nfe) { |
149 |
| - logger.error("Unparseable lockAtLeastFor", nfe); |
150 |
| - } |
| 144 | + try { |
| 145 | + return Duration.of(Long.valueOf(stringValueFromAnnotation), MILLIS); |
| 146 | + } catch (NumberFormatException nfe) { |
| 147 | + throw new IllegalArgumentException("Invalid " + paramName + " value \"" + stringValueFromAnnotation + "\" - cannot parse into long"); |
151 | 148 | }
|
| 149 | + } else { |
| 150 | + return Duration.from(defaultValue); |
152 | 151 | }
|
153 |
| - return valueFromAnnotation >= 0 |
154 |
| - ? Duration.of(valueFromAnnotation, MILLIS) |
155 |
| - : defaultLockAtLeastFor; |
156 | 152 | }
|
157 | 153 |
|
| 154 | + |
| 155 | + |
158 | 156 | private boolean shouldLock(SchedulerLock annotation) {
|
159 | 157 | return annotation != null;
|
160 | 158 | }
|
|
0 commit comments