Interface PubSubClusterCommands

  • All Known Implementing Classes:
    GlideClusterClient

    public interface PubSubClusterCommands
    Supports commands for the "Pub/Sub" group for a cluster client.
    See Also:
    Pub/Sub Commands
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Set<java.lang.String> ALL_SHARDED_CHANNELS
      Constant representing "unsubscribe from all sharded channels".
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletableFuture<java.lang.String> publish​(GlideString message, GlideString channel, boolean sharded)
      Publishes message on pubsub channel.
      java.util.concurrent.CompletableFuture<java.lang.String> publish​(java.lang.String message, java.lang.String channel, boolean sharded)
      Publishes message on pubsub channel.
      java.util.concurrent.CompletableFuture<java.lang.String[]> pubsubShardChannels()
      Lists the currently active shard channels.
      java.util.concurrent.CompletableFuture<GlideString[]> pubsubShardChannels​(GlideString pattern)
      Lists the currently active shard channels.
      java.util.concurrent.CompletableFuture<java.lang.String[]> pubsubShardChannels​(java.lang.String pattern)
      Lists the currently active shard channels.
      java.util.concurrent.CompletableFuture<GlideString[]> pubsubShardChannelsBinary()
      Lists the currently active shard channels.
      java.util.concurrent.CompletableFuture<java.util.Map<GlideString,​java.lang.Long>> pubsubShardNumSub​(GlideString[] channels)
      Returns the number of subscribers (exclusive of clients subscribed to patterns) for the specified shard channels.
      java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.lang.Long>> pubsubShardNumSub​(java.lang.String[] channels)
      Returns the number of subscribers (exclusive of clients subscribed to patterns) for the specified shard channels.
      java.util.concurrent.CompletableFuture<java.lang.Void> ssubscribe​(java.util.Set<java.lang.String> channels, int timeoutMs)
      Subscribes the client to the specified sharded channels with a timeout.
      java.util.concurrent.CompletableFuture<java.lang.Void> ssubscribeLazy​(java.util.Set<java.lang.String> channels)
      Subscribes the client to the specified sharded channels.
      java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribe​(int timeoutMs)
      Unsubscribes the client from all currently subscribed sharded channels with a timeout.
      java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribe​(java.util.Set<java.lang.String> channels, int timeoutMs)
      Unsubscribes the client from the specified sharded channels with a timeout.
      java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribeLazy()
      Unsubscribes the client from all currently subscribed sharded channels.
      java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribeLazy​(java.util.Set<java.lang.String> channels)
      Unsubscribes the client from the specified sharded channels.
    • Field Detail

      • ALL_SHARDED_CHANNELS

        static final java.util.Set<java.lang.String> ALL_SHARDED_CHANNELS
        Constant representing "unsubscribe from all sharded channels". Pass this to sunsubscribeLazy(Set) or sunsubscribe(Set, int) to unsubscribe from all sharded channels.
        Example:
        
         // Unsubscribe from all sharded channels
         client.sunsubscribeLazy(PubSubClusterCommands.ALL_SHARDED_CHANNELS).get();
         
    • Method Detail

      • publish

        java.util.concurrent.CompletableFuture<java.lang.String> publish​(java.lang.String message,
                                                                         java.lang.String channel,
                                                                         boolean sharded)
        Publishes message on pubsub channel.
        Parameters:
        message - The message to publish.
        channel - The channel to publish the message on.
        sharded - Indicates that this should be run in sharded mode. Setting sharded to true is only applicable with Valkey 7.0+.
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.publish("The cat said 'meow'!", "announcements", true).get();
         assert response.equals("OK");
         
      • publish

        java.util.concurrent.CompletableFuture<java.lang.String> publish​(GlideString message,
                                                                         GlideString channel,
                                                                         boolean sharded)
        Publishes message on pubsub channel.
        Parameters:
        message - The message to publish.
        channel - The channel to publish the message on.
        sharded - Indicates that this should be run in sharded mode. Setting sharded to true is only applicable with Valkey 7.0+.
        Returns:
        OK.
        Since:
        Valkey 7.0 and above.
        See Also:
        valkey.io for details.
        Example:
        
         String response = client.publish(gs("The cat said 'meow'!"), gs("announcements"), true).get();
         assert response.equals("OK");
         
      • pubsubShardChannels

        java.util.concurrent.CompletableFuture<java.lang.String[]> pubsubShardChannels()
        Lists the currently active shard channels.
        Returns:
        An array of all active shard channels.
        See Also:
        valkey.io for details.
        Example:
        
         String[] result = client.pubsubShardChannels().get();
         assert Arrays.equals(result, new String[] { "channel1", "channel2" });
         
      • pubsubShardChannelsBinary

        java.util.concurrent.CompletableFuture<GlideString[]> pubsubShardChannelsBinary()
        Lists the currently active shard channels.
        Returns:
        An array of all active shard channels.
        See Also:
        valkey.io for details.
        Example:
        
         GlideString[] result = client.pubsubShardChannelsBinary().get();
         assert Arrays.equals(result, new GlideString[] { gs("channel1"), gs("channel2") });
         
      • pubsubShardChannels

        java.util.concurrent.CompletableFuture<java.lang.String[]> pubsubShardChannels​(java.lang.String pattern)
        Lists the currently active shard channels.
        Parameters:
        pattern - A glob-style pattern to match active shard channels.
        Returns:
        An array of currently active shard channels matching the given pattern.
        See Also:
        valkey.io for details.
        Example:
        
         String[] result = client.pubsubShardChannels("channel*").get();
         assert Arrays.equals(result, new String[] { "channel1", "channel2" });
         
      • pubsubShardChannels

        java.util.concurrent.CompletableFuture<GlideString[]> pubsubShardChannels​(GlideString pattern)
        Lists the currently active shard channels.
        Parameters:
        pattern - A glob-style pattern to match active shard channels.
        Returns:
        An array of currently active shard channels matching the given pattern.
        See Also:
        valkey.io for details.
        Example:
        
         GlideString[] result = client.pubsubShardChannels(gs.("channel*")).get();
         assert Arrays.equals(result, new GlideString[] { gs("channel1"), gs("channel2") });
         
      • pubsubShardNumSub

        java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,​java.lang.Long>> pubsubShardNumSub​(java.lang.String[] channels)
        Returns the number of subscribers (exclusive of clients subscribed to patterns) for the specified shard channels. Note that it is valid to call this command without channels. In this case, it will just return an empty map.
        Parameters:
        channels - The list of shard channels to query for the number of subscribers.
        Returns:
        An Map where keys are the shard channel names and values are the number of subscribers.
        See Also:
        valkey.io for details.
        Example:
        
         Map<String, Long> result = client.pubsubShardNumSub(new String[] {"channel1", "channel2"}).get();
         assert result.equals(Map.of("channel1", 3L, "channel2", 5L));
         
      • pubsubShardNumSub

        java.util.concurrent.CompletableFuture<java.util.Map<GlideString,​java.lang.Long>> pubsubShardNumSub​(GlideString[] channels)
        Returns the number of subscribers (exclusive of clients subscribed to patterns) for the specified shard channels. Note that it is valid to call this command without channels. In this case, it will just return an empty map.
        Parameters:
        channels - The list of shard channels to query for the number of subscribers.
        Returns:
        An Map where keys are the shard channel names and values are the number of subscribers.
        See Also:
        valkey.io for details.
        Example:
        
         Map<GlideString, Long> result = client.pubsubShardNumSub(new GlideString[] {gs("channel1"), gs("channel2")}).get();
         assert result.equals(Map.of(gs("channel1"), 3L, gs("channel2"), 5L));
         
      • ssubscribeLazy

        java.util.concurrent.CompletableFuture<java.lang.Void> ssubscribeLazy​(java.util.Set<java.lang.String> channels)
        Subscribes the client to the specified sharded channels.

        This command updates the client's internal desired subscription state without waiting for server confirmation. It returns immediately after updating the local state. The client will attempt to subscribe asynchronously in the background.

        Sharded pubsub (available in Valkey 7.0+) allows messages to be published to specific cluster shards, reducing overhead compared to cluster-wide pubsub.

        Note: Use getSubscriptions() to verify the actual server-side subscription state.

        Parameters:
        channels - A set of sharded channel names to subscribe to
        Returns:
        A CompletableFuture that completes when the subscription request is processed
        See Also:
        valkey.io for details
        Example:
        
         client.ssubscribeLazy(Set.of("shard-news", "shard-updates")).get();
         
      • ssubscribe

        java.util.concurrent.CompletableFuture<java.lang.Void> ssubscribe​(java.util.Set<java.lang.String> channels,
                                                                          int timeoutMs)
        Subscribes the client to the specified sharded channels with a timeout.

        This command updates the client's internal desired subscription state and waits for server confirmation.

        Parameters:
        channels - A set of sharded channel names to subscribe to
        timeoutMs - Maximum time in milliseconds to wait for subscription confirmation. A value of 0 blocks indefinitely until confirmation.
        Returns:
        A CompletableFuture that completes when the subscription is confirmed or times out
        See Also:
        valkey.io for details
        Example:
        
         client.ssubscribe(Set.of("shard-news", "shard-updates"), 5000).get();
         
      • sunsubscribeLazy

        java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribeLazy()
        Unsubscribes the client from all currently subscribed sharded channels.

        This command updates the client's internal desired subscription state without waiting for server confirmation. It returns immediately after updating the local state. The client will attempt to unsubscribe asynchronously in the background.

        Note: Use getSubscriptions() to verify the actual server-side subscription state.

        Returns:
        A CompletableFuture that completes when the unsubscription request is processed
        See Also:
        valkey.io for details
        Example:
        
         client.sunsubscribeLazy().get();
         
      • sunsubscribeLazy

        java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribeLazy​(java.util.Set<java.lang.String> channels)
        Unsubscribes the client from the specified sharded channels.

        This command updates the client's internal desired subscription state without waiting for server confirmation. It returns immediately after updating the local state. The client will attempt to unsubscribe asynchronously in the background.

        Note: Use getSubscriptions() to verify the actual server-side subscription state.

        Parameters:
        channels - A set of sharded channel names to unsubscribe from
        Returns:
        A CompletableFuture that completes when the unsubscription request is processed
        See Also:
        valkey.io for details
        Example:
        
         client.sunsubscribeLazy(Set.of("shard-news", "shard-updates")).get();
         
      • sunsubscribe

        java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribe​(java.util.Set<java.lang.String> channels,
                                                                            int timeoutMs)
        Unsubscribes the client from the specified sharded channels with a timeout.

        This command updates the client's internal desired subscription state and waits for server confirmation.

        Parameters:
        channels - A set of sharded channel names to unsubscribe from
        timeoutMs - Maximum time in milliseconds to wait for unsubscription confirmation. A value of 0 blocks indefinitely until confirmation.
        Returns:
        A CompletableFuture that completes when the unsubscription is confirmed or times out
        See Also:
        valkey.io for details
        Example:
        
         client.sunsubscribe(Set.of("shard-news", "shard-updates"), 5000).get();
         
      • sunsubscribe

        java.util.concurrent.CompletableFuture<java.lang.Void> sunsubscribe​(int timeoutMs)
        Unsubscribes the client from all currently subscribed sharded channels with a timeout.
        Parameters:
        timeoutMs - Maximum time in milliseconds to wait for unsubscription confirmation
        Returns:
        A CompletableFuture that completes when the unsubscription is confirmed or times out
        See Also:
        valkey.io for details
        Example:
        
         client.sunsubscribe(5000).get();