DATAREDIS-840 - Polishing.
Introduce dedicated Subscription.close() to unsubscribe() and punsubscribe(). Original Pull Request: #346
This commit is contained in:
@@ -91,4 +91,11 @@ public interface Subscription {
|
||||
* @return true if the subscription still applies, false otherwise.
|
||||
*/
|
||||
boolean isAlive();
|
||||
|
||||
/**
|
||||
* Shutdown the subscription and free any resources held.
|
||||
*
|
||||
* @since 1.8.12
|
||||
*/
|
||||
void close();
|
||||
}
|
||||
|
||||
@@ -97,6 +97,15 @@ public abstract class AbstractSubscription implements Subscription {
|
||||
*/
|
||||
protected abstract void doPUnsubscribe(boolean all, byte[]... patterns);
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.Subscription#close()
|
||||
*/
|
||||
@Override
|
||||
public void close() {
|
||||
doClose();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the subscription and free any resources held.
|
||||
*/
|
||||
|
||||
@@ -729,9 +729,11 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
synchronized (localMonitor) {
|
||||
subscriptionTaskRunning = true;
|
||||
}
|
||||
|
||||
try {
|
||||
connection = connectionFactory.getConnection();
|
||||
if (connection.isSubscribed()) {
|
||||
@@ -839,21 +841,21 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
if (!listening) {
|
||||
|
||||
if (!listening || connection == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
listening = false;
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Cancelling Redis subscription...");
|
||||
}
|
||||
|
||||
if (connection == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Subscription sub = connection.getSubscription();
|
||||
|
||||
if (sub != null) {
|
||||
|
||||
synchronized (localMonitor) {
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
@@ -861,8 +863,7 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
|
||||
}
|
||||
|
||||
try {
|
||||
sub.pUnsubscribe();
|
||||
sub.unsubscribe();
|
||||
sub.close();
|
||||
} catch (Exception e) {
|
||||
logger.warn("Unable to unsubscribe from subscriptions", e);
|
||||
}
|
||||
@@ -886,6 +887,7 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
|
||||
}
|
||||
|
||||
void closeConnection() {
|
||||
|
||||
if (connection != null) {
|
||||
logger.trace("Closing connection");
|
||||
try {
|
||||
@@ -898,6 +900,7 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
|
||||
}
|
||||
|
||||
void subscribeChannel(byte[]... channels) {
|
||||
|
||||
if (channels != null && channels.length > 0) {
|
||||
if (connection != null) {
|
||||
synchronized (localMonitor) {
|
||||
|
||||
Reference in New Issue
Block a user