Skip to content

Commit ed07bcb

Browse files
committed
ZooKeeper configurable with path
1 parent 9036a84 commit ed07bcb

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

shedlock-provider-zookeeper-curator/src/main/java/net/javacrumbs/shedlock/provider/zookeeper/curator/ZookeeperCuratorLockProvider.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.javacrumbs.shedlock.core.SimpleLock;
2121
import net.javacrumbs.shedlock.support.LockException;
2222
import org.apache.curator.framework.CuratorFramework;
23+
import org.apache.curator.utils.PathUtils;
2324
import org.apache.zookeeper.CreateMode;
2425
import org.apache.zookeeper.KeeperException;
2526

@@ -31,41 +32,49 @@
3132
* Locks kept using ZooKeeper. When locking, creates an ephemeral node with node name = lock name, when nlocking, removes the node.
3233
*/
3334
public class ZookeeperCuratorLockProvider implements LockProvider {
35+
public static final String DEFAULT_PATH = "/shedlock";
36+
private final String path;
3437
private final CuratorFramework client;
3538

3639
public ZookeeperCuratorLockProvider(CuratorFramework client) {
40+
this(client, DEFAULT_PATH);
41+
}
42+
43+
public ZookeeperCuratorLockProvider(CuratorFramework client, String path) {
3744
this.client = requireNonNull(client);
45+
this.path = PathUtils.validatePath(path);
3846
}
3947

4048
@Override
4149
public Optional<SimpleLock> lock(LockConfiguration lockConfiguration) {
4250
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));
4554
} catch (KeeperException.NodeExistsException ex) {
4655
return Optional.empty();
4756
} catch (Exception e) {
4857
throw new LockException("Can not create node", e);
4958
}
5059
}
5160

52-
private static String getNodeName(LockConfiguration lockConfiguration) {
53-
return "/" + lockConfiguration.getName();
61+
private String getNodePath(LockConfiguration lockConfiguration) {
62+
return path + "/" + lockConfiguration.getName();
5463
}
5564

5665
private static final class CuratorLock implements SimpleLock {
57-
private final LockConfiguration configuration;
66+
private final String nodePath;
5867
private final CuratorFramework client;
5968

60-
private CuratorLock(LockConfiguration configuration, CuratorFramework client) {
61-
this.configuration = configuration;
69+
private CuratorLock(String nodePath, CuratorFramework client) {
70+
this.nodePath = nodePath;
6271
this.client = client;
6372
}
6473

6574
@Override
6675
public void unlock() {
6776
try {
68-
client.delete().forPath(getNodeName(configuration));
77+
client.delete().forPath(nodePath);
6978
} catch (Exception e) {
7079
throw new LockException("Can not remove node", e);
7180
}

shedlock-provider-zookeeper-curator/src/test/java/net/javacrumbs/shedlock/provider/zookeeper/curator/ZookeeperCuratorLockProviderIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void stopZookeeper() throws IOException {
5252
}
5353

5454
private CuratorFramework newClient() {
55-
CuratorFramework client = CuratorFrameworkFactory.builder().namespace("test/node")
55+
CuratorFramework client = CuratorFrameworkFactory.builder().namespace("MyApp")
5656
.retryPolicy(new RetryOneTime(2000))
5757
.connectString(zkTestServer.getConnectString()).build();
5858
client.start();
@@ -95,7 +95,7 @@ protected void assertLocked(String lockName) {
9595

9696
private Stat getNodeStat(String lockName) {
9797
try {
98-
return client.checkExists().forPath("/" + lockName);
98+
return client.checkExists().forPath("/shedlock/" + lockName);
9999
} catch (Exception e) {
100100
throw new RuntimeException(e);
101101
}

0 commit comments

Comments
 (0)