|
20 | 20 | import net.javacrumbs.shedlock.core.SimpleLock;
|
21 | 21 | import net.javacrumbs.shedlock.support.LockException;
|
22 | 22 | import org.apache.curator.framework.CuratorFramework;
|
| 23 | +import org.apache.curator.utils.PathUtils; |
23 | 24 | import org.apache.zookeeper.CreateMode;
|
24 | 25 | import org.apache.zookeeper.KeeperException;
|
25 | 26 |
|
|
31 | 32 | * Locks kept using ZooKeeper. When locking, creates an ephemeral node with node name = lock name, when nlocking, removes the node.
|
32 | 33 | */
|
33 | 34 | public class ZookeeperCuratorLockProvider implements LockProvider {
|
| 35 | + public static final String DEFAULT_PATH = "/shedlock"; |
| 36 | + private final String path; |
34 | 37 | private final CuratorFramework client;
|
35 | 38 |
|
36 | 39 | public ZookeeperCuratorLockProvider(CuratorFramework client) {
|
| 40 | + this(client, DEFAULT_PATH); |
| 41 | + } |
| 42 | + |
| 43 | + public ZookeeperCuratorLockProvider(CuratorFramework client, String path) { |
37 | 44 | this.client = requireNonNull(client);
|
| 45 | + this.path = PathUtils.validatePath(path); |
38 | 46 | }
|
39 | 47 |
|
40 | 48 | @Override
|
41 | 49 | public Optional<SimpleLock> lock(LockConfiguration lockConfiguration) {
|
42 | 50 | try {
|
43 |
| - client.create().withMode(CreateMode.EPHEMERAL).forPath(getNodeName(lockConfiguration)); |
44 |
| - return Optional.of(new CuratorLock(lockConfiguration, client)); |
| 51 | + String nodePath = getNodePath(lockConfiguration); |
| 52 | + client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(nodePath); |
| 53 | + return Optional.of(new CuratorLock(nodePath, client)); |
45 | 54 | } catch (KeeperException.NodeExistsException ex) {
|
46 | 55 | return Optional.empty();
|
47 | 56 | } catch (Exception e) {
|
48 | 57 | throw new LockException("Can not create node", e);
|
49 | 58 | }
|
50 | 59 | }
|
51 | 60 |
|
52 |
| - private static String getNodeName(LockConfiguration lockConfiguration) { |
53 |
| - return "/" + lockConfiguration.getName(); |
| 61 | + private String getNodePath(LockConfiguration lockConfiguration) { |
| 62 | + return path + "/" + lockConfiguration.getName(); |
54 | 63 | }
|
55 | 64 |
|
56 | 65 | private static final class CuratorLock implements SimpleLock {
|
57 |
| - private final LockConfiguration configuration; |
| 66 | + private final String nodePath; |
58 | 67 | private final CuratorFramework client;
|
59 | 68 |
|
60 |
| - private CuratorLock(LockConfiguration configuration, CuratorFramework client) { |
61 |
| - this.configuration = configuration; |
| 69 | + private CuratorLock(String nodePath, CuratorFramework client) { |
| 70 | + this.nodePath = nodePath; |
62 | 71 | this.client = client;
|
63 | 72 | }
|
64 | 73 |
|
65 | 74 | @Override
|
66 | 75 | public void unlock() {
|
67 | 76 | try {
|
68 |
| - client.delete().forPath(getNodeName(configuration)); |
| 77 | + client.delete().forPath(nodePath); |
69 | 78 | } catch (Exception e) {
|
70 | 79 | throw new LockException("Can not remove node", e);
|
71 | 80 | }
|
|
0 commit comments