Skip to content

Commit a9263f9

Browse files
committed
String param extractor cleanup
1 parent 9c9bcc1 commit a9263f9

File tree

3 files changed

+77
-90
lines changed

3 files changed

+77
-90
lines changed

shedlock-core/src/main/java/net/javacrumbs/shedlock/core/SchedulerLock.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/**
22
* Copyright 2009-2017 the original author or authors.
33
*
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
67
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
89
*
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.
1215
*/
1316
package net.javacrumbs.shedlock.core;
1417

@@ -17,7 +20,7 @@
1720
import java.lang.annotation.RetentionPolicy;
1821
import java.lang.annotation.Target;
1922

20-
@Target({ ElementType.METHOD })
23+
@Target({ElementType.METHOD})
2124
@Retention(RetentionPolicy.RUNTIME)
2225
public @interface SchedulerLock {
2326
/**
@@ -37,10 +40,10 @@
3740
String lockAtMostForString() default "";
3841

3942
/**
40-
* The lock will be held at least for X millis. Can be used if you really need to execute the task at most once in
41-
* given period of time. If the duration of the task is shorter than clock difference between nodes, the task can be
42-
* theoretically executed more than once (one node after another). By setting this parameter, you can make sure that
43-
* the lock will be kept at least for given period of time.
43+
* The lock will be held at least for X millis. Can be used if you really need to execute the task
44+
* at most once in given period of time. If the duration of the task is shorter than clock difference between nodes, the task can
45+
* be theoretically executed more than once (one node after another). By setting this parameter, you can make sure that the
46+
* lock will be kept at least for given period of time.
4447
*/
4548
long lockAtLeastFor() default -1;
4649

shedlock-spring/src/main/java/net/javacrumbs/shedlock/spring/SpringLockConfigurationExtractor.java

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
/**
22
* Copyright 2009-2017 the original author or authors.
33
*
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
67
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
89
*
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.
1215
*/
1316
package net.javacrumbs.shedlock.spring;
1417

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;
2621
import org.slf4j.Logger;
2722
import org.slf4j.LoggerFactory;
2823
import org.springframework.aop.support.AopUtils;
2924
import org.springframework.context.EmbeddedValueResolverAware;
3025
import org.springframework.core.annotation.AnnotationUtils;
3126
import org.springframework.scheduling.support.ScheduledMethodRunnable;
27+
import org.springframework.util.StringUtils;
3228
import org.springframework.util.StringValueResolver;
3329

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;
3740

3841
/**
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>
4146
*/
4247
public class SpringLockConfigurationExtractor
4348
implements
@@ -112,49 +117,42 @@ SchedulerLock findAnnotation(ScheduledMethodRunnable task) {
112117
}
113118

114119
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+
);
135126
}
136127

137128
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);
144143
}
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");
151148
}
149+
} else {
150+
return Duration.from(defaultValue);
152151
}
153-
return valueFromAnnotation >= 0
154-
? Duration.of(valueFromAnnotation, MILLIS)
155-
: defaultLockAtLeastFor;
156152
}
157153

154+
155+
158156
private boolean shouldLock(SchedulerLock annotation) {
159157
return annotation != null;
160158
}

shedlock-spring/src/main/java/net/javacrumbs/shedlock/spring/SpringLockableTaskSchedulerFactory.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
*/
1616
package net.javacrumbs.shedlock.spring;
1717

18-
import static net.javacrumbs.shedlock.spring.SpringLockConfigurationExtractor.DEFAULT_LOCK_AT_MOST_FOR;
19-
20-
import java.time.temporal.TemporalAmount;
21-
import java.util.concurrent.ScheduledExecutorService;
22-
18+
import net.javacrumbs.shedlock.core.DefaultLockManager;
19+
import net.javacrumbs.shedlock.core.LockProvider;
2320
import org.springframework.scheduling.TaskScheduler;
2421
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
2522
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
2623

27-
import net.javacrumbs.shedlock.core.DefaultLockManager;
28-
import net.javacrumbs.shedlock.core.LockConfigurationExtractor;
29-
import net.javacrumbs.shedlock.core.LockProvider;
24+
import java.time.temporal.TemporalAmount;
25+
import java.util.concurrent.ScheduledExecutorService;
26+
27+
import static net.javacrumbs.shedlock.spring.SpringLockConfigurationExtractor.DEFAULT_LOCK_AT_MOST_FOR;
3028

3129
/**
3230
* Helper class to simplify configuration of Spring LockableTaskScheduler.
@@ -45,10 +43,6 @@ public static LockableTaskScheduler newLockableTaskScheduler(TaskScheduler taskS
4543
return new LockableTaskScheduler(taskScheduler, new DefaultLockManager(lockProvider, new SpringLockConfigurationExtractor(defaultLockAtMostFor)));
4644
}
4745

48-
public static LockableTaskScheduler newLockableTaskScheduler(TaskScheduler taskScheduler, LockProvider lockProvider, LockConfigurationExtractor lockConfigurationExtractor) {
49-
return new LockableTaskScheduler(taskScheduler, new DefaultLockManager(lockProvider, lockConfigurationExtractor));
50-
}
51-
5246
/**
5347
* Wraps the task scheduler and ensures that {@link net.javacrumbs.shedlock.core.SchedulerLock} annotated methods
5448
* are locked using the lockProvider
@@ -89,19 +83,11 @@ public static LockableTaskScheduler newLockableTaskScheduler(ScheduledExecutorSe
8983
*
9084
* @param poolSize size of the thread pool
9185
* @param lockProvider lock provider to be used
92-
* @param lockConfigurationExtractor lock configuration extractor to use.
9386
*/
94-
public static LockableTaskScheduler newLockableTaskScheduler(int poolSize, LockProvider lockProvider, LockConfigurationExtractor lockConfigurationExtractor) {
87+
public static LockableTaskScheduler newLockableTaskScheduler(int poolSize, LockProvider lockProvider) {
9588
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
9689
taskScheduler.setPoolSize(poolSize);
9790
taskScheduler.initialize();
98-
if (lockConfigurationExtractor == null) {
99-
return newLockableTaskScheduler(taskScheduler, lockProvider);
100-
}
101-
return newLockableTaskScheduler(taskScheduler, lockProvider, lockConfigurationExtractor);
102-
}
103-
104-
public static LockableTaskScheduler newLockableTaskScheduler(int poolSize, LockProvider lockProvider) {
105-
return newLockableTaskScheduler(poolSize, lockProvider, (LockConfigurationExtractor)null);
91+
return newLockableTaskScheduler(taskScheduler, lockProvider);
10692
}
10793
}

0 commit comments

Comments
 (0)