1+ package com .atomix .example ;
2+
3+ import io .atomix .AtomixReplica ;
4+ import io .atomix .catalyst .transport .Address ;
5+ import io .atomix .catalyst .transport .netty .NettyTransport ;
6+ import io .atomix .concurrent .DistributedLock ;
7+ import io .atomix .copycat .server .storage .Storage ;
8+ import io .atomix .copycat .server .storage .StorageLevel ;
9+
10+ import java .io .File ;
11+ import java .util .Arrays ;
12+ import java .util .List ;
13+
14+ public class OtherNodes {
15+
16+ public static void main (String [] args ) throws InterruptedException {
17+ List <Address > cluster = Arrays
18+ .asList (
19+ new Address ("localhost" , 8700 ),
20+ new Address ("localhost" , 8701 ),
21+ new Address ("localhost" , 8702 ));
22+
23+ Storage storage = Storage .builder ()
24+ .withDirectory (new File ("log" ))
25+ .withStorageLevel (StorageLevel .DISK )
26+ .build ();
27+
28+ AtomixReplica replica2 = AtomixReplica .builder (new Address ("localhost" , 8701 ))
29+ .withStorage (storage )
30+ .withTransport (new NettyTransport ())
31+ .build ();
32+
33+ WorkerThread WT1 = new WorkerThread (replica2 , cluster );
34+ WT1 .run ();
35+
36+ AtomixReplica replica3 = AtomixReplica .builder (new Address ("localhost" , 8702 ))
37+ .withStorage (storage )
38+ .withTransport (new NettyTransport ())
39+ .build ();
40+
41+ WorkerThread WT2 = new WorkerThread (replica3 , cluster );
42+ WT2 .run ();
43+
44+ Thread .sleep (6000 );
45+
46+ DistributedLock lock = replica2 .getLock ("my-lock" )
47+ .join ();
48+ lock .lock ()
49+ .thenRun (() -> System .out .println ("Acquired a lock" ));
50+
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 ();
55+ }
56+
57+ private static class WorkerThread extends Thread {
58+ private AtomixReplica replica ;
59+ private List <Address > cluster ;
60+
61+ WorkerThread (AtomixReplica replica , List <Address > cluster ) {
62+ this .replica = replica ;
63+ this .cluster = cluster ;
64+ }
65+
66+ public void run () {
67+ replica .join (cluster )
68+ .join ();
69+ }
70+ }
71+ }
0 commit comments