private void doSelectNode(final Message<JsonObject> message) {
    final Object key = message.body().getValue("key");
    if (key == null) {
      message.reply(new JsonObject().putString("status", "error").putString("message", "No key specified."));
    } else {
      context.execute(new Action<String>() {
        @Override
        public String perform() {
          String address = nodeSelectors.get(key);
          if (address != null) {
            return address;
          }
          Collection<String> nodes = groups.get(group);
          int index = new Random().nextInt(nodes.size());
          int i = 0;
          for (String node : nodes) {
            if (i == index) {
              nodeSelectors.put(key, node);
              return node;
            }
            i++;
          }
          return null;
        }
      }, new Handler<AsyncResult<String>>() {
        @Override
        public void handle(AsyncResult<String> result) {
          if (result.failed()) {
            message.reply(new JsonObject().putString("status", "error").putString("message", result.cause().getMessage()));
          } else if (result.result() == null) {
            message.reply(new JsonObject().putString("status", "error").putString("message", "No nodes to select."));
          } else {
            message.reply(new JsonObject().putString("status", "ok").putString("result", result.result()));
          }
        }
      });
    }
  }
private void doSelectNode ( final Message < JsonObject > message ) { final Object key = message . body ( ) . getValue ( "key" ) ; if ( key == null ) { message . reply ( new JsonObject ( ) . putString ( "status" , "error" ) . putString ( "message" , "No key specified." ) ) ; } else { context . execute ( new Action < String > ( ) { @ Override public String perform ( ) { String address = nodeSelectors . get ( key ) ; if ( address != null ) { return address ; } Collection < String > nodes = groups . get ( group ) ; int index = new Random ( ) . nextInt ( nodes . size ( ) ) ; int i = 0 ; for ( String node : nodes ) { if ( i == index ) { nodeSelectors . put ( key , node ) ; return node ; } i ++ ; } return null ; } } , new Handler < AsyncResult < String > > ( ) { @ Override public void handle ( AsyncResult < String > result ) { if ( result . failed ( ) ) { message . reply ( new JsonObject ( ) . putString ( "status" , "error" ) . putString ( "message" , result . cause ( ) . getMessage ( ) ) ) ; } else if ( result . result ( ) == null ) { message . reply ( new JsonObject ( ) . putString ( "status" , "error" ) . putString ( "message" , "No nodes to select." ) ) ; } else { message . reply ( new JsonObject ( ) . putString ( "status" , "ok" ) . putString ( "result" , result . result ( ) ) ) ; } } } ) ; } }
Selects a node in the group.