Refactor Atomix samples (#2696)
This commit is contained in:
parent
3220913767
commit
7cb94f676c
@ -1,41 +1,46 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.atomix.io</groupId>
|
<groupId>com.atomix.io</groupId>
|
||||||
<artifactId>atomix</artifactId>
|
<artifactId>atomix</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<dependencies>
|
<parent>
|
||||||
<dependency>
|
<groupId>com.baeldung</groupId>
|
||||||
<groupId>io.atomix</groupId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<artifactId>atomix-all</artifactId>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<version>1.0.0-rc9</version>
|
</parent>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependencies>
|
||||||
<groupId>junit</groupId>
|
<dependency>
|
||||||
<artifactId>junit</artifactId>
|
<groupId>io.atomix</groupId>
|
||||||
<version>4.9</version>
|
<artifactId>atomix-all</artifactId>
|
||||||
<scope>test</scope>
|
<version>1.0.0-rc9</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>1.2.17</version>
|
<version>4.9</version>
|
||||||
</dependency>
|
<scope>test</scope>
|
||||||
</dependencies>
|
</dependency>
|
||||||
<build>
|
<dependency>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
<groupId>log4j</groupId>
|
||||||
<plugins>
|
<artifactId>log4j</artifactId>
|
||||||
<plugin>
|
<version>1.2.17</version>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
</dependency>
|
||||||
<version>3.7.0</version>
|
</dependencies>
|
||||||
<configuration>
|
<build>
|
||||||
<source>1.8</source>
|
<plugins>
|
||||||
<target>1.8</target>
|
<plugin>
|
||||||
</configuration>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
</plugin>
|
<version>3.7.0</version>
|
||||||
</plugins>
|
<configuration>
|
||||||
</build>
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,30 +1,27 @@
|
|||||||
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)
|
||||||
.build();
|
.build();
|
||||||
AtomixReplica replica = AtomixReplica.builder(new Address("localhost", 8700))
|
AtomixReplica replica = AtomixReplica.builder(new Address("localhost", 8700))
|
||||||
.withStorage(storage)
|
.withStorage(storage)
|
||||||
.withTransport(new NettyTransport())
|
.withTransport(new NettyTransport())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
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,42 +1,42 @@
|
|||||||
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"))
|
||||||
.withStorageLevel(StorageLevel.DISK)
|
.withStorageLevel(StorageLevel.DISK)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
AtomixReplica replica2 = AtomixReplica.builder(new Address("localhost", 8701))
|
AtomixReplica replica2 = AtomixReplica.builder(new Address("localhost", 8701))
|
||||||
.withStorage(storage)
|
.withStorage(storage)
|
||||||
.withTransport(new NettyTransport())
|
.withTransport(new NettyTransport())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
WorkerThread WT1 = new WorkerThread(replica2, cluster);
|
WorkerThread WT1 = new WorkerThread(replica2, cluster);
|
||||||
WT1.run();
|
WT1.run();
|
||||||
|
|
||||||
AtomixReplica replica3 = AtomixReplica.builder(new Address("localhost", 8702))
|
AtomixReplica replica3 = AtomixReplica.builder(new Address("localhost", 8702))
|
||||||
.withStorage(storage)
|
.withStorage(storage)
|
||||||
.withTransport(new NettyTransport())
|
.withTransport(new NettyTransport())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
WorkerThread WT2 = new WorkerThread(replica3, cluster);
|
WorkerThread WT2 = new WorkerThread(replica3, cluster);
|
||||||
WT2.run();
|
WT2.run();
|
||||||
@ -44,30 +44,28 @@ public class OtherNodes {
|
|||||||
Thread.sleep(6000);
|
Thread.sleep(6000);
|
||||||
|
|
||||||
DistributedLock lock = replica2.getLock("my-lock")
|
DistributedLock lock = replica2.getLock("my-lock")
|
||||||
.join();
|
.join();
|
||||||
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")
|
||||||
.join();
|
.thenCompose(m -> m.put("bar", "Hello world!"))
|
||||||
|
.thenRun(() -> System.out.println("Value is set in Distributed Map"))
|
||||||
// Put a value in the map and call the completion callback on response
|
.join();
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
replica.join(cluster)
|
replica.join(cluster)
|
||||||
.join();
|
.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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