public class DistributedMembershipGroup extends AbstractResource
The distributed membership group resource facilitates managing group membership within the Atomix cluster.
Each instance of a DistributedMembershipGroup resource represents a single GroupMember.
Members can join() and leave() the group and be notified of other members joining
and leaving the group. Members may leave the group either voluntarily or due to
a failure or other disconnection from the cluster.
To create a membership group resource, use the DistributedMembershipGroup class or constructor:
atomix.create("group", DistributedMembershipGroup.class).thenAccept(group -> {
...
});
Joining the group
When a new instance of the resource is created, it is initialized with an empty members() list and
member() as it is not yet a member of the group. Once the instance has been created, the instance must
join the group via join():
group.join().thenAccept(member -> {
System.out.println("Joined with member ID: " + member.id());
});
Once the group has been joined, the members() list provides an up-to-date view of the group which will
be automatically updated as members join and leave the group. To be explicitly notified when a member joins or
leaves the group, use the onJoin(Consumer) or onLeave(Consumer) event consumers respectively:
group.onJoin(member -> {
System.out.println(member.id() + " joined the group!");
});
Remote execution
Once members of the group, any member can execute immediate callbacks or
schedule delayed callbacks to be run on any other member of the
group. Submitting a Runnable callback to a member will cause it to be serialized and sent to that node
to be executed.
group.onJoin(member -> {
long memberId = member.id();
member.execute((Serializable & Runnable) () -> System.out.println("Executing on " + memberId));
});
| Constructor and Description |
|---|
DistributedMembershipGroup(RaftClient client) |
| Modifier and Type | Method and Description |
|---|---|
CompletableFuture<GroupMember> |
join()
Joins the instance to the membership group.
|
CompletableFuture<Void> |
leave()
Leaves the membership group.
|
GroupMember |
member()
Returns the local group member.
|
GroupMember |
member(long memberId)
Returns a group member by ID.
|
Collection<GroupMember> |
members()
Returns the collection of all members in the group.
|
Listener<GroupMember> |
onJoin(Consumer<GroupMember> listener)
Adds a listener for members joining the group.
|
Listener<GroupMember> |
onLeave(Consumer<GroupMember> listener)
Adds a listener for members leaving the group.
|
DistributedMembershipGroup |
with(Consistency consistency) |
public DistributedMembershipGroup(RaftClient client)
public DistributedMembershipGroup with(Consistency consistency)
with in interface Resourcewith in class AbstractResourcepublic GroupMember member()
The local GroupMember is constructed when this instance joins the group.
The GroupMember.id() is guaranteed to be unique to this instance throughout the lifetime of
this distributed resource.
null if the member has not joined the group.public GroupMember member(long memberId)
memberId - The member ID for which to return a GroupMember.memberId or null if it is not a known member of the group.public Collection<GroupMember> members()
The returned members collection is guaranteed to be representative of the current group state.
public CompletableFuture<GroupMember> join()
When this instance joins the membership group, the membership lists of this and all other instances
in the group are guaranteed to be updated before the CompletableFuture returned by
this method is completed. Once this instance has joined the group, the returned future will be completed
with the GroupMember instance for this member.
This method returns a CompletableFuture which can be used to block until the operation completes
or to be notified in a separate thread once the operation completes. To block until the operation completes,
use the CompletableFuture.join() method to block the calling thread:
group.join().join();
Alternatively, to execute the operation asynchronous and be notified once the lock is acquired in a different
thread, use one of the many completable future callbacks:
group.join().thenAccept(thisMember -> System.out.println("This member is: " + thisMember.id()));
public Listener<GroupMember> onJoin(Consumer<GroupMember> listener)
The provided Consumer will be called each time a member joins the group. Note that
the join consumer will be called before the joining member's join() completes.
The returned Listener can be used to unregister the listener
when its use if finished.
listener - The join listener.public CompletableFuture<Void> leave()
When this instance leaves the membership group, the membership lists of this and all other instances
in the group are guaranteed to be updated before the CompletableFuture returned by
this method is completed. Once this instance has left the group, the returned future will be completed.
This method returns a CompletableFuture which can be used to block until the operation completes
or to be notified in a separate thread once the operation completes. To block until the operation completes,
use the CompletableFuture.join() method to block the calling thread:
group.leave().join();
Alternatively, to execute the operation asynchronous and be notified once the lock is acquired in a different
thread, use one of the many completable future callbacks:
group.leave().thenRun(() -> System.out.println("Left the group!")));
public Listener<GroupMember> onLeave(Consumer<GroupMember> listener)
The provided Consumer will be called each time a member leaves the group. Members can
leave the group either voluntarily or by crashing or otherwise becoming disconnected from the
cluster for longer than their session timeout. Note that the leave consumer will be called before
the leaving member's leave() completes.
The returned Listener can be used to unregister the listener
when its use if finished.
listener - The leave listener.Copyright © 2013–2015. All rights reserved.