refactoring

pull/1705/head
Nikita 6 years ago
parent ef4198ca07
commit a93320dde5

@ -16,7 +16,6 @@
package org.redisson; package org.redisson;
import org.redisson.api.listener.StatusListener; import org.redisson.api.listener.StatusListener;
import org.redisson.client.ChannelName;
import org.redisson.client.RedisPubSubListener; import org.redisson.client.RedisPubSubListener;
import org.redisson.client.protocol.pubsub.PubSubType; import org.redisson.client.protocol.pubsub.PubSubType;

@ -26,12 +26,19 @@ import org.redisson.client.ChannelName;
import org.redisson.client.RedisPubSubListener; import org.redisson.client.RedisPubSubListener;
import org.redisson.client.RedisTimeoutException; import org.redisson.client.RedisTimeoutException;
import org.redisson.client.codec.Codec; import org.redisson.client.codec.Codec;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.command.CommandExecutor; import org.redisson.command.CommandExecutor;
import org.redisson.command.CommandSyncExecutor;
import org.redisson.config.MasterSlaveServersConfig; import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
import org.redisson.pubsub.AsyncSemaphore; import org.redisson.pubsub.AsyncSemaphore;
import org.redisson.pubsub.PubSubConnectionEntry; import org.redisson.pubsub.PubSubConnectionEntry;
import org.redisson.pubsub.PublishSubscribeService; import org.redisson.pubsub.PublishSubscribeService;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
/** /**
* Distributed topic implementation. Messages are delivered to all message listeners across Redis cluster. * Distributed topic implementation. Messages are delivered to all message listeners across Redis cluster.
* *
@ -42,16 +49,16 @@ import org.redisson.pubsub.PublishSubscribeService;
public class RedissonPatternTopic<M> implements RPatternTopic<M> { public class RedissonPatternTopic<M> implements RPatternTopic<M> {
final PublishSubscribeService subscribeService; final PublishSubscribeService subscribeService;
final CommandExecutor commandExecutor; final CommandAsyncExecutor commandExecutor;
private final String name; private final String name;
private final ChannelName channelName; private final ChannelName channelName;
private final Codec codec; private final Codec codec;
protected RedissonPatternTopic(CommandExecutor commandExecutor, String name) { protected RedissonPatternTopic(CommandAsyncExecutor commandExecutor, String name) {
this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name); this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name);
} }
protected RedissonPatternTopic(Codec codec, CommandExecutor commandExecutor, String name) { protected RedissonPatternTopic(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
this.name = name; this.name = name;
this.channelName = new ChannelName(name); this.channelName = new ChannelName(name);
@ -75,7 +82,36 @@ public class RedissonPatternTopic<M> implements RPatternTopic<M> {
commandExecutor.syncSubscription(future); commandExecutor.syncSubscription(future);
return System.identityHashCode(pubSubListener); return System.identityHashCode(pubSubListener);
} }
@Override
public RFuture<Integer> addListenerAsync(PatternStatusListener listener) {
PubSubPatternStatusListener<M> pubSubListener = new PubSubPatternStatusListener<M>(listener, name);
return addListenerAsync(pubSubListener);
}
@Override
public RFuture<Integer> addListenerAsync(PatternMessageListener<M> listener) {
PubSubPatternMessageListener<M> pubSubListener = new PubSubPatternMessageListener<M>(listener, name);
return addListenerAsync(pubSubListener);
}
private RFuture<Integer> addListenerAsync(final RedisPubSubListener<?> pubSubListener) {
RFuture<PubSubConnectionEntry> future = subscribeService.subscribe(codec, channelName, pubSubListener);
final RPromise<Integer> result = new RedissonPromise<Integer>();
future.addListener(new FutureListener<PubSubConnectionEntry>() {
@Override
public void operationComplete(Future<PubSubConnectionEntry> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
result.trySuccess(System.identityHashCode(pubSubListener));
}
});
return result;
}
protected void acquire(AsyncSemaphore semaphore) { protected void acquire(AsyncSemaphore semaphore) {
MasterSlaveServersConfig config = commandExecutor.getConnectionManager().getConfig(); MasterSlaveServersConfig config = commandExecutor.getConnectionManager().getConfig();
int timeout = config.getTimeout() + config.getRetryInterval() * config.getRetryAttempts(); int timeout = config.getTimeout() + config.getRetryInterval() * config.getRetryAttempts();

@ -75,13 +75,11 @@ import org.redisson.reactive.RedissonListMultimapReactive;
import org.redisson.reactive.RedissonListReactive; import org.redisson.reactive.RedissonListReactive;
import org.redisson.reactive.RedissonMapCacheReactive; import org.redisson.reactive.RedissonMapCacheReactive;
import org.redisson.reactive.RedissonMapReactive; import org.redisson.reactive.RedissonMapReactive;
import org.redisson.reactive.RedissonPatternTopicReactive;
import org.redisson.reactive.RedissonReadWriteLockReactive; import org.redisson.reactive.RedissonReadWriteLockReactive;
import org.redisson.reactive.RedissonScoredSortedSetReactive; import org.redisson.reactive.RedissonScoredSortedSetReactive;
import org.redisson.reactive.RedissonSetCacheReactive; import org.redisson.reactive.RedissonSetCacheReactive;
import org.redisson.reactive.RedissonSetMultimapReactive; import org.redisson.reactive.RedissonSetMultimapReactive;
import org.redisson.reactive.RedissonSetReactive; import org.redisson.reactive.RedissonSetReactive;
import org.redisson.reactive.RedissonTopicReactive;
import org.redisson.reactive.RedissonTransactionReactive; import org.redisson.reactive.RedissonTransactionReactive;
/** /**
@ -297,22 +295,22 @@ public class RedissonReactive implements RedissonReactiveClient {
@Override @Override
public <M> RTopicReactive<M> getTopic(String name) { public <M> RTopicReactive<M> getTopic(String name) {
return new RedissonTopicReactive<M>(commandExecutor, name); return ReactiveProxyBuilder.create(commandExecutor, new RedissonTopic<M>(commandExecutor, name), RTopicReactive.class);
} }
@Override @Override
public <M> RTopicReactive<M> getTopic(String name, Codec codec) { public <M> RTopicReactive<M> getTopic(String name, Codec codec) {
return new RedissonTopicReactive<M>(codec, commandExecutor, name); return ReactiveProxyBuilder.create(commandExecutor, new RedissonTopic<M>(codec, commandExecutor, name), RTopicReactive.class);
} }
@Override @Override
public <M> RPatternTopicReactive<M> getPatternTopic(String pattern) { public <M> RPatternTopicReactive<M> getPatternTopic(String pattern) {
return new RedissonPatternTopicReactive<M>(commandExecutor, pattern); return ReactiveProxyBuilder.create(commandExecutor, new RedissonPatternTopic<M>(commandExecutor, pattern), RPatternTopicReactive.class);
} }
@Override @Override
public <M> RPatternTopicReactive<M> getPatternTopic(String pattern, Codec codec) { public <M> RPatternTopicReactive<M> getPatternTopic(String pattern, Codec codec) {
return new RedissonPatternTopicReactive<M>(codec, commandExecutor, pattern); return ReactiveProxyBuilder.create(commandExecutor, new RedissonPatternTopic<M>(codec, commandExecutor, pattern), RPatternTopicReactive.class);
} }
@Override @Override

@ -69,6 +69,7 @@ public class RedissonTopic<M> implements RTopic<M> {
this.subscribeService = commandExecutor.getConnectionManager().getSubscribeService(); this.subscribeService = commandExecutor.getConnectionManager().getSubscribeService();
} }
@Override
public List<String> getChannelNames() { public List<String> getChannelNames() {
return Collections.singletonList(name); return Collections.singletonList(name);
} }
@ -110,22 +111,15 @@ public class RedissonTopic<M> implements RTopic<M> {
} }
@Override @Override
public RFuture<Integer> addListenerAsync(final MessageListener<M> listener) { public RFuture<Integer> addListenerAsync(StatusListener listener) {
final PubSubMessageListener<M> pubSubListener = new PubSubMessageListener<M>(listener, name); PubSubStatusListener<M> pubSubListener = new PubSubStatusListener<M>(listener, name);
RFuture<PubSubConnectionEntry> future = subscribeService.subscribe(codec, channelName, pubSubListener); return addListenerAsync((RedisPubSubListener<?>)pubSubListener);
final RPromise<Integer> result = new RedissonPromise<Integer>(); }
future.addListener(new FutureListener<PubSubConnectionEntry>() {
@Override @Override
public void operationComplete(Future<PubSubConnectionEntry> future) throws Exception { public RFuture<Integer> addListenerAsync(MessageListener<M> listener) {
if (!future.isSuccess()) { PubSubMessageListener<M> pubSubListener = new PubSubMessageListener<M>(listener, name);
result.tryFailure(future.cause()); return addListenerAsync(pubSubListener);
return;
}
result.trySuccess(System.identityHashCode(pubSubListener));
}
});
return result;
} }
private int addListener(RedisPubSubListener<?> pubSubListener) { private int addListener(RedisPubSubListener<?> pubSubListener) {
@ -134,7 +128,7 @@ public class RedissonTopic<M> implements RTopic<M> {
return System.identityHashCode(pubSubListener); return System.identityHashCode(pubSubListener);
} }
public RFuture<Integer> addListenerAsync(final RedisPubSubListener<?> pubSubListener) { private RFuture<Integer> addListenerAsync(final RedisPubSubListener<?> pubSubListener) {
RFuture<PubSubConnectionEntry> future = subscribeService.subscribe(codec, channelName, pubSubListener); RFuture<PubSubConnectionEntry> future = subscribeService.subscribe(codec, channelName, pubSubListener);
final RPromise<Integer> result = new RedissonPromise<Integer>(); final RPromise<Integer> result = new RedissonPromise<Integer>();
future.addListener(new FutureListener<PubSubConnectionEntry>() { future.addListener(new FutureListener<PubSubConnectionEntry>() {

@ -21,7 +21,7 @@ import java.util.concurrent.TimeUnit;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
/** /**
* Distributed reactive implementation of {@link BlockingDeque} * Reactive interface for Redis based BlockingDeque object
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* @param <V> the type of elements held in this collection * @param <V> the type of elements held in this collection

@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
/** /**
* Distributed reactive implementation of {@link BlockingQueue} * Reactive interface for BlockingQueue object
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* @param <V> the type of elements held in this collection * @param <V> the type of elements held in this collection

@ -22,7 +22,7 @@ import org.reactivestreams.Publisher;
import io.reactivex.Flowable; import io.reactivex.Flowable;
/** /**
* Common reactive interface for collection object * Common RxJava2 interface for collection object
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *

@ -18,7 +18,7 @@ package org.redisson.api;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
/** /**
* Distributed reactive implementation of {@link java.util.Deque} * Reactive interface for Deque object
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *

@ -20,6 +20,7 @@ import java.util.Collection;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
/** /**
* Reactive interface for LexSortedSet object
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *

@ -21,7 +21,7 @@ import org.redisson.api.listener.PatternMessageListener;
import org.redisson.api.listener.PatternStatusListener; import org.redisson.api.listener.PatternStatusListener;
/** /**
* Distributed topic. Messages are delivered to all message listeners across Redis cluster. * Pattern based observer for Publish Subscribe object.
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *
@ -75,5 +75,8 @@ public interface RPatternTopic<M> {
*/ */
void removeAllListeners(); void removeAllListeners();
RFuture<Integer> addListenerAsync(PatternStatusListener listener);
RFuture<Integer> addListenerAsync(PatternMessageListener<M> listener);
} }

@ -22,7 +22,7 @@ import org.redisson.api.listener.PatternMessageListener;
import org.redisson.api.listener.PatternStatusListener; import org.redisson.api.listener.PatternStatusListener;
/** /**
* Distributed topic. Messages are delivered to all message listeners across Redis cluster. * Reactive interface for Pattern based observer for Publish Subscribe object.
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *

@ -25,6 +25,7 @@ import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry; import org.redisson.client.protocol.ScoredEntry;
/** /**
* Reactive interface for SortedSet object
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *

@ -16,6 +16,7 @@
package org.redisson.api; package org.redisson.api;
import org.redisson.api.listener.MessageListener; import org.redisson.api.listener.MessageListener;
import org.redisson.api.listener.StatusListener;
/** /**
* Distributed topic. Messages are delivered to all message listeners across Redis cluster. * Distributed topic. Messages are delivered to all message listeners across Redis cluster.
@ -34,6 +35,15 @@ public interface RTopicAsync<M> {
*/ */
RFuture<Long> publishAsync(M message); RFuture<Long> publishAsync(M message);
/**
* Subscribes to status changes of this topic
*
* @param listener for messages
* @return listener id
* @see org.redisson.api.listener.StatusListener
*/
RFuture<Integer> addListenerAsync(StatusListener listener);
/** /**
* Subscribes to this topic. * Subscribes to this topic.
* <code>MessageListener.onMessage</code> is called when any message * <code>MessageListener.onMessage</code> is called when any message

@ -22,7 +22,7 @@ import org.redisson.api.listener.MessageListener;
import org.redisson.api.listener.StatusListener; import org.redisson.api.listener.StatusListener;
/** /**
* Distributed topic. Messages are delivered to all message listeners across Redis cluster. * Reactive interface for Publish Subscribe object. Messages are delivered to all message listeners across Redis cluster.
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *
@ -30,6 +30,11 @@ import org.redisson.api.listener.StatusListener;
*/ */
public interface RTopicReactive<M> { public interface RTopicReactive<M> {
/**
* Get topic channel names
*
* @return channel names
*/
List<String> getChannelNames(); List<String> getChannelNames();
/** /**
@ -40,9 +45,30 @@ public interface RTopicReactive<M> {
*/ */
Publisher<Long> publish(M message); Publisher<Long> publish(M message);
/**
* Subscribes to status changes of this topic
*
* @param listener for messages
* @return listener id
* @see org.redisson.api.listener.StatusListener
*/
Publisher<Integer> addListener(StatusListener listener); Publisher<Integer> addListener(StatusListener listener);
/**
* Subscribes to this topic.
* <code>MessageListener.onMessage</code> is called when any message
* is published on this topic.
*
* @param listener for messages
* @return locally unique listener id
* @see org.redisson.api.listener.MessageListener
*/
Publisher<Integer> addListener(MessageListener<M> listener); Publisher<Integer> addListener(MessageListener<M> listener);
/**
* Removes the listener by <code>id</code> for listening this topic
*
* @param listenerId - listener id
*/
void removeListener(int listenerId); void removeListener(int listenerId);
} }

@ -19,7 +19,7 @@ import org.reactivestreams.Publisher;
import org.redisson.client.codec.Codec; import org.redisson.client.codec.Codec;
/** /**
* Transaction object allows to execute transactions over Redisson objects. * Reactive interface for transaction object allows to execute transactions over Redisson objects.
* Uses locks for write operations and maintains data modification operations list till the commit/rollback operation. * Uses locks for write operations and maintains data modification operations list till the commit/rollback operation.
* <p> * <p>
* Transaction isolation level: <b>READ_COMMITTED</b> * Transaction isolation level: <b>READ_COMMITTED</b>

@ -1,143 +0,0 @@
/**
* Copyright 2018 Nikita Koksharov
*
* 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
*
* http://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.redisson.reactive;
import java.util.Collections;
import java.util.List;
import org.reactivestreams.Publisher;
import org.redisson.PubSubPatternMessageListener;
import org.redisson.PubSubPatternStatusListener;
import org.redisson.api.RFuture;
import org.redisson.api.RPatternTopicReactive;
import org.redisson.api.listener.PatternMessageListener;
import org.redisson.api.listener.PatternStatusListener;
import org.redisson.client.ChannelName;
import org.redisson.client.RedisPubSubListener;
import org.redisson.client.RedisTimeoutException;
import org.redisson.client.codec.Codec;
import org.redisson.command.CommandReactiveExecutor;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
import org.redisson.pubsub.AsyncSemaphore;
import org.redisson.pubsub.PubSubConnectionEntry;
import org.redisson.pubsub.PublishSubscribeService;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import reactor.fn.Supplier;
/**
* Distributed topic implementation. Messages are delivered to all message listeners across Redis cluster.
*
* @author Nikita Koksharov
*
* @param <M> message
*/
public class RedissonPatternTopicReactive<M> implements RPatternTopicReactive<M> {
final PublishSubscribeService subscribeService;
final CommandReactiveExecutor commandExecutor;
private final String name;
private final ChannelName channelName;
private final Codec codec;
public RedissonPatternTopicReactive(CommandReactiveExecutor commandExecutor, String name) {
this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name);
}
public RedissonPatternTopicReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name) {
this.commandExecutor = commandExecutor;
this.name = name;
this.channelName = new ChannelName(name);
this.codec = codec;
this.subscribeService = commandExecutor.getConnectionManager().getSubscribeService();
}
@Override
public Publisher<Integer> addListener(final PatternStatusListener listener) {
return commandExecutor.reactive(new Supplier<RFuture<Integer>>() {
@Override
public RFuture<Integer> get() {
RPromise<Integer> promise = new RedissonPromise<Integer>();
addListener(new PubSubPatternStatusListener(listener, name), promise);
return promise;
}
});
};
@Override
public Publisher<Integer> addListener(final PatternMessageListener<M> listener) {
return commandExecutor.reactive(new Supplier<RFuture<Integer>>() {
@Override
public RFuture<Integer> get() {
RPromise<Integer> promise = new RedissonPromise<Integer>();
PubSubPatternMessageListener<M> pubSubListener = new PubSubPatternMessageListener<M>(listener, name);
addListener(pubSubListener, promise);
return promise;
}
});
}
private void addListener(final RedisPubSubListener<M> pubSubListener, final RPromise<Integer> promise) {
RFuture<PubSubConnectionEntry> future = subscribeService.psubscribe(channelName, codec, pubSubListener);
future.addListener(new FutureListener<PubSubConnectionEntry>() {
@Override
public void operationComplete(Future<PubSubConnectionEntry> future) throws Exception {
if (!future.isSuccess()) {
promise.tryFailure(future.cause());
return;
}
promise.trySuccess(pubSubListener.hashCode());
}
});
}
protected void acquire(AsyncSemaphore semaphore) {
MasterSlaveServersConfig config = commandExecutor.getConnectionManager().getConfig();
int timeout = config.getTimeout() + config.getRetryInterval() * config.getRetryAttempts();
if (!semaphore.tryAcquire(timeout)) {
throw new RedisTimeoutException("Remove listeners operation timeout: (" + timeout + "ms) for " + name + " topic");
}
}
@Override
public void removeListener(int listenerId) {
AsyncSemaphore semaphore = subscribeService.getSemaphore(channelName);
acquire(semaphore);
PubSubConnectionEntry entry = subscribeService.getPubSubEntry(channelName);
if (entry == null) {
semaphore.release();
return;
}
entry.removeListener(channelName, listenerId);
if (!entry.hasListeners(channelName)) {
subscribeService.punsubscribe(channelName, semaphore);
} else {
semaphore.release();
}
}
@Override
public List<String> getPatternNames() {
return Collections.singletonList(name);
}
}

@ -1,101 +0,0 @@
/**
* Copyright 2018 Nikita Koksharov
*
* 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
*
* http://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.redisson.reactive;
import java.util.Collections;
import java.util.List;
import org.reactivestreams.Publisher;
import org.redisson.PubSubMessageListener;
import org.redisson.PubSubStatusListener;
import org.redisson.RedissonTopic;
import org.redisson.api.RFuture;
import org.redisson.api.RTopic;
import org.redisson.api.RTopicReactive;
import org.redisson.api.listener.MessageListener;
import org.redisson.api.listener.StatusListener;
import org.redisson.client.RedisPubSubListener;
import org.redisson.client.codec.Codec;
import org.redisson.command.CommandReactiveExecutor;
import reactor.fn.Supplier;
/**
* Distributed topic implementation. Messages are delivered to all message listeners across Redis cluster.
*
* @author Nikita Koksharov
*
* @param <M> message
*/
public class RedissonTopicReactive<M> implements RTopicReactive<M> {
private final RTopic<M> topic;
private final CommandReactiveExecutor commandExecutor;
private final String name;
public RedissonTopicReactive(CommandReactiveExecutor commandExecutor, String name) {
this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name);
}
public RedissonTopicReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name) {
this.topic = new RedissonTopic<M>(codec, commandExecutor, name);
this.commandExecutor = commandExecutor;
this.name = name;
}
@Override
public List<String> getChannelNames() {
return Collections.singletonList(name);
}
@Override
public Publisher<Long> publish(final M message) {
return commandExecutor.reactive(new Supplier<RFuture<Long>>() {
@Override
public RFuture<Long> get() {
return topic.publishAsync(message);
}
});
}
@Override
public Publisher<Integer> addListener(StatusListener listener) {
return addListener(new PubSubStatusListener<Object>(listener, name));
};
@Override
public Publisher<Integer> addListener(MessageListener<M> listener) {
PubSubMessageListener<M> pubSubListener = new PubSubMessageListener<M>(listener, name);
return addListener(pubSubListener);
}
private Publisher<Integer> addListener(final RedisPubSubListener<?> pubSubListener) {
return commandExecutor.reactive(new Supplier<RFuture<Integer>>() {
@Override
public RFuture<Integer> get() {
return ((RedissonTopic<Integer>) topic).addListenerAsync(pubSubListener);
}
});
}
@Override
public void removeListener(int listenerId) {
topic.removeListener(listenerId);
}
}
Loading…
Cancel
Save