Refactor Atomix samples (#2696)
This commit is contained in:
committed by
GitHub
parent
3220913767
commit
7cb94f676c
@@ -1,30 +1,27 @@
|
||||
package com.atomix.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import io.atomix.AtomixReplica;
|
||||
import io.atomix.catalyst.transport.Address;
|
||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||
import io.atomix.copycat.server.storage.Storage;
|
||||
import io.atomix.copycat.server.storage.StorageLevel;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class BootstrapingCluster {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Storage storage = Storage.builder()
|
||||
.withDirectory(new File("log"))
|
||||
.withStorageLevel(StorageLevel.DISK)
|
||||
.build();
|
||||
.withDirectory(new File("log"))
|
||||
.withStorageLevel(StorageLevel.DISK)
|
||||
.build();
|
||||
AtomixReplica replica = AtomixReplica.builder(new Address("localhost", 8700))
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
CompletableFuture<AtomixReplica> completableFuture = replica.bootstrap();
|
||||
completableFuture.join();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.atomix.example;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.atomix.AtomixClient;
|
||||
import io.atomix.catalyst.transport.Address;
|
||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||
import io.atomix.collections.DistributedMap;
|
||||
|
||||
public class ClientExample {
|
||||
|
||||
public static void main(String args[]) throws InterruptedException {
|
||||
AtomixClient client = AtomixClient.builder()
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
List<Address> cluster = Arrays.asList(new Address("localhost", 8700), new Address("localhsot", 8701));
|
||||
client.connect(cluster)
|
||||
.thenRun(() -> System.out.println("Client Connected"));
|
||||
|
||||
Thread.sleep(5000);
|
||||
|
||||
DistributedMap<Object, Object> map = client.getMap("map")
|
||||
.join();
|
||||
|
||||
String value = (String) map.get("bar")
|
||||
.join();
|
||||
System.out.println("Value: " + value);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,42 +1,42 @@
|
||||
package com.atomix.example;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.atomix.AtomixReplica;
|
||||
import io.atomix.catalyst.transport.Address;
|
||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||
import io.atomix.collections.DistributedMap;
|
||||
import io.atomix.concurrent.DistributedLock;
|
||||
import io.atomix.copycat.server.storage.Storage;
|
||||
import io.atomix.copycat.server.storage.StorageLevel;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class OtherNodes {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
List<Address> cluster = Arrays.asList(new Address("localhost", 8700), new Address("localhost", 8701), new Address("localhost", 8702));
|
||||
List<Address> cluster = Arrays
|
||||
.asList(
|
||||
new Address("localhost", 8700),
|
||||
new Address("localhost", 8701),
|
||||
new Address("localhost", 8702));
|
||||
|
||||
Storage storage = Storage.builder()
|
||||
.withDirectory(new File("log"))
|
||||
.withStorageLevel(StorageLevel.DISK)
|
||||
.build();
|
||||
.withDirectory(new File("log"))
|
||||
.withStorageLevel(StorageLevel.DISK)
|
||||
.build();
|
||||
|
||||
AtomixReplica replica2 = AtomixReplica.builder(new Address("localhost", 8701))
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
WorkerThread WT1 = new WorkerThread(replica2, cluster);
|
||||
WT1.run();
|
||||
|
||||
AtomixReplica replica3 = AtomixReplica.builder(new Address("localhost", 8702))
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
WorkerThread WT2 = new WorkerThread(replica3, cluster);
|
||||
WT2.run();
|
||||
@@ -44,30 +44,28 @@ public class OtherNodes {
|
||||
Thread.sleep(6000);
|
||||
|
||||
DistributedLock lock = replica2.getLock("my-lock")
|
||||
.join();
|
||||
.join();
|
||||
lock.lock()
|
||||
.thenRun(() -> System.out.println("Acquired a lock"));
|
||||
.thenRun(() -> System.out.println("Acquired a lock"));
|
||||
|
||||
DistributedMap<Object, Object> map = replica2.getMap("map")
|
||||
.join();
|
||||
|
||||
// Put a value in the map and call the completion callback on response
|
||||
map.put("bar", "Hello world!")
|
||||
.thenRun(() -> System.out.println("Value is set in Distributed Map"));
|
||||
replica2.getMap("map")
|
||||
.thenCompose(m -> m.put("bar", "Hello world!"))
|
||||
.thenRun(() -> System.out.println("Value is set in Distributed Map"))
|
||||
.join();
|
||||
}
|
||||
|
||||
private static class WorkerThread extends Thread {
|
||||
AtomixReplica replica;
|
||||
List<Address> cluster;
|
||||
private AtomixReplica replica;
|
||||
private List<Address> cluster;
|
||||
|
||||
public WorkerThread(AtomixReplica replica, List<Address> cluster) {
|
||||
WorkerThread(AtomixReplica replica, List<Address> cluster) {
|
||||
this.replica = replica;
|
||||
this.cluster = cluster;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
replica.join(cluster)
|
||||
.join();
|
||||
.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user