Skip to content

Commit 12d9c32

Browse files
committed
Extracted LockingTaskExecutor
1 parent 8acea6d commit 12d9c32

File tree

7 files changed

+78
-25
lines changed

7 files changed

+78
-25
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ public void run() {
199199

200200

201201
##Change log
202+
## 0.3.0
203+
1. Extracted LockingTaskExecutor
204+
2. LockManager.executeIfNotLocked renamed to executeWithLock
205+
3. Default table name in JDBC lock providers
206+
202207
## 0.3.0
203208
1. `@ShedlulerLock.name` made obligatory
204209
2. `@ShedlulerLock.lockForMillis` renamed to lockAtMostFor

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,26 @@
2929
public class DefaultLockManager implements LockManager {
3030
private static final Logger logger = LoggerFactory.getLogger(DefaultLockManager.class);
3131

32-
private final LockProvider lockProvider;
32+
private final LockingTaskExecutor lockingTaskExecutor;
3333
private final LockConfigurationExtractor lockConfigurationExtractor;
3434

3535
public DefaultLockManager(LockProvider lockProvider, LockConfigurationExtractor lockConfigurationExtractor) {
36-
this.lockProvider = requireNonNull(lockProvider);
36+
this(new DefaultLockingTaskExecutor(lockProvider), lockConfigurationExtractor);
37+
}
38+
39+
public DefaultLockManager(LockingTaskExecutor lockingTaskExecutor, LockConfigurationExtractor lockConfigurationExtractor) {
40+
this.lockingTaskExecutor = requireNonNull(lockingTaskExecutor);
3741
this.lockConfigurationExtractor = requireNonNull(lockConfigurationExtractor);
3842
}
3943

4044
@Override
41-
public void executeIfNotLocked(Runnable task) {
45+
public void executeWithLock(Runnable task) {
4246
Optional<LockConfiguration> lockConfigOptional = lockConfigurationExtractor.getLockConfiguration(task);
4347
if (!lockConfigOptional.isPresent()) {
4448
logger.debug("No lock configuration for {}. Executing without lock.", task);
4549
task.run();
4650
} else {
47-
executeIfNotLocked(task, lockConfigOptional.get());
48-
}
49-
}
50-
51-
private void executeIfNotLocked(Runnable task, LockConfiguration lockConfig) {
52-
Optional<SimpleLock> lock = lockProvider.lock(lockConfig);
53-
if (lock.isPresent()) {
54-
try {
55-
logger.debug("Locked {}.", lockConfig.getName());
56-
task.run();
57-
} finally {
58-
lock.get().unlock();
59-
logger.debug("Unlocked {}.", lockConfig.getName());
60-
}
61-
} else {
62-
logger.info("Not executing {}. It's locked.", lockConfig.getName());
51+
lockingTaskExecutor.executeWithLock(task, lockConfigOptional.get());
6352
}
6453
}
6554
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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+
package net.javacrumbs.shedlock.core;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import java.util.Optional;
22+
23+
import static java.util.Objects.requireNonNull;
24+
25+
/**
26+
* Default {@link LockingTaskExecutor} implementation.
27+
*/
28+
public class DefaultLockingTaskExecutor implements LockingTaskExecutor {
29+
private static final Logger logger = LoggerFactory.getLogger(DefaultLockingTaskExecutor.class);
30+
private final LockProvider lockProvider;
31+
32+
public DefaultLockingTaskExecutor(LockProvider lockProvider) {
33+
this.lockProvider = requireNonNull(lockProvider);
34+
}
35+
36+
@Override
37+
public void executeWithLock(Runnable task, LockConfiguration lockConfig) {
38+
Optional<SimpleLock> lock = lockProvider.lock(lockConfig);
39+
if (lock.isPresent()) {
40+
try {
41+
logger.debug("Locked {}.", lockConfig.getName());
42+
task.run();
43+
} finally {
44+
lock.get().unlock();
45+
logger.debug("Unlocked {}.", lockConfig.getName());
46+
}
47+
} else {
48+
logger.info("Not executing {}. It's locked.", lockConfig.getName());
49+
}
50+
}
51+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
* Executes task if not locked.
2020
*/
2121
public interface LockManager {
22-
void executeIfNotLocked(Runnable task);
22+
void executeWithLock(Runnable task);
2323
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import static java.util.Objects.requireNonNull;
1919

2020
/**
21-
* Executes wrapped runnable using {@link LockManager#executeIfNotLocked(Runnable)}
21+
* Executes wrapped runnable using {@link LockManager#executeWithLock(Runnable)}
2222
*/
2323
public class LockableRunnable implements Runnable {
2424
private final Runnable task;
@@ -32,6 +32,6 @@ public LockableRunnable(Runnable task, LockManager lockManager) {
3232

3333
@Override
3434
public void run() {
35-
lockManager.executeIfNotLocked(task);
35+
lockManager.executeWithLock(task);
3636
}
3737
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package net.javacrumbs.shedlock.core;
2+
3+
public interface LockingTaskExecutor {
4+
/**
5+
* Executes task if it's not already running.
6+
*/
7+
void executeWithLock(Runnable task, LockConfiguration lockConfig);
8+
}

shedlock-core/src/test/java/net/javacrumbs/shedlock/core/DefaultLockManagerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class DefaultLockManagerTest {
4343
public void noConfigNoLock() {
4444
when(lockConfigurationExtractor.getLockConfiguration(task)).thenReturn(Optional.empty());
4545

46-
defaultLockManager.executeIfNotLocked(task);
46+
defaultLockManager.executeWithLock(task);
4747
verify(task).run();
4848
verifyZeroInteractions(lockProvider);
4949
}
@@ -53,7 +53,7 @@ public void executeIfLockAvailable() {
5353
when(lockConfigurationExtractor.getLockConfiguration(task)).thenReturn(Optional.of(LOCK_CONFIGURATION));
5454
when(lockProvider.lock(LOCK_CONFIGURATION)).thenReturn(Optional.of(lock));
5555

56-
defaultLockManager.executeIfNotLocked(task);
56+
defaultLockManager.executeWithLock(task);
5757
verify(task).run();
5858
InOrder inOrder = inOrder(task, lock);
5959
inOrder.verify(task).run();
@@ -65,7 +65,7 @@ public void doNotExecuteIfAlreadyLocked() {
6565
when(lockConfigurationExtractor.getLockConfiguration(task)).thenReturn(Optional.of(LOCK_CONFIGURATION));
6666
when(lockProvider.lock(LOCK_CONFIGURATION)).thenReturn(Optional.empty());
6767

68-
defaultLockManager.executeIfNotLocked(task);
68+
defaultLockManager.executeWithLock(task);
6969
verifyZeroInteractions(task);
7070
}
7171

0 commit comments

Comments
 (0)