Skip to content

Commit 7cb94f6

Browse files
authored
Refactor Atomix samples (eugenp#2696)
1 parent 3220913 commit 7cb94f6

7 files changed

Lines changed: 114 additions & 148 deletions

File tree

atomix/pom.xml

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.atomix.io</groupId>
5-
<artifactId>atomix</artifactId>
6-
<version>0.0.1-SNAPSHOT</version>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.atomix.io</groupId>
5+
<artifactId>atomix</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
77

8-
<dependencies>
9-
<dependency>
10-
<groupId>io.atomix</groupId>
11-
<artifactId>atomix-all</artifactId>
12-
<version>1.0.0-rc9</version>
13-
</dependency>
14-
<dependency>
15-
<groupId>junit</groupId>
16-
<artifactId>junit</artifactId>
17-
<version>4.9</version>
18-
<scope>test</scope>
19-
</dependency>
20-
<dependency>
21-
<groupId>log4j</groupId>
22-
<artifactId>log4j</artifactId>
23-
<version>1.2.17</version>
24-
</dependency>
25-
</dependencies>
26-
<build>
27-
<sourceDirectory>src</sourceDirectory>
28-
<plugins>
29-
<plugin>
30-
<artifactId>maven-compiler-plugin</artifactId>
31-
<version>3.7.0</version>
32-
<configuration>
33-
<source>1.8</source>
34-
<target>1.8</target>
35-
</configuration>
36-
</plugin>
37-
</plugins>
38-
</build>
8+
<parent>
9+
<groupId>com.baeldung</groupId>
10+
<artifactId>parent-modules</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.atomix</groupId>
17+
<artifactId>atomix-all</artifactId>
18+
<version>1.0.0-rc9</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.9</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>log4j</groupId>
28+
<artifactId>log4j</artifactId>
29+
<version>1.2.17</version>
30+
</dependency>
31+
</dependencies>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.7.0</version>
37+
<configuration>
38+
<source>1.8</source>
39+
<target>1.8</target>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
3944

4045

4146
</project>
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
package com.atomix.example;
22

3-
import java.io.File;
4-
import java.util.concurrent.CompletableFuture;
5-
63
import io.atomix.AtomixReplica;
74
import io.atomix.catalyst.transport.Address;
85
import io.atomix.catalyst.transport.netty.NettyTransport;
96
import io.atomix.copycat.server.storage.Storage;
107
import io.atomix.copycat.server.storage.StorageLevel;
118

9+
import java.io.File;
10+
import java.util.concurrent.CompletableFuture;
11+
1212
public class BootstrapingCluster {
1313

1414
public static void main(String[] args) {
15-
// TODO Auto-generated method stub
16-
1715
Storage storage = Storage.builder()
18-
.withDirectory(new File("log"))
19-
.withStorageLevel(StorageLevel.DISK)
20-
.build();
16+
.withDirectory(new File("log"))
17+
.withStorageLevel(StorageLevel.DISK)
18+
.build();
2119
AtomixReplica replica = AtomixReplica.builder(new Address("localhost", 8700))
22-
.withStorage(storage)
23-
.withTransport(new NettyTransport())
24-
.build();
20+
.withStorage(storage)
21+
.withTransport(new NettyTransport())
22+
.build();
2523

2624
CompletableFuture<AtomixReplica> completableFuture = replica.bootstrap();
2725
completableFuture.join();
28-
2926
}
3027
}

atomix/src/main/java/com/atomix/example/ClientExample.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,71 @@
11
package com.atomix.example;
22

3-
import java.io.File;
4-
5-
import java.util.Arrays;
6-
import java.util.List;
7-
83
import io.atomix.AtomixReplica;
94
import io.atomix.catalyst.transport.Address;
105
import io.atomix.catalyst.transport.netty.NettyTransport;
11-
import io.atomix.collections.DistributedMap;
126
import io.atomix.concurrent.DistributedLock;
137
import io.atomix.copycat.server.storage.Storage;
148
import io.atomix.copycat.server.storage.StorageLevel;
159

10+
import java.io.File;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
1614
public class OtherNodes {
1715

1816
public static void main(String[] args) throws InterruptedException {
19-
// TODO Auto-generated method stub
20-
21-
List<Address> cluster = Arrays.asList(new Address("localhost", 8700), new Address("localhost", 8701), new Address("localhost", 8702));
17+
List<Address> cluster = Arrays
18+
.asList(
19+
new Address("localhost", 8700),
20+
new Address("localhost", 8701),
21+
new Address("localhost", 8702));
2222

2323
Storage storage = Storage.builder()
24-
.withDirectory(new File("log"))
25-
.withStorageLevel(StorageLevel.DISK)
26-
.build();
24+
.withDirectory(new File("log"))
25+
.withStorageLevel(StorageLevel.DISK)
26+
.build();
2727

2828
AtomixReplica replica2 = AtomixReplica.builder(new Address("localhost", 8701))
29-
.withStorage(storage)
30-
.withTransport(new NettyTransport())
31-
.build();
29+
.withStorage(storage)
30+
.withTransport(new NettyTransport())
31+
.build();
3232

3333
WorkerThread WT1 = new WorkerThread(replica2, cluster);
3434
WT1.run();
3535

3636
AtomixReplica replica3 = AtomixReplica.builder(new Address("localhost", 8702))
37-
.withStorage(storage)
38-
.withTransport(new NettyTransport())
39-
.build();
37+
.withStorage(storage)
38+
.withTransport(new NettyTransport())
39+
.build();
4040

4141
WorkerThread WT2 = new WorkerThread(replica3, cluster);
4242
WT2.run();
4343

4444
Thread.sleep(6000);
4545

4646
DistributedLock lock = replica2.getLock("my-lock")
47-
.join();
47+
.join();
4848
lock.lock()
49-
.thenRun(() -> System.out.println("Acquired a lock"));
50-
51-
DistributedMap<Object, Object> map = replica2.getMap("map")
52-
.join();
49+
.thenRun(() -> System.out.println("Acquired a lock"));
5350

54-
// Put a value in the map and call the completion callback on response
55-
map.put("bar", "Hello world!")
56-
.thenRun(() -> System.out.println("Value is set in Distributed Map"));
51+
replica2.getMap("map")
52+
.thenCompose(m -> m.put("bar", "Hello world!"))
53+
.thenRun(() -> System.out.println("Value is set in Distributed Map"))
54+
.join();
5755
}
5856

5957
private static class WorkerThread extends Thread {
60-
AtomixReplica replica;
61-
List<Address> cluster;
58+
private AtomixReplica replica;
59+
private List<Address> cluster;
6260

63-
public WorkerThread(AtomixReplica replica, List<Address> cluster) {
61+
WorkerThread(AtomixReplica replica, List<Address> cluster) {
6462
this.replica = replica;
6563
this.cluster = cluster;
6664
}
6765

6866
public void run() {
6967
replica.join(cluster)
70-
.join();
68+
.join();
7169
}
7270
}
7371
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.atomix.exampletest;
2+
3+
import io.atomix.AtomixClient;
4+
import io.atomix.catalyst.transport.Address;
5+
import io.atomix.catalyst.transport.netty.NettyTransport;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.concurrent.ExecutionException;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class AtomixClientLiveTest {
15+
16+
private final AtomixClient client = AtomixClient.builder()
17+
.withTransport(new NettyTransport())
18+
.build();
19+
20+
@Test
21+
public void whenBootstrap_thenShouldGet() throws InterruptedException, ExecutionException {
22+
List<Address> cluster = Arrays.asList(
23+
new Address("localhost", 8700),
24+
new Address("localhsot", 8701));
25+
26+
String value = client.connect(cluster)
27+
.thenRun(() -> System.out.println("Client Connected"))
28+
.thenCompose(c -> client.getMap("map"))
29+
.thenCompose(m -> m.get("bar"))
30+
.thenApply(a -> (String) a)
31+
.get();
32+
33+
assertEquals("Hello world!", value);
34+
}
35+
}

atomix/src/test/java/com/atomix/exampletest/ClientExampleTest.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
</properties>
2929

3030
<modules>
31+
<module>atomix</module>
3132
<module>apache-cayenne</module>
3233
<module>aws</module>
3334
<module>akka-streams</module>

0 commit comments

Comments
 (0)