To have the counter as a functioning distributed object, we need a class. This class (named CounterService in the following sample) will be the gateway between Hazelcast internals and the counter, allowing us to add features to the counter. In the following sample, the class CounterService
is created. Its lifecycle will be managed by Hazelcast.
CounterService
should implement the interface com.hazelcast.spi.ManagedService
as shown below.
import com.hazelcast.spi.ManagedService;
import com.hazelcast.spi.NodeEngine;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class CounterService implements ManagedService {
private NodeEngine nodeEngine;
@Override
public void init( NodeEngine nodeEngine, Properties properties ) {
System.out.println( "CounterService.init" );
this.nodeEngine = nodeEngine;
}
@Override
public void shutdown( boolean terminate ) {
System.out.println( "CounterService.shutdown" );
}
@Override
public void reset() {
}
}
As can be seen from the code, CounterService
implements the following methods.
init
: This is called when CounterService
is initialized. NodeEngine
enables access to Hazelcast internals such as HazelcastInstance
and PartitionService
. Also, the object Properties
will provide us with the ability to create our own properties.shutdown
: This is called when CounterService
is shutdown. It cleans up the resources.reset
: This is called when cluster members are faced with the Split-Brain issue. This occurs when disconnected members that have created their own cluster are merged back into the main cluster. Services can also implement the SplitBrainHandleService
to indicate that they can take part in the merge process. For CounterService
we are going to implement as a no-op.