Class UniversalMembershipListenerAdapter
- All Implemented Interfaces:
MembershipListener
The UniversalMembershipListenerAdapter is a wrapper for
MembershipListener and
ClientMembershipListener, providing a facade that makes both appear as a single
MembershipListener . This includes adapting ClientMembershipListener
events to appear as events for the MembershipListener.
UniversalMembershipListenerAdapter implements MembershipListener,
exposing the callback in that interface as methods to be overridden by implementing classes.
An internal implementation of ClientMembershipListener is registered when this class
is instantiated. This implementation creates a
MembershipEvent and calls the corresponding
MembershipListener public methods on
UniversalMembershipListenerAdapter.The ClientMembershipEvents are
wrapped to appear as MembershipEvents. In this way, both types of membership events
appear as MembershipEvents.
Any CacheServer using the UniversalMembershipListenerAdapter will receive
notifications of peer membership changes and client membership changes through a single listener.
Any cache client using the UniversalMembershipListenerAdapter would receive
notifications of cache server connection changes. If that cache client also creates a connection
to the GemFire DistributedSystem, then it will also register
the adapter for membership events. But it wont be an automatic process. User needs to register
the UniversalMembershipListenerAdapter with ManagementService to receive membership events. How
to register UniversalMembershipListenerAdapter with ManagementService is explained below.
Subclasses of UniversalMembershipListenerAdapter may be registered as a
MembershipListener using
ManagementService.addMembershipListener(org.apache.geode.management.membership.MembershipListener) .It is best, however,
to register the listener using registerMembershipListener(org.apache.geode.management.ManagementService) since this allows the adapter
to prevent duplicate events for members that are both a peer member and a client.
Simply constructing the UniversalMembershipListenerAdapter results in the underlying
ClientMembershipListener also being registered.
The following code illustrates how a CacheServer application would use
UniversalMembershipListenerAdapter. The code in this example assumes that the class
MyMembershipListenerImpl extends UniversalMembershipListenerAdapter:
public class MyMembershipListenerImpl extends UniversalMembershipListenerAdapter {
public void memberCrashed(MembershipEvent event) {
// customer code
}
public void memberLeft(MembershipEvent event) {
// customer code
}
public void memberJoined(MembershipEvent event) {
// customer code
}
}
Cache cache = //Get hold of GemFire Cache instance
ManagementService service = ManagementService.getExistingManagementService(cache);
MyMembershipListenerImpl myListener = new MyMembershipListenerImpl();
myListener.registerMembershipListener(service);
The callback on MyMembershipListenerImpl would then be invoked for all
MembershipEvents and ClientMembershipEvents. The latter will appear to
be MembershipEvents.
Similarly, the following code illustrates how a client application would use
UniversalMembershipListenerAdapter, where MyMembershipListenerImpl is a
subclass.Simply by constructing this subclass of UniversalMembershipListenerAdapter
it is registering itself as a ClientMembershipListener:
new MyMembershipListenerImpl();
A client that also connects to the DistributedSystem could register with
theManagementService as shown above.
It is recommended that subclasses register with the ManagementService using
registerMembershipListener(org.apache.geode.management.ManagementService), as this will prevent duplicate events for members that are
both clients and peer members.If duplicate events are acceptable, you may register subclasses
using ManagementService#addMembershipListener.
- Since:
- GemFire 8.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAdapts ClientMembershipEvent to look like a MembershipEvent -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault number of historical events to track in order to avoid duplicate events for members that are both clients and peer members; value is 100.protected final MembershipListener -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an instance of UniversalMembershipListenerAdapter.UniversalMembershipListenerAdapter(int historySize) Constructs an instance of UniversalMembershipListenerAdapter. -
Method Summary
Modifier and TypeMethodDescriptionvoidmemberCrashed(MembershipEvent event) Invoked when a member has unexpectedly left the distributed system.voidmemberJoined(MembershipEvent event) Invoked when a member has joined the distributed system.voidmemberLeft(MembershipEvent event) Invoked when a member has gracefully left the distributed system.voidRegisters this adapter as aClientMembershipListener.voidRegisters this adapter with theManagementService.voidUnregisters this adapter as aClientMembershipListener.voidUnregisters this adapter with theManagementService.
-
Field Details
-
DEFAULT_HISTORY_SIZE
public static final int DEFAULT_HISTORY_SIZEDefault number of historical events to track in order to avoid duplicate events for members that are both clients and peer members; value is 100.- See Also:
-
membershipListener
-
-
Constructor Details
-
UniversalMembershipListenerAdapter
public UniversalMembershipListenerAdapter()Constructs an instance of UniversalMembershipListenerAdapter. -
UniversalMembershipListenerAdapter
public UniversalMembershipListenerAdapter(int historySize) Constructs an instance of UniversalMembershipListenerAdapter.- Parameters:
historySize- number of historical events to track in order to avoid duplicate events for members that are both client and peer members; must a number between 10 andInteger.MAX_INT- Throws:
IllegalArgumentException- if historySize is less than 10
-
-
Method Details
-
registerMembershipListener
Registers this adapter with theManagementService. Registering in this way allows the adapter to ensure that callback will not be invoked twice for members that have a client connection and a peer connection. If you register withManagementService.addMembershipListener(org.apache.geode.management.membership.MembershipListener)then duplicate events may occur for members that are both client and peer.- Parameters:
service- theManagementServicewith which to register this adapter
-
unregisterMembershipListener
Unregisters this adapter with theManagementService. If registration is performed withregisterMembershipListener(org.apache.geode.management.ManagementService)then this method must be used to successfully unregister the adapter.- Parameters:
service- theManagementServicewith which to unregister this adapter
-
registerClientMembershipListener
public void registerClientMembershipListener()Registers this adapter as aClientMembershipListener. Registration is automatic when constructing this adapter, so this call is not necessary unless it was previously unregistered by callingunregisterClientMembershipListener(). -
unregisterClientMembershipListener
public void unregisterClientMembershipListener()Unregisters this adapter as aClientMembershipListener.- See Also:
-
memberJoined
Invoked when a member has joined the distributed system. Also invoked when a client has connected to this process or when this process has connected to aCacheServer.- Specified by:
memberJoinedin interfaceMembershipListener- Parameters:
event- the triggering event
-
memberLeft
Invoked when a member has gracefully left the distributed system. Also invoked when a client has gracefully disconnected from this process. or when this process has gracefully disconnected from aCacheServer.- Specified by:
memberLeftin interfaceMembershipListener- Parameters:
event- the triggering event
-
memberCrashed
Invoked when a member has unexpectedly left the distributed system. Also invoked when a client has unexpectedly disconnected from this process or when this process has unexpectedly disconnected from aCacheServer.- Specified by:
memberCrashedin interfaceMembershipListener- Parameters:
event- the triggering event
-