1 | initial version |
One way to execute code exclusively for the initial clustered client in a Hazelcast Client Server system is by using the HazelcastClientInitialMembershipListener interface. This interface provides a method called init() which is called only once when the client initially connects to the Hazelcast cluster.
Here is an example code snippet:
public class MyHazelcastClientInitialMembershipListener implements HazelcastClientInitialMembershipListener {
@Override
public void init(InitialMembershipEvent event) {
// Code to be executed exclusively for the initial clustered client
System.out.println("Executing code exclusively for the initial clustered client");
}
@Override
public void memberAdded(MembershipEvent event) {}
@Override
public void memberRemoved(MembershipEvent event) {}
}
To register this listener, you can use the HazelcastClientConfig object as follows:
HazelcastClientConfig config = new HazelcastClientConfig();
config.addListenerConfig(new ListenerConfig(new MyHazelcastClientInitialMembershipListener()));
HazelcastInstance hazelcastClient = HazelcastClient.newHazelcastClient(config);
With this configuration, the init() method of the MyHazelcastClientInitialMembershipListener class will be executed only once for the initial clustered client.