Class KeyValueClient


  • public class KeyValueClient
    extends java.lang.Object
    HTTP Client for /v1/kv/ endpoints.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  KeyValueClient.Api
      Retrofit API interface.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Http http  
      static int NOT_FOUND_404  
    • Method Summary

      Modifier and Type Method Description
      boolean acquireLock​(java.lang.String key, java.lang.String session)
      Aquire a lock for a given key.
      boolean acquireLock​(java.lang.String key, java.lang.String value, java.lang.String session)
      Aquire a lock for a given key.
      void deleteKey​(java.lang.String key)
      Deletes a specified key.
      void deleteKey​(java.lang.String key, DeleteOptions deleteOptions)
      Deletes a specified key.
      void deleteKeys​(java.lang.String key)
      Deletes a specified key and any below it.
      ClientConfig getConfig()  
      java.util.Optional<ConsulResponse<Value>> getConsulResponseWithValue​(java.lang.String key)
      Retrieves a ConsulResponse with the Value for a spefici key from the key/value store
      java.util.Optional<ConsulResponse<Value>> getConsulResponseWithValue​(java.lang.String key, QueryOptions queryOptions)
      Returns a ConsulResponse for a specific key from the kv store.
      ConsulResponse<java.util.List<Value>> getConsulResponseWithValues​(java.lang.String key)
      Retrieves a ConsulResponse with a list of Value objects along with consul response headers for a specific key from the key/value store.
      ConsulResponse<java.util.List<Value>> getConsulResponseWithValues​(java.lang.String key, QueryOptions queryOptions)
      Retrieves a ConsulResponse with a list of Value objects along with consul response headers for a specific key from the key/value store.
      ClientEventHandler getEventHandler()  
      java.util.List<java.lang.String> getKeys​(java.lang.String key)
      Retrieves a list of matching keys for the given key.
      java.util.List<java.lang.String> getKeys​(java.lang.String key, QueryOptions queryOptions)
      Retrieves a list of matching keys for the given key.
      java.util.Optional<java.lang.String> getSession​(java.lang.String key)
      Retrieves a session string for a specific key from the key/value store.
      java.util.Optional<Value> getValue​(java.lang.String key)
      Retrieves a Value for a specific key from the key/value store.
      java.util.Optional<Value> getValue​(java.lang.String key, QueryOptions queryOptions)
      Retrieves a Value for a specific key from the key/value store.
      void getValue​(java.lang.String key, QueryOptions queryOptions, ConsulResponseCallback<java.util.Optional<Value>> callback)
      Asynchronously retrieves a Value for a specific key from the key/value store.
      java.util.Optional<java.lang.String> getValueAsString​(java.lang.String key)
      Retrieves a string value for a specific key from the key/value store.
      java.util.Optional<java.lang.String> getValueAsString​(java.lang.String key, java.nio.charset.Charset charset)
      Retrieves a string value for a specific key from the key/value store.
      java.util.List<Value> getValues​(java.lang.String key)
      Retrieves a list of Value objects for a specific key from the key/value store.
      java.util.List<Value> getValues​(java.lang.String key, QueryOptions queryOptions)
      Retrieves a list of Value objects for a specific key from the key/value store.
      void getValues​(java.lang.String key, QueryOptions queryOptions, ConsulResponseCallback<java.util.List<Value>> callback)
      Asynchronously retrieves a list of Value objects for a specific key from the key/value store.
      java.util.List<java.lang.String> getValuesAsString​(java.lang.String key)
      Retrieves a list of string values for a specific key from the key/value store.
      java.util.List<java.lang.String> getValuesAsString​(java.lang.String key, java.nio.charset.Charset charset)
      Retrieves a list of string values for a specific key from the key/value store.
      ConsulResponse<TxResponse> performTransaction​(Operation... operations)
      Performs a Consul transaction.
      ConsulResponse<TxResponse> performTransaction​(ConsistencyMode consistency, Operation... operations)
      ConsulResponse<TxResponse> performTransaction​(TransactionOptions transactionOptions, Operation... operations)
      Performs a Consul transaction.
      boolean putValue​(java.lang.String key)
      Puts a null value into the key/value store.
      boolean putValue​(java.lang.String key, byte[] value, long flags, PutOptions putOptions)
      Puts a value into the key/value store.
      boolean putValue​(java.lang.String key, java.lang.String value)
      Puts a value into the key/value store.
      boolean putValue​(java.lang.String key, java.lang.String value, long flags)
      Puts a value into the key/value store.
      boolean putValue​(java.lang.String key, java.lang.String value, long flags, PutOptions putOptions)
      Puts a value into the key/value store.
      boolean putValue​(java.lang.String key, java.lang.String value, long flags, PutOptions putOptions, java.nio.charset.Charset charset)
      Puts a value into the key/value store.
      boolean putValue​(java.lang.String key, java.lang.String value, long flags, java.nio.charset.Charset charset)
      Puts a value into the key/value store.
      boolean putValue​(java.lang.String key, java.lang.String value, java.nio.charset.Charset charset)
      Puts a value into the key/value store.
      boolean releaseLock​(java.lang.String key, java.lang.String sessionId)
      Releases the lock for a given service and session.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getValue

        public java.util.Optional<Value> getValue​(java.lang.String key)
        Retrieves a Value for a specific key from the key/value store. GET /v1/kv/{key}
        Parameters:
        key - The key to retrieve.
        Returns:
        An Optional containing the value or {@link Optional#empty()()}
      • getConsulResponseWithValue

        public java.util.Optional<ConsulResponse<Value>> getConsulResponseWithValue​(java.lang.String key)
        Retrieves a ConsulResponse with the Value for a spefici key from the key/value store
        Parameters:
        key - The key to retrieve
        Returns:
        An Optional containing the ConsulResponse or {@link Optional#empty()()}
      • getValue

        public java.util.Optional<Value> getValue​(java.lang.String key,
                                                  QueryOptions queryOptions)
        Retrieves a Value for a specific key from the key/value store. GET /v1/kv/{key}
        Parameters:
        key - The key to retrieve.
        queryOptions - The query options.
        Returns:
        An Optional containing the value or {@link Optional#empty()()}
      • getConsulResponseWithValue

        public java.util.Optional<ConsulResponse<Value>> getConsulResponseWithValue​(java.lang.String key,
                                                                                    QueryOptions queryOptions)
        Returns a ConsulResponse for a specific key from the kv store. Contains the consul response headers along with the configuration value. GET /v1/kv/{key}
        Parameters:
        key - The key to retrieve.
        queryOptions - The query options.
        Returns:
        An Optional containing the ConsulResponse or Optional.empty()
      • getValue

        public void getValue​(java.lang.String key,
                             QueryOptions queryOptions,
                             ConsulResponseCallback<java.util.Optional<Value>> callback)
        Asynchronously retrieves a Value for a specific key from the key/value store. GET /v1/kv/{key}
        Parameters:
        key - The key to retrieve.
        queryOptions - The query options.
        callback - Callback implemented by callee to handle results.
      • getValues

        public java.util.List<Value> getValues​(java.lang.String key)
        Retrieves a list of Value objects for a specific key from the key/value store. GET /v1/kv/{key}?recurse
        Parameters:
        key - The key to retrieve.
        Returns:
        A list of zero to many Value objects.
      • getConsulResponseWithValues

        public ConsulResponse<java.util.List<Value>> getConsulResponseWithValues​(java.lang.String key)
        Retrieves a ConsulResponse with a list of Value objects along with consul response headers for a specific key from the key/value store. GET /v1/kv/{key}?recurse
        Parameters:
        key - The key to retrieve.
        Returns:
        A ConsulResponse with a list of zero to many Value objects and consul response headers.
      • getValues

        public java.util.List<Value> getValues​(java.lang.String key,
                                               QueryOptions queryOptions)
        Retrieves a list of Value objects for a specific key from the key/value store. GET /v1/kv/{key}?recurse
        Parameters:
        key - The key to retrieve.
        queryOptions - The query options.
        Returns:
        A list of zero to many Value objects.
      • getConsulResponseWithValues

        public ConsulResponse<java.util.List<Value>> getConsulResponseWithValues​(java.lang.String key,
                                                                                 QueryOptions queryOptions)
        Retrieves a ConsulResponse with a list of Value objects along with consul response headers for a specific key from the key/value store. GET /v1/kv/{key}?recurse
        Parameters:
        key - The key to retrieve.
        queryOptions - The query options to use.
        Returns:
        A ConsulResponse with a list of zero to many Value objects and consul response headers.
      • getValues

        public void getValues​(java.lang.String key,
                              QueryOptions queryOptions,
                              ConsulResponseCallback<java.util.List<Value>> callback)
        Asynchronously retrieves a list of Value objects for a specific key from the key/value store. GET /v1/kv/{key}?recurse
        Parameters:
        key - The key to retrieve.
        queryOptions - The query options.
        callback - Callback implemented by callee to handle results.
      • getValueAsString

        public java.util.Optional<java.lang.String> getValueAsString​(java.lang.String key)
        Retrieves a string value for a specific key from the key/value store. GET /v1/kv/{key}
        Parameters:
        key - The key to retrieve.
        Returns:
        An Optional containing the value as a string or Optional.empty()
      • getValueAsString

        public java.util.Optional<java.lang.String> getValueAsString​(java.lang.String key,
                                                                     java.nio.charset.Charset charset)
        Retrieves a string value for a specific key from the key/value store. GET /v1/kv/{key}
        Parameters:
        key - The key to retrieve.
        charset - The charset of the value
        Returns:
        An Optional containing the value as a string or Optional.empty()
      • getValuesAsString

        public java.util.List<java.lang.String> getValuesAsString​(java.lang.String key)
        Retrieves a list of string values for a specific key from the key/value store. GET /v1/kv/{key}?recurse
        Parameters:
        key - The key to retrieve.
        Returns:
        A list of zero to many string values.
      • getValuesAsString

        public java.util.List<java.lang.String> getValuesAsString​(java.lang.String key,
                                                                  java.nio.charset.Charset charset)
        Retrieves a list of string values for a specific key from the key/value store. GET /v1/kv/{key}?recurse
        Parameters:
        key - The key to retrieve.
        charset - The charset of the value
        Returns:
        A list of zero to many string values.
      • putValue

        public boolean putValue​(java.lang.String key)
        Puts a null value into the key/value store.
        Parameters:
        key - The key to use as index.
        Returns:
        true if the value was successfully indexed.
      • putValue

        public boolean putValue​(java.lang.String key,
                                java.lang.String value)
        Puts a value into the key/value store.
        Parameters:
        key - The key to use as index.
        value - The value to index.
        Returns:
        true if the value was successfully indexed.
      • putValue

        public boolean putValue​(java.lang.String key,
                                java.lang.String value,
                                java.nio.charset.Charset charset)
        Puts a value into the key/value store.
        Parameters:
        key - The key to use as index.
        value - The value to index.
        Returns:
        true if the value was successfully indexed.
      • putValue

        public boolean putValue​(java.lang.String key,
                                java.lang.String value,
                                long flags)
        Puts a value into the key/value store.
        Parameters:
        key - The key to use as index.
        value - The value to index.
        flags - The flags for this key.
        Returns:
        true if the value was successfully indexed.
      • putValue

        public boolean putValue​(java.lang.String key,
                                java.lang.String value,
                                long flags,
                                java.nio.charset.Charset charset)
        Puts a value into the key/value store.
        Parameters:
        key - The key to use as index.
        value - The value to index.
        flags - The flags for this key.
        Returns:
        true if the value was successfully indexed.
      • putValue

        public boolean putValue​(java.lang.String key,
                                java.lang.String value,
                                long flags,
                                PutOptions putOptions)
        Puts a value into the key/value store.
        Parameters:
        key - The key to use as index.
        value - The value to index.
        putOptions - PUT options (e.g. wait, acquire).
        Returns:
        true if the value was successfully indexed.
      • putValue

        public boolean putValue​(java.lang.String key,
                                java.lang.String value,
                                long flags,
                                PutOptions putOptions,
                                java.nio.charset.Charset charset)
        Puts a value into the key/value store.
        Parameters:
        key - The key to use as index.
        value - The value to index.
        putOptions - PUT options (e.g. wait, acquire).
        Returns:
        true if the value was successfully indexed.
      • putValue

        public boolean putValue​(java.lang.String key,
                                byte[] value,
                                long flags,
                                PutOptions putOptions)
        Puts a value into the key/value store.
        Parameters:
        key - The key to use as index.
        value - The value to index.
        putOptions - PUT options (e.g. wait, acquire).
        Returns:
        true if the value was successfully indexed.
      • getKeys

        public java.util.List<java.lang.String> getKeys​(java.lang.String key)
        Retrieves a list of matching keys for the given key. GET /v1/kv/{key}?keys
        Parameters:
        key - The key to retrieve.
        Returns:
        A list of zero to many keys.
      • getKeys

        public java.util.List<java.lang.String> getKeys​(java.lang.String key,
                                                        QueryOptions queryOptions)
        Retrieves a list of matching keys for the given key. GET /v1/kv/{key}?keys
        Parameters:
        key - The key to retrieve.
        queryOptions - The query options.
        Returns:
        A list of zero to many keys.
      • deleteKey

        public void deleteKey​(java.lang.String key)
        Deletes a specified key. DELETE /v1/kv/{key}
        Parameters:
        key - The key to delete.
      • deleteKeys

        public void deleteKeys​(java.lang.String key)
        Deletes a specified key and any below it. DELETE /v1/kv/{key}?recurse
        Parameters:
        key - The key to delete.
      • deleteKey

        public void deleteKey​(java.lang.String key,
                              DeleteOptions deleteOptions)
        Deletes a specified key. DELETE /v1/kv/{key}
        Parameters:
        key - The key to delete.
        deleteOptions - DELETE options (e.g. recurse, cas)
      • acquireLock

        public boolean acquireLock​(java.lang.String key,
                                   java.lang.String session)
        Aquire a lock for a given key. PUT /v1/kv/{key}?acquire={session}
        Parameters:
        key - The key to acquire the lock.
        session - The session to acquire lock.
        Returns:
        true if the lock is acquired successfully, false otherwise.
      • acquireLock

        public boolean acquireLock​(java.lang.String key,
                                   java.lang.String value,
                                   java.lang.String session)
        Aquire a lock for a given key. PUT /v1/kv/{key}?acquire={session}
        Parameters:
        key - The key to acquire the lock.
        session - The session to acquire lock.
        value - key value (usually - application specific info about the lock requester)
        Returns:
        true if the lock is acquired successfully, false otherwise.
      • getSession

        public java.util.Optional<java.lang.String> getSession​(java.lang.String key)
        Retrieves a session string for a specific key from the key/value store. GET /v1/kv/{key}
        Parameters:
        key - The key to retrieve.
        Returns:
        An Optional containing the value as a string or Optional.empty()
      • releaseLock

        public boolean releaseLock​(java.lang.String key,
                                   java.lang.String sessionId)
        Releases the lock for a given service and session. GET /v1/kv/{key}?release={sessionId}
        Parameters:
        key - identifying the service.
        sessionId -
        Returns:
        SessionInfo.
      • performTransaction

        public ConsulResponse<TxResponse> performTransaction​(Operation... operations)
        Performs a Consul transaction. PUT /v1/tx
        Parameters:
        operations - A list of KV operations.
        Returns:
        A ConsulResponse containing results and potential errors.
      • performTransaction

        public ConsulResponse<TxResponse> performTransaction​(TransactionOptions transactionOptions,
                                                             Operation... operations)
        Performs a Consul transaction. PUT /v1/tx
        Parameters:
        transactionOptions - transaction options (e.g. dc, consistency).
        operations - A list of KV operations.
        Returns:
        A ConsulResponse containing results and potential errors.