RCountDownLatch and RLock refactoring.
parent
d84b98627d
commit
2436dcc52f
@ -0,0 +1,13 @@
|
||||
package org.redisson;
|
||||
|
||||
import io.netty.util.concurrent.Promise;
|
||||
|
||||
public interface PubSubEntry<E> {
|
||||
|
||||
void aquire();
|
||||
|
||||
int release();
|
||||
|
||||
Promise<E> getPromise();
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package org.redisson.pubsub;
|
||||
|
||||
import org.redisson.RedissonCountDownLatch;
|
||||
import org.redisson.RedissonCountDownLatchEntry;
|
||||
import org.redisson.client.BaseRedisPubSubListener;
|
||||
import org.redisson.client.RedisPubSubListener;
|
||||
import org.redisson.client.protocol.pubsub.PubSubType;
|
||||
|
||||
import io.netty.util.concurrent.Promise;
|
||||
|
||||
public class CountDownLatchPubSub extends PublishSubscribe<RedissonCountDownLatchEntry> {
|
||||
|
||||
@Override
|
||||
protected RedissonCountDownLatchEntry createEntry(Promise<RedissonCountDownLatchEntry> newPromise) {
|
||||
return new RedissonCountDownLatchEntry(newPromise);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RedisPubSubListener<Long> createListener(final String channelName, final RedissonCountDownLatchEntry value) {
|
||||
RedisPubSubListener<Long> listener = new BaseRedisPubSubListener<Long>() {
|
||||
|
||||
@Override
|
||||
public void onMessage(String channel, Long message) {
|
||||
if (!channelName.equals(channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.equals(RedissonCountDownLatch.zeroCountMessage)) {
|
||||
value.getLatch().open();
|
||||
}
|
||||
if (message.equals(RedissonCountDownLatch.newCountMessage)) {
|
||||
value.getLatch().close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onStatus(PubSubType type, String channel) {
|
||||
if (!channelName.equals(channel)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == PubSubType.SUBSCRIBE) {
|
||||
value.getPromise().trySuccess(value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
return listener;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package org.redisson.pubsub;
|
||||
|
||||
import org.redisson.RedissonLock;
|
||||
import org.redisson.RedissonLockEntry;
|
||||
import org.redisson.client.BaseRedisPubSubListener;
|
||||
import org.redisson.client.RedisPubSubListener;
|
||||
import org.redisson.client.protocol.pubsub.PubSubType;
|
||||
|
||||
import io.netty.util.concurrent.Promise;
|
||||
|
||||
public class LockPubSub extends PublishSubscribe<RedissonLockEntry> {
|
||||
|
||||
@Override
|
||||
protected RedissonLockEntry createEntry(Promise<RedissonLockEntry> newPromise) {
|
||||
return new RedissonLockEntry(newPromise);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RedisPubSubListener<Long> createListener(final String channelName, final RedissonLockEntry value) {
|
||||
RedisPubSubListener<Long> listener = new BaseRedisPubSubListener<Long>() {
|
||||
|
||||
@Override
|
||||
public void onMessage(String channel, Long message) {
|
||||
if (!channelName.equals(channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.equals(RedissonLock.unlockMessage)) {
|
||||
value.getLatch().release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onStatus(PubSubType type, String channel) {
|
||||
if (!channelName.equals(channel)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == PubSubType.SUBSCRIBE) {
|
||||
value.getPromise().trySuccess(value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
return listener;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package org.redisson.pubsub;
|
||||
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.redisson.PubSubEntry;
|
||||
import org.redisson.client.RedisPubSubListener;
|
||||
import org.redisson.client.codec.LongCodec;
|
||||
import org.redisson.connection.ConnectionManager;
|
||||
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.Promise;
|
||||
import io.netty.util.internal.PlatformDependent;
|
||||
|
||||
abstract class PublishSubscribe<E extends PubSubEntry<E>> {
|
||||
|
||||
private final ConcurrentMap<String, E> entries = PlatformDependent.newConcurrentHashMap();
|
||||
|
||||
public void unsubscribe(E entry, String entryName, String channelName, ConnectionManager connectionManager) {
|
||||
synchronized (this) {
|
||||
if (entry.release() == 0) {
|
||||
// just an assertion
|
||||
boolean removed = entries.remove(entryName) == entry;
|
||||
if (removed) {
|
||||
connectionManager.unsubscribe(channelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public E getEntry(String entryName) {
|
||||
return entries.get(entryName);
|
||||
}
|
||||
|
||||
public Future<E> subscribe(String entryName, String channelName, ConnectionManager connectionManager) {
|
||||
synchronized (this) {
|
||||
E entry = entries.get(entryName);
|
||||
if (entry != null) {
|
||||
entry.aquire();
|
||||
return entry.getPromise();
|
||||
}
|
||||
|
||||
Promise<E> newPromise = connectionManager.newPromise();
|
||||
E value = createEntry(newPromise);
|
||||
value.aquire();
|
||||
|
||||
E oldValue = entries.putIfAbsent(entryName, value);
|
||||
if (oldValue != null) {
|
||||
oldValue.aquire();
|
||||
return oldValue.getPromise();
|
||||
}
|
||||
|
||||
RedisPubSubListener<Long> listener = createListener(channelName, value);
|
||||
connectionManager.subscribe(LongCodec.INSTANCE, channelName, listener);
|
||||
return newPromise;
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract E createEntry(Promise<E> newPromise);
|
||||
|
||||
protected abstract RedisPubSubListener<Long> createListener(String channelName, E value);
|
||||
}
|
Loading…
Reference in New Issue