private void restoreState() { Command<?> command; if (state.compareAndSet(State.CONNECTING, State.CONNECTED)) { // we are connected so clean up the pending queue while ((command = pending.poll()) != null) { write(command); } // restore the pub/sub subscriptions if (subscriptions != null) { for (String channel : subscriptions.channelNames()) { final List<Object> args = new ArrayList<>(); args.add(channel); write(new Command<>(context, RedisCommand.SUBSCRIBE, args, Charset.forName(config.getEncoding()), ResponseTransform.NONE, JsonArray.class)); } for (String pattern : subscriptions.patternNames()) { final List<Object> args = new ArrayList<>(); args.add(pattern); write(new Command<>(context, RedisCommand.PSUBSCRIBE, args, Charset.forName(config.getEncoding()), ResponseTransform.NONE, JsonArray.class)); } } } }
BaseRedisClient(Vertx vertx, RedisOptions config) { this.eb = vertx.eventBus(); this.encoding = config.getEncoding(); this.charset = Charset.forName(encoding); this.binaryCharset = Charset.forName("iso-8859-1"); this.baseAddress = config.getAddress(); subscriptions = new RedisSubscriptions(vertx); redis = new RedisConnection(vertx, config, null); pubsub = new RedisConnection(vertx, config, subscriptions); }
private void doCheckMaster() { Command<JsonObject> infoCommand = new Command<>(context, RedisCommand.INFO, Collections.emptyList(), Charset.forName(config.getEncoding()), ResponseTransform.INFO, JsonObject.class).handler(info -> { if (info.failed()) { // clean up any waiting command clearQueue(pending, info.cause()); netSocket.close(); } else { if (!"master".equals(info.result().getJsonObject("replication").getString("role"))) { clearQueue(pending, info.cause()); netSocket.close(); log.error("Forced disconnect of non-master"); } else { log.info("Verification of master role succeeded"); } } }); // write to the socket in the netSocket context // queue it write(infoCommand); }
/** * Once a socket connection is established one needs to authenticate if there is a password */ private void doAuth() { if (config.getAuth() != null) { // we need to authenticate first final List<Object> args = new ArrayList<>(); args.add(config.getAuth()); Command<String> authCmd = new Command<>(context, RedisCommand.AUTH, args, Charset.forName(config.getEncoding()), ResponseTransform.NONE, String.class).handler(auth -> { if (auth.failed()) { // clean up any waiting command clearQueue(pending, auth.cause()); netSocket.close(); } else { // auth success, proceed with select doSelect(); } }); // write to the socket in the netSocket context // queue it write(authCmd); } else { // no auth, proceed with select doSelect(); } }
json.put("domainSocketAddress", obj.getDomainSocketAddress()); if (obj.getEncoding() != null) { json.put("encoding", obj.getEncoding());
private void doSelect() { // optionally there could be a select command if (config.getSelect() != null) { final List<Object> args = new ArrayList<>(); args.add(config.getSelect()); Command<String> selectCmd = new Command<>(context, RedisCommand.SELECT, args, Charset.forName(config.getEncoding()), ResponseTransform.NONE, String.class).handler(select -> { if (select.failed()) { // clean up any waiting command clearQueue(pending, select.cause()); netSocket.close(); } else { // select success, proceed with resendof pending messages/resubscribe pub/sub restoreState(); } }); // write to the socket in the netSocket context // queue it write(selectCmd); } else { // no select, proceed with resendof pending messages/resubscribe pub/sub restoreState(); } }