-
Notifications
You must be signed in to change notification settings - Fork 6
/
SmartLockManager.java
86 lines (70 loc) · 2.25 KB
/
SmartLockManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/
package sirius.biz.locks;
import sirius.db.redis.Redis;
import sirius.kernel.di.GlobalContext;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.List;
/**
* Provides a "cluster aware" lock manager.
* <p>
* Uses {@link RedisLockManager} is <tt>Redis</tt> is configured or otherwise {@link JavaLockManager}
*/
@Register(framework = Locks.FRAMEWORK_LOCKS)
public class SmartLockManager implements LockManager {
@Part
private GlobalContext ctx;
@Part
private Redis redis;
private LockManager delegate;
@Nonnull
@Override
public String getName() {
return "smart";
}
private LockManager getDelegate() {
if (delegate == null) {
determineLockManager();
}
return delegate;
}
private void determineLockManager() {
if (redis.isConfigured()) {
Locks.LOG.INFO("SmartLockManager: Using RedisLockManager as Redis is configured");
delegate = ctx.getPart(RedisLockManager.NAME, LockManager.class);
} else {
Locks.LOG.INFO(
"SmartLockManager: Assuming a single machine setup as Redis isn't present - using fast JVM locks");
delegate = ctx.getPart(JavaLockManager.NAME, LockManager.class);
}
}
@Override
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout) {
return getDelegate().tryLock(lockName, acquireTimeout);
}
@Override
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout, @Nonnull Duration lockTimeout) {
return getDelegate().tryLock(lockName, acquireTimeout, lockTimeout);
}
@Override
public boolean isLocked(@Nonnull String lock) {
return getDelegate().isLocked(lock);
}
@Override
public void unlock(String lock, boolean force) {
getDelegate().unlock(lock, force);
}
@Override
public List<LockInfo> getLocks() {
return getDelegate().getLocks();
}
}