Class RedisConnection

java.lang.Object
com.codename1.backend.kv.RedisConnection

public final class RedisConnection extends Object

One connection to a Redis server speaking RESP2, the protocol every Redis, Valkey and KeyDB release understands.

RedisKeyValueStore keeps a pool of these and lends one per call; ask it for a RedisKeyValueStore.dedicated() connection for a command that blocks -- XREADGROUP ... BLOCK, BLPOP, SUBSCRIBE -- so it never holds a pooled one.

Replies come back as Java values: a simple string as String, a bulk string as byte[], an integer as Long, an array as Object[], a nil as null. An error reply is thrown as a RedisException, and leaves the connection usable. Any other failure leaves it in an unknown state; the caller closes it.

Not thread safe.

  • Method Details

    • isBroken

      public boolean isBroken()
      Whether a failure other than an error reply happened on this connection, after which it must not be reused.
    • setReadTimeout

      public void setReadTimeout(int millis) throws IOException
      Changes how long a reply may take; zero waits forever.
      Throws:
      IOException
    • call

      public Object call(String... args) throws IOException
      Sends one command and answers its reply.
      Throws:
      IOException
    • call

      public Object call(byte[][] args) throws IOException
      Sends one command whose arguments are already bytes and answers its reply.
      Throws:
      IOException
    • write

      public void write(byte[][] args)
      Stages one command without sending it, for a pipeline: stage several, flush() once, then read() each reply in order.
    • flush

      public void flush() throws IOException
      Sends what write(byte[][]) staged.
      Throws:
      IOException
    • read

      public Object read() throws IOException
      Reads the next reply. An error reply is thrown after it has been read whole, so the connection stays in step.
      Throws:
      IOException
    • close

      public void close()
      Closes the socket. Never throws.