Search in sources :

Example 1 with Cluster

use of io.scalecube.cluster.Cluster in project scalecube by scalecube.

the class ClusterMetadataExample method main.

/**
 * Main method.
 */
public static void main(String[] args) throws Exception {
    // Start seed cluster member Alice
    Cluster alice = Cluster.joinAwait();
    // Join Joe to cluster with metadata
    Map<String, String> metadata = ImmutableMap.of("name", "Joe");
    Cluster joe = Cluster.joinAwait(metadata, alice.address());
    // Subscribe Joe to listen for incoming messages and print them to system out
    joe.listen().map(Message::data).subscribe(System.out::println);
    // Scan the list of members in the cluster and find Joe there
    Optional<Member> joeMemberOptional = alice.otherMembers().stream().filter(member -> "Joe".equals(member.metadata().get("name"))).findAny();
    // Send hello to Joe
    if (joeMemberOptional.isPresent()) {
        alice.send(joeMemberOptional.get(), Message.fromData("Hello Joe"));
    }
    TimeUnit.SECONDS.sleep(3);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Cluster(io.scalecube.cluster.Cluster) ImmutableMap(com.google.common.collect.ImmutableMap) Member(io.scalecube.cluster.Member) Map(java.util.Map) Optional(java.util.Optional) Message(io.scalecube.transport.Message) Cluster(io.scalecube.cluster.Cluster) Member(io.scalecube.cluster.Member)

Example 2 with Cluster

use of io.scalecube.cluster.Cluster in project scalecube by scalecube.

the class GossipExample method main.

/**
 * Main method.
 */
public static void main(String[] args) throws Exception {
    // Start cluster nodes and subscribe on listening gossips
    Cluster alice = Cluster.joinAwait();
    alice.listenGossips().subscribe(gossip -> System.out.println("Alice heard: " + gossip.data()));
    Cluster bob = Cluster.joinAwait(alice.address());
    bob.listenGossips().subscribe(gossip -> System.out.println("Bob heard: " + gossip.data()));
    Cluster carol = Cluster.joinAwait(alice.address());
    carol.listenGossips().subscribe(gossip -> System.out.println("Carol heard: " + gossip.data()));
    Cluster dan = Cluster.joinAwait(alice.address());
    dan.listenGossips().subscribe(gossip -> System.out.println("Dan heard: " + gossip.data()));
    // Start cluster node Eve that joins cluster and spreads gossip
    Cluster eve = Cluster.joinAwait(alice.address());
    eve.spreadGossip(Message.fromData("Gossip from Eve"));
    // Avoid exit main thread immediately ]:->
    Thread.sleep(1000);
}
Also used : Cluster(io.scalecube.cluster.Cluster)

Example 3 with Cluster

use of io.scalecube.cluster.Cluster in project scalecube by scalecube.

the class MessagingExample method main.

/**
 * Main method.
 */
public static void main(String[] args) throws Exception {
    // Start cluster node Alice to listen and respond for incoming greeting messages
    Cluster alice = Cluster.joinAwait();
    alice.listen().subscribe(msg -> {
        System.out.println("Alice received: " + msg.data());
        alice.send(msg.sender(), Message.fromData("Greetings from Alice"));
    });
    // Join cluster node Bob to cluster with Alice, listen and respond for incoming greeting messages
    Cluster bob = Cluster.joinAwait(alice.address());
    bob.listen().subscribe(msg -> {
        System.out.println("Bob received: " + msg.data());
        bob.send(msg.sender(), Message.fromData("Greetings from Bob"));
    });
    // Join cluster node Carol to cluster with Alice and Bob
    Cluster carol = Cluster.joinAwait(alice.address(), bob.address());
    // Subscribe Carol to listen for incoming messages and print them to system out
    carol.listen().map(msg -> "Carol received: " + msg.data()).subscribe(System.out::println);
    // Send from Carol greeting message to all other cluster members (which is Alice and Bob)
    Message greetingMsg = Message.fromData("Greetings from Carol");
    carol.otherMembers().forEach(member -> carol.send(member, greetingMsg));
    // Avoid exit main thread immediately ]:->
    Thread.sleep(1000);
}
Also used : Cluster(io.scalecube.cluster.Cluster) Message(io.scalecube.transport.Message) Message(io.scalecube.transport.Message) Cluster(io.scalecube.cluster.Cluster)

Example 4 with Cluster

use of io.scalecube.cluster.Cluster in project scalecube by scalecube.

the class ClusterJoinExamples method main.

/**
 * Main method.
 */
public static void main(String[] args) throws Exception {
    // Start seed member Alice
    Cluster alice = Cluster.joinAwait();
    // Join Bob to cluster with Alice
    Cluster bob = Cluster.joinAwait(alice.address());
    // Join Carol to cluster with metadata
    Map<String, String> metadata = ImmutableMap.of("name", "Carol");
    Cluster carol = Cluster.joinAwait(metadata, alice.address());
    // Start Dan on port 3000
    ClusterConfig configWithFixedPort = ClusterConfig.builder().seedMembers(alice.address()).portAutoIncrement(false).port(3000).build();
    Cluster dan = Cluster.joinAwait(configWithFixedPort);
    // Start Eve in separate cluster (separate sync group)
    ClusterConfig configWithSyncGroup = ClusterConfig.builder().seedMembers(alice.address(), bob.address(), carol.address(), // won't join anyway
    dan.address()).syncGroup("another cluster").build();
    Cluster eve = Cluster.joinAwait(configWithSyncGroup);
    // Print cluster members of each node
    System.out.println("Alice (" + alice.address() + ") cluster: " + alice.members().stream().map(Member::toString).collect(joining("\n", "\n", "\n")));
    System.out.println("Bob (" + bob.address() + ") cluster: " + bob.members().stream().map(Member::toString).collect(joining("\n", "\n", "\n")));
    System.out.println("Carol (" + carol.address() + ") cluster: " + carol.members().stream().map(Member::toString).collect(joining("\n", "\n", "\n")));
    System.out.println("Dan (" + dan.address() + ") cluster: " + dan.members().stream().map(Member::toString).collect(joining("\n", "\n", "\n")));
    System.out.println(// alone in cluster
    "Eve (" + eve.address() + ") cluster: " + eve.members().stream().map(Member::toString).collect(joining("\n", "\n", "\n")));
}
Also used : Cluster(io.scalecube.cluster.Cluster) ClusterConfig(io.scalecube.cluster.ClusterConfig)

Example 5 with Cluster

use of io.scalecube.cluster.Cluster in project scalecube by scalecube.

the class MembershipEventsExample method main.

/**
 * Main method.
 */
public static void main(String[] args) throws Exception {
    // Alice init cluster
    Cluster alice = Cluster.joinAwait(ImmutableMap.of("name", "Alice"));
    System.out.println(now() + " Alice join members: " + alice.members());
    alice.listenMembership().subscribe(event -> System.out.println(now() + " Alice received: " + event));
    // Bob join cluster
    Cluster bob = Cluster.joinAwait(ImmutableMap.of("name", "Bob"), alice.address());
    System.out.println(now() + " Bob join members: " + bob.members());
    bob.listenMembership().subscribe(event -> System.out.println(now() + " Bob received: " + event));
    // Carol join cluster
    Cluster carol = Cluster.joinAwait(ImmutableMap.of("name", "Carol"), alice.address(), bob.address());
    System.out.println(now() + " Carol join members: " + carol.members());
    carol.listenMembership().subscribe(event -> System.out.println(now() + " Carol received: " + event));
    // Bob leave cluster
    Future<Void> shutdownFuture = bob.shutdown();
    shutdownFuture.get();
    // Avoid exit main thread immediately ]:->
    long pingInterval = ClusterConfig.DEFAULT_PING_INTERVAL;
    long suspicionTimeout = ClusterMath.suspicionTimeout(ClusterConfig.DEFAULT_SUSPICION_MULT, 4, pingInterval);
    long maxRemoveTimeout = suspicionTimeout + 3 * pingInterval;
    Thread.sleep(maxRemoveTimeout);
}
Also used : Cluster(io.scalecube.cluster.Cluster)

Aggregations

Cluster (io.scalecube.cluster.Cluster)5 Message (io.scalecube.transport.Message)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ClusterConfig (io.scalecube.cluster.ClusterConfig)1 Member (io.scalecube.cluster.Member)1 Map (java.util.Map)1 Optional (java.util.Optional)1 TimeUnit (java.util.concurrent.TimeUnit)1