Class RedisPubSubConnection

java.lang.Object
com.blackrook.redis.RedisConnectionAbstract
com.blackrook.redis.RedisPubSubConnection
All Implemented Interfaces:
RedisPubSubCommands, AutoCloseable

public class RedisPubSubConnection extends RedisConnectionAbstract implements RedisPubSubCommands
A special Redis connection that is essentially a subscription to one or more Redis channels. Once a connection is opened, this connection will listen indefinitely until it is closed or is unsubscribed from all channels that it is listening to.

This will fire events to RedisSubscriptionListeners that have been attached to this connection. The thread that is spawned by this connection can be set to either be daemon or not (see Thread.setDaemon(boolean)) so that its life does or does not affect JVM runtime life.

Author:
Matthew Tropiano
  • Constructor Details

    • RedisPubSubConnection

      public RedisPubSubConnection(RedisSubscriptionListener... listeners) throws IOException
      Creates an open connection to localhost, port 6379, the default Redis port.
      Parameters:
      listeners - the listeners to add to this pub/sub connection.
      Throws:
      IOException - if an I/O error occurs when creating the socket.
      UnknownHostException - if the IP address of the host could not be determined.
      SecurityException - if a security manager exists and doesn't allow the connection to be made.
    • RedisPubSubConnection

      public RedisPubSubConnection(String host, int port, RedisSubscriptionListener... listeners) throws IOException
      Creates an open connection.
      Parameters:
      host - the server hostname or address.
      port - the server connection port.
      listeners - the listeners to add to this pub/sub connection.
      Throws:
      IOException - if an I/O error occurs when creating the socket.
      UnknownHostException - if the IP address of the host could not be determined.
      SecurityException - if a security manager exists and doesn't allow the connection to be made.
    • RedisPubSubConnection

      public RedisPubSubConnection(String host, int port, String password, RedisSubscriptionListener... listeners) throws IOException
      Creates an open connection.
      Parameters:
      host - the server hostname or address.
      port - the server connection port.
      password - the server database password.
      listeners - the listeners to add to this pub/sub connection.
      Throws:
      IOException - if an I/O error occurs when creating the socket.
      UnknownHostException - if the IP address of the host could not be determined.
      SecurityException - if a security manager exists and doesn't allow the connection to be made.
      RedisException - if the password in the server information is incorrect.
    • RedisPubSubConnection

      public RedisPubSubConnection(RedisInfo info, RedisSubscriptionListener... listeners) throws IOException
      Creates an open connection.
      Parameters:
      info - the RedisInfo class detailing a connection.
      listeners - the listeners to add to this pub/sub connection.
      Throws:
      IOException - if an I/O error occurs when creating the socket.
      UnknownHostException - if the IP address of the host could not be determined.
      SecurityException - if a security manager exists and doesn't allow the connection to be made.
      RedisException - if the password in the server information is incorrect.
  • Method Details

    • addListener

      public void addListener(RedisSubscriptionListener... listeners)
      Adds RedisSubscriptionListeners to this connection. All listeners on this connection are alerted when a message is received.
      Parameters:
      listeners - the listeners to add.
    • removeListeners

      public void removeListeners(RedisSubscriptionListener... listeners)
      Removes RedisSubscriptionListeners from this connection.
      Parameters:
      listeners - to remove.
    • subscribe

      public void subscribe(String... channels)
      Description copied from interface: RedisPubSubCommands

      From http://redis.io/commands/subscribe:

      Available since 2.0.0.

      Time complexity: O(N) where N is the number of channels to subscribe to.

      Subscribes the client to the specified channels.

      Specified by:
      subscribe in interface RedisPubSubCommands
      Parameters:
      channels - the channels to subscribe to.
    • unsubscribe

      public void unsubscribe(String... channels)
      Description copied from interface: RedisPubSubCommands

      From http://redis.io/commands/unsubscribe:

      Available since 2.0.0.

      Time complexity: O(N) where N is the number of clients already subscribed to a channel.

      Unsubscribes the client from the given channels, or from all of them if none is given.

      Specified by:
      unsubscribe in interface RedisPubSubCommands
      Parameters:
      channels - the channels to subscribe to.
    • psubscribe

      public void psubscribe(String... patterns)
      Description copied from interface: RedisPubSubCommands

      From http://redis.io/commands/psubscribe:

      Available since 2.0.0.

      Time complexity: O(N) where N is the number of patterns the client is already subscribed to.

      Subscribes the client to the given patterns.

      Specified by:
      psubscribe in interface RedisPubSubCommands
      Parameters:
      patterns - the patterns of channels to subscribe to.
    • punsubscribe

      public void punsubscribe(String... patterns)
      Description copied from interface: RedisPubSubCommands

      From http://redis.io/commands/punsubscribe:

      Available since 2.0.0.

      Time complexity: O(N+M) where N is the number of patterns the client is already subscribed and M is the number of total patterns subscribed in the system (by any client).

      Unsubscribes the client from the given patterns, or from all of them if none is given.

      Specified by:
      punsubscribe in interface RedisPubSubCommands
      Parameters:
      patterns - the patterns of channels to subscribe to.
    • fireOnSubscribe

      protected void fireOnSubscribe(String channelName, long channelTotal)
      Fires an event to listeners when this subscription connection subscribes to a channel.
      Parameters:
      channelName - the subscribed channel.
      channelTotal - the total number of channels that this subscriber is subscribed to.
    • fireOnUnsubscribe

      protected void fireOnUnsubscribe(String channelName, long channelTotal)
      Fires an event to listeners when this subscription connection unsubscribes from a channel.
      Parameters:
      channelName - the channel that this unsubscribed from.
      channelTotal - the total number of channels that this subscriber is subscribed to.
    • fireOnPatternSubscribe

      protected void fireOnPatternSubscribe(String channelPattern, long channelTotal)
      Fires an event to listeners when this subscription connection subscribes to a set of channels via a pattern.
      Parameters:
      channelPattern - the channel pattern that this subscribed to.
      channelTotal - the total number of channels that this subscriber is subscribed to.
    • fireOnPatternUnsubscribe

      protected void fireOnPatternUnsubscribe(String channelPattern, long channelTotal)
      Fires an event to listeners when this subscription connection unsubscribes from a set of channels via a pattern.
      Parameters:
      channelPattern - the channel pattern that this unsubscribed from.
      channelTotal - the total number of channels that this subscriber is subscribed to.
    • fireOnMessageReceive

      protected void fireOnMessageReceive(String channel, String message)
      Fires an event to listeners when this subscription connection receives a message from a channel.
      Parameters:
      channel - the channel that the message came from.
      message - the message received.
    • fireOnPatternMessageReceive

      protected void fireOnPatternMessageReceive(String channelPattern, String channel, String message)
      Fires an event to listeners when this subscription connection receives a message from a channel.
      Parameters:
      channelPattern - the channel pattern that was matched.
      channel - the channel that the message came from.
      message - the message received.