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
RedisSubscriptionListener
s that have been attached to this connection. The thread that is spawned by this connection can be set to either be daemon or not (seeThread.setDaemon(boolean)
) so that its life does or does not affect JVM runtime life.- Author:
- Matthew Tropiano
-
-
Field Summary
-
Fields inherited from class com.blackrook.redis.RedisConnectionAbstract
reader, writer
-
-
Constructor Summary
Constructors Constructor Description RedisPubSubConnection(RedisSubscriptionListener... listeners)
Creates an open connection to localhost, port 6379, the default Redis port.RedisPubSubConnection(RedisInfo info, RedisSubscriptionListener... listeners)
Creates an open connection.RedisPubSubConnection(String host, int port, RedisSubscriptionListener... listeners)
Creates an open connection.RedisPubSubConnection(String host, int port, String password, RedisSubscriptionListener... listeners)
Creates an open connection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(RedisSubscriptionListener... listeners)
AddsRedisSubscriptionListener
s to this connection.protected void
fireOnMessageReceive(String channel, String message)
Fires an event to listeners when this subscription connection receives a message from a channel.protected void
fireOnPatternMessageReceive(String channelPattern, String channel, String message)
Fires an event to listeners when this subscription connection receives a message from a channel.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.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.protected void
fireOnSubscribe(String channelName, long channelTotal)
Fires an event to listeners when this subscription connection subscribes to a channel.protected void
fireOnUnsubscribe(String channelName, long channelTotal)
Fires an event to listeners when this subscription connection unsubscribes from a channel.void
psubscribe(String... patterns)
void
punsubscribe(String... patterns)
void
removeListeners(RedisSubscriptionListener... listeners)
RemovesRedisSubscriptionListener
s from this connection.void
subscribe(String... channels)
void
unsubscribe(String... channels)
-
Methods inherited from class com.blackrook.redis.RedisConnectionAbstract
close, disconnect, isBound, isClosed, isConnected, reconnect
-
-
-
-
Constructor Detail
-
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
- theRedisInfo
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 Detail
-
addListener
public void addListener(RedisSubscriptionListener... listeners)
AddsRedisSubscriptionListener
s 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)
RemovesRedisSubscriptionListener
s 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 interfaceRedisPubSubCommands
- 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 interfaceRedisPubSubCommands
- 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 interfaceRedisPubSubCommands
- 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 interfaceRedisPubSubCommands
- 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.
-
-