Broadcasting commands

Commands are a special kind of event. They imply that when they are handled, a Result event will be fired containing the outcome of the command. For each command, we have one result per recipient. Each command contains an unique id (unique for all cluster nodes, created from Hazelcast). This id is used to correlate the request with the result. For each result successfully correlated the result is added to list of results on the command object. If the list gets full of if 10 seconds from the command execution have elapsed, the list is moved to a blocking queue from which the result can be retrieved.

The following code snippet shows what happens when a command is sent for execution:

public Map<node,result> execute(Command command) throws Exception {
   if (command == null) {
      throw new Exception("Command store not found");
   } else {
      //store the command to correlate it with the result.
      commandStore.getPending().put(command.getId(), command);
      //I create a timeout task and schedule it
      TimeoutTask timeoutTask = new TimeoutTask(command, commandStore);
      ScheduledFuture timeoutFuture = timeoutScheduler.schedule(timeoutTask, command.getTimeout(), TimeUnit.MILLISECONDS);
   }
   if (producer != null) {
      //send the command to the topic
      producer.produce(command);
      //retrieve the result list from the blocking queue.
      return command.getResult();
   }
   throw new Exception("Command producer not found");
}