Atomix Intro Project (#2675)

* This is whole project

* BeanInjectionTest.java class is added for Junit Test.

* Atomix Intro Project

* Update pom.xml : Module name changed to Atomix

* Intro to Atomix:
1. Module name is changed.
2. Module name is updated in pom.xml.

* maven-compiler-plugin version number is updated.
This commit is contained in:
abirkhan04 2017-09-30 21:09:04 +06:00 committed by Grzegorz Piwowarek
parent 77aa65b2e9
commit cfb952f849
6 changed files with 215 additions and 0 deletions

1
atomix/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

41
atomix/pom.xml Normal file
View File

@ -0,0 +1,41 @@
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atomix.io</groupId>
<artifactId>atomix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.atomix</groupId>
<artifactId>atomix-all</artifactId>
<version>1.0.0-rc9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,30 @@
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;
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();
AtomixReplica replica = AtomixReplica.builder(new Address("localhost", 8700))
.withStorage(storage)
.withTransport(new NettyTransport())
.build();
CompletableFuture<AtomixReplica> completableFuture = replica.bootstrap();
completableFuture.join();
}
}

View File

@ -0,0 +1,32 @@
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);
}
}

View File

@ -0,0 +1,73 @@
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;
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));
Storage storage = Storage.builder()
.withDirectory(new File("log"))
.withStorageLevel(StorageLevel.DISK)
.build();
AtomixReplica replica2 = AtomixReplica.builder(new Address("localhost", 8701))
.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();
WorkerThread WT2 = new WorkerThread(replica3, cluster);
WT2.run();
Thread.sleep(6000);
DistributedLock lock = replica2.getLock("my-lock")
.join();
lock.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"));
}
private static class WorkerThread extends Thread {
AtomixReplica replica;
List<Address> cluster;
public WorkerThread(AtomixReplica replica, List<Address> cluster) {
this.replica = replica;
this.cluster = cluster;
}
public void run() {
replica.join(cluster)
.join();
}
}
}

View File

@ -0,0 +1,38 @@
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);
}
}