| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package locking; |
| 21 |
|
| 22 |
import java.util.concurrent.Callable; |
| 23 |
import java.util.concurrent.ExecutorService; |
| 24 |
import java.util.concurrent.Executors; |
| 25 |
import java.util.concurrent.TimeUnit; |
| 26 |
import org.apache.curator.framework.CuratorFramework; |
| 27 |
import org.apache.curator.framework.CuratorFrameworkFactory; |
| 28 |
import org.apache.curator.retry.ExponentialBackoffRetry; |
| 29 |
import org.apache.curator.test.TestingServer; |
| 30 |
import org.apache.curator.utils.CloseableUtils; |
| 31 |
|
| 32 |
public class LockingExample { |
| 33 |
private static final int QTY = 5; |
| 34 |
private static final int REPETITIONS = QTY * 10; |
| 35 |
|
| 36 |
private static final String PATH = "/examples/locks"; |
| 37 |
|
| 38 |
public static void main(String[] args) throws Exception { |
| 39 |
// all of the useful sample code is in ExampleClientThatLocks.java |
| 40 |
|
| 41 |
// FakeLimitedResource simulates some external resource that can only be access by one process at a time |
| 42 |
final FakeLimitedResource resource = new FakeLimitedResource(); |
| 43 |
|
| 44 |
ExecutorService service = Executors.newFixedThreadPool(QTY); |
| 45 |
final TestingServer server = new TestingServer(); |
| 46 |
try { |
| 47 |
for (int i = 0; i < QTY; ++i) { |
| 48 |
final int index = i; |
| 49 |
Callable<Void> task = new Callable<Void>() { |
| 50 |
@Override |
| 51 |
public Void call() throws Exception { |
| 52 |
CuratorFramework client = CuratorFrameworkFactory.newClient( |
| 53 |
server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); |
| 54 |
try { |
| 55 |
client.start(); |
| 56 |
|
| 57 |
ExampleClientThatLocks example = |
| 58 |
new ExampleClientThatLocks(client, PATH, resource, "Client " + index); |
| 59 |
for (int j = 0; j < REPETITIONS; ++j) { |
| 60 |
example.doWork(10, TimeUnit.SECONDS); |
| 61 |
} |
| 62 |
} catch (InterruptedException e) { |
| 63 |
Thread.currentThread().interrupt(); |
| 64 |
} catch (Exception e) { |
| 65 |
e.printStackTrace(); |
| 66 |
// log or do something |
| 67 |
} finally { |
| 68 |
CloseableUtils.closeQuietly(client); |
| 69 |
} |
| 70 |
return null; |
| 71 |
} |
| 72 |
}; |
| 73 |
service.submit(task); |
| 74 |
} |
| 75 |
|
| 76 |
service.shutdown(); |
| 77 |
service.awaitTermination(10, TimeUnit.MINUTES); |
| 78 |
} finally { |
| 79 |
CloseableUtils.closeQuietly(server); |
| 80 |
} |
| 81 |
} |
| 82 |
} |