Class RedisPubSubConnection
- All Implemented Interfaces:
RedisPubSubCommands, AutoCloseable
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
-
Field Summary
Fields inherited from class RedisConnectionAbstract
reader, writer -
Constructor Summary
ConstructorsConstructorDescriptionRedisPubSubConnection(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
Modifier and TypeMethodDescriptionvoidaddListener(RedisSubscriptionListener... listeners) AddsRedisSubscriptionListeners to this connection.protected voidfireOnMessageReceive(String channel, String message) Fires an event to listeners when this subscription connection receives a message from a channel.protected voidfireOnPatternMessageReceive(String channelPattern, String channel, String message) Fires an event to listeners when this subscription connection receives a message from a channel.protected voidfireOnPatternSubscribe(String channelPattern, long channelTotal) Fires an event to listeners when this subscription connection subscribes to a set of channels via a pattern.protected voidfireOnPatternUnsubscribe(String channelPattern, long channelTotal) Fires an event to listeners when this subscription connection unsubscribes from a set of channels via a pattern.protected voidfireOnSubscribe(String channelName, long channelTotal) Fires an event to listeners when this subscription connection subscribes to a channel.protected voidfireOnUnsubscribe(String channelName, long channelTotal) Fires an event to listeners when this subscription connection unsubscribes from a channel.voidpsubscribe(String... patterns) voidpunsubscribe(String... patterns) voidremoveListeners(RedisSubscriptionListener... listeners) RemovesRedisSubscriptionListeners from this connection.voidvoidunsubscribe(String... channels) Methods inherited from class RedisConnectionAbstract
close, disconnect, isBound, isClosed, isConnected, reconnect
-
Constructor Details
-
RedisPubSubConnection
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- theRedisInfoclass 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
AddsRedisSubscriptionListeners to this connection. All listeners on this connection are alerted when a message is received.- Parameters:
listeners- the listeners to add.
-
removeListeners
RemovesRedisSubscriptionListeners from this connection.- Parameters:
listeners- to remove.
-
subscribe
Description copied from interface:RedisPubSubCommandsFrom 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:
subscribein interfaceRedisPubSubCommands- Parameters:
channels- the channels to subscribe to.
-
unsubscribe
Description copied from interface:RedisPubSubCommandsFrom 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:
unsubscribein interfaceRedisPubSubCommands- Parameters:
channels- the channels to subscribe to.
-
psubscribe
Description copied from interface:RedisPubSubCommandsFrom 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:
psubscribein interfaceRedisPubSubCommands- Parameters:
patterns- the patterns of channels to subscribe to.
-
punsubscribe
Description copied from interface:RedisPubSubCommandsFrom 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:
punsubscribein interfaceRedisPubSubCommands- Parameters:
patterns- the patterns of channels to subscribe to.
-
fireOnSubscribe
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
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
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
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
-
fireOnPatternMessageReceive
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.
-