Search code examples
hazelcast

HazelCast multiCasting works inconsistently


I am using hazelCast to save only an integer value on 2 different java application. I have 2 java application which are running different servers. HazelCast connection is established successfully. When i try to increment the value using following command.

DistributedCounter counter = Hazelcast.getHazelcastInstanceByName("myinstance").getDistributedObject("myservice", "value");
counter.inc();

I am trying to get the value and to see value=10 after 10 increment operation is completed. but sometimes i have seen the value 8 or 9. sometimes it works. inc method is :

NodeEngine nodeEngine = getNodeEngine();
  IncOperation operation = new IncOperation("value", 1);
  int partitionId = nodeEngine.getPartitionService().getPartitionId("value");
  InvocationBuilder builder = nodeEngine.getOperationService().createInvocationBuilder("myservice", operation, partitionId);
  try {
     final Future<Integer> future = builder.invoke();
     return true;
  } 
  catch (Exception e) {
     throw ExceptionUtil.rethrow(e);
  }

HazelCast network config is set as multiCasting. I disabled the tcp/ip.

Can anyone explain why hazelcast works inconsistently? And how can i come over this situation ?


Solution

  • @OkayAtalay, I see that you created your own service. If you look to the example on Hazelcast Doc, http://docs.hazelcast.org/docs/3.10.2/manual/html-single/index.html#implementing-counterproxy, you'll see that inc operation wait for operation complete by calling future.get(). Even if you're just returning true/false, you must still wait for the operation to complete before sending another operation.

    PS: Hazelcast already has AtomicInteger & PN-Counter dist. objects that do what you're doing manually here.