io.scalecube.examples.ClusterMetadataExample Maven / Gradle / Ivy
package io.scalecube.examples;
import io.scalecube.cluster.Cluster;
import io.scalecube.cluster.Member;
import io.scalecube.transport.Message;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* Using Cluster metadata: metadata is set of custom parameters that may be used by application
* developers to attach additional business information and identifications to cluster members.
*
* in this example we see how to attach logical name to a cluster member we nick name Joe
*
* @author ronen_h, Anton Kharenko
*/
public class ClusterMetadataExample {
/** 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 metadata = Collections.singletonMap("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 joeMemberOptional =
alice
.otherMembers()
.stream()
.filter(member -> "Joe".equals(member.metadata().get("name")))
.findAny();
// Send hello to Joe
joeMemberOptional.ifPresent(member -> alice.send(member, Message.fromData("Hello Joe")));
TimeUnit.SECONDS.sleep(3);
}
}