Feature - RTopicReactive.getMessages streaming method added
parent
ad091fe14f
commit
040878a7dd
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.redisson.api.RFuture;
|
||||
import org.redisson.api.RTopic;
|
||||
import org.redisson.api.listener.MessageListener;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class RedissonTopicReactive {
|
||||
|
||||
private final RTopic topic;
|
||||
|
||||
public RedissonTopicReactive(RTopic topic) {
|
||||
this.topic = topic;
|
||||
}
|
||||
|
||||
public <M> Flux<M> getMessages(Class<M> type) {
|
||||
return Flux.<M>create(emitter -> {
|
||||
emitter.onRequest(n -> {
|
||||
AtomicLong counter = new AtomicLong(n);
|
||||
RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() {
|
||||
@Override
|
||||
public void onMessage(CharSequence channel, M msg) {
|
||||
emitter.next(msg);
|
||||
if (counter.decrementAndGet() == 0) {
|
||||
topic.removeListenerAsync(this);
|
||||
emitter.complete();
|
||||
}
|
||||
}
|
||||
});
|
||||
t.whenComplete((id, e) -> {
|
||||
emitter.onDispose(() -> {
|
||||
topic.removeListenerAsync(id);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package org.redisson;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
import org.redisson.api.RTopicReactive;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
public class RedissonTopicReactiveTest extends BaseReactiveTest {
|
||||
|
||||
@Test
|
||||
public void testLong() throws InterruptedException {
|
||||
RTopicReactive topic = redisson.getTopic("test");
|
||||
Flux<String> messages = topic.getMessages(String.class);
|
||||
List<String> list = new ArrayList<>();
|
||||
messages.subscribe(new Subscriber<String>() {
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
s.request(10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(String t) {
|
||||
list.add(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < 15; i++) {
|
||||
sync(topic.publish("" + i));
|
||||
}
|
||||
|
||||
assertThat(list).containsExactly("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue