Refactor Atomix samples (#2696)
This commit is contained in:
parent
3220913767
commit
7cb94f676c
@ -5,6 +5,12 @@
|
|||||||
<artifactId>atomix</artifactId>
|
<artifactId>atomix</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.atomix</groupId>
|
<groupId>io.atomix</groupId>
|
||||||
@ -24,7 +30,6 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
package com.atomix.example;
|
package com.atomix.example;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
import io.atomix.AtomixReplica;
|
import io.atomix.AtomixReplica;
|
||||||
import io.atomix.catalyst.transport.Address;
|
import io.atomix.catalyst.transport.Address;
|
||||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||||
import io.atomix.copycat.server.storage.Storage;
|
import io.atomix.copycat.server.storage.Storage;
|
||||||
import io.atomix.copycat.server.storage.StorageLevel;
|
import io.atomix.copycat.server.storage.StorageLevel;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class BootstrapingCluster {
|
public class BootstrapingCluster {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
Storage storage = Storage.builder()
|
Storage storage = Storage.builder()
|
||||||
.withDirectory(new File("log"))
|
.withDirectory(new File("log"))
|
||||||
.withStorageLevel(StorageLevel.DISK)
|
.withStorageLevel(StorageLevel.DISK)
|
||||||
@ -25,6 +23,5 @@ public class BootstrapingCluster {
|
|||||||
|
|
||||||
CompletableFuture<AtomixReplica> completableFuture = replica.bootstrap();
|
CompletableFuture<AtomixReplica> completableFuture = replica.bootstrap();
|
||||||
completableFuture.join();
|
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,24 +1,24 @@
|
|||||||
package com.atomix.example;
|
package com.atomix.example;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import io.atomix.AtomixReplica;
|
import io.atomix.AtomixReplica;
|
||||||
import io.atomix.catalyst.transport.Address;
|
import io.atomix.catalyst.transport.Address;
|
||||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||||
import io.atomix.collections.DistributedMap;
|
|
||||||
import io.atomix.concurrent.DistributedLock;
|
import io.atomix.concurrent.DistributedLock;
|
||||||
import io.atomix.copycat.server.storage.Storage;
|
import io.atomix.copycat.server.storage.Storage;
|
||||||
import io.atomix.copycat.server.storage.StorageLevel;
|
import io.atomix.copycat.server.storage.StorageLevel;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class OtherNodes {
|
public class OtherNodes {
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
// TODO Auto-generated method stub
|
List<Address> cluster = Arrays
|
||||||
|
.asList(
|
||||||
List<Address> cluster = Arrays.asList(new Address("localhost", 8700), new Address("localhost", 8701), new Address("localhost", 8702));
|
new Address("localhost", 8700),
|
||||||
|
new Address("localhost", 8701),
|
||||||
|
new Address("localhost", 8702));
|
||||||
|
|
||||||
Storage storage = Storage.builder()
|
Storage storage = Storage.builder()
|
||||||
.withDirectory(new File("log"))
|
.withDirectory(new File("log"))
|
||||||
@ -48,19 +48,17 @@ public class OtherNodes {
|
|||||||
lock.lock()
|
lock.lock()
|
||||||
.thenRun(() -> System.out.println("Acquired a lock"));
|
.thenRun(() -> System.out.println("Acquired a lock"));
|
||||||
|
|
||||||
DistributedMap<Object, Object> map = replica2.getMap("map")
|
replica2.getMap("map")
|
||||||
|
.thenCompose(m -> m.put("bar", "Hello world!"))
|
||||||
|
.thenRun(() -> System.out.println("Value is set in Distributed Map"))
|
||||||
.join();
|
.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"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class WorkerThread extends Thread {
|
private static class WorkerThread extends Thread {
|
||||||
AtomixReplica replica;
|
private AtomixReplica replica;
|
||||||
List<Address> cluster;
|
private List<Address> cluster;
|
||||||
|
|
||||||
public WorkerThread(AtomixReplica replica, List<Address> cluster) {
|
WorkerThread(AtomixReplica replica, List<Address> cluster) {
|
||||||
this.replica = replica;
|
this.replica = replica;
|
||||||
this.cluster = cluster;
|
this.cluster = cluster;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.atomix.exampletest;
|
||||||
|
|
||||||
|
import io.atomix.AtomixClient;
|
||||||
|
import io.atomix.catalyst.transport.Address;
|
||||||
|
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class AtomixClientLiveTest {
|
||||||
|
|
||||||
|
private final AtomixClient client = AtomixClient.builder()
|
||||||
|
.withTransport(new NettyTransport())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBootstrap_thenShouldGet() throws InterruptedException, ExecutionException {
|
||||||
|
List<Address> cluster = Arrays.asList(
|
||||||
|
new Address("localhost", 8700),
|
||||||
|
new Address("localhsot", 8701));
|
||||||
|
|
||||||
|
String value = client.connect(cluster)
|
||||||
|
.thenRun(() -> System.out.println("Client Connected"))
|
||||||
|
.thenCompose(c -> client.getMap("map"))
|
||||||
|
.thenCompose(m -> m.get("bar"))
|
||||||
|
.thenApply(a -> (String) a)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
assertEquals("Hello world!", value);
|
||||||
|
}
|
||||||
|
}
|
@ -1,38 +0,0 @@
|
|||||||
package com.atomix.exampletest;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertSame;
|
|
||||||
import io.atomix.AtomixClient;
|
|
||||||
import io.atomix.catalyst.transport.Address;
|
|
||||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
|
||||||
import io.atomix.collections.DistributedMap;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class ClientExampleTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void ExampleTest() 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();
|
|
||||||
|
|
||||||
assertEquals("Hello world!", value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user