Ask Your Question
4

How can a code be executed exclusively for the initial clustered client in a Hazelcast Client Server system?

asked 2021-08-15 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-03-15 01:00:00 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-15 11:00:00 +0000

Seen: 12 times

Last updated: Mar 15 '23