org.springframework.data.redis.connection.lettuce.LettuceSubscription Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.spring-data-redis
Show all versions of org.apache.servicemix.bundles.spring-data-redis
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar files.
/*
* Copyright 2011-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.util.AbstractSubscription;
/**
* Message subscription on top of Lettuce.
*
* @author Costin Leau
* @author Mark Paluch
* @author Christoph Strobl
* @author Sarah Abbey
* @author Murtuza Boxwala
* @author Jens Deppe
*/
public class LettuceSubscription extends AbstractSubscription {
private final StatefulRedisPubSubConnection connection;
private final LettuceMessageListener listener;
private final LettuceConnectionProvider connectionProvider;
private final RedisPubSubCommands pubsub;
/**
* Creates a new {@link LettuceSubscription} given {@link MessageListener}, {@link StatefulRedisPubSubConnection}, and
* {@link LettuceConnectionProvider}.
*
* @param listener the listener to notify, must not be {@literal null}.
* @param pubsubConnection must not be {@literal null}.
* @param connectionProvider must not be {@literal null}.
*/
protected LettuceSubscription(MessageListener listener,
StatefulRedisPubSubConnection pubsubConnection, LettuceConnectionProvider connectionProvider) {
super(listener);
this.connection = pubsubConnection;
this.listener = new LettuceMessageListener(listener);
this.connectionProvider = connectionProvider;
this.pubsub = connection.sync();
this.connection.addListener(this.listener);
}
protected StatefulRedisPubSubConnection getNativeConnection() {
return connection;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.util.AbstractSubscription#doClose()
*/
@Override
protected void doClose() {
if (!getChannels().isEmpty()) {
doUnsubscribe(true);
}
if (!getPatterns().isEmpty()) {
doPUnsubscribe(true);
}
connection.removeListener(this.listener);
connectionProvider.release(connection);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.util.AbstractSubscription#doPsubscribe(byte[][])
*/
@Override
protected void doPsubscribe(byte[]... patterns) {
pubsub.psubscribe(patterns);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.util.AbstractSubscription#doPUnsubscribe(boolean, byte[][])
*/
@Override
protected void doPUnsubscribe(boolean all, byte[]... patterns) {
if (all) {
pubsub.punsubscribe();
} else {
pubsub.punsubscribe(patterns);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.util.AbstractSubscription#doSubscribe(byte[][])
*/
@Override
protected void doSubscribe(byte[]... channels) {
pubsub.subscribe(channels);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.util.AbstractSubscription#doUnsubscribe(boolean, byte[][])
*/
@Override
protected void doUnsubscribe(boolean all, byte[]... channels) {
if (all) {
pubsub.unsubscribe();
} else {
pubsub.unsubscribe(channels);
}
}
}