DATAREDIS-612 - Polishing.

Introduce dedicated methods utilizing the ReactiveMessageListenerContainer to create a stream of Messages via ReactiveRedisTemplate.

redisTemplate.listenToChannel("channel1", "channel2").doOnNext(msg -> {
    // message processing ...
}).subscribe();

Add close-/destroyLater methods avoiding blocking shutdown of connections and containers.

Original Pull Request: #295
This commit is contained in:
Christoph Strobl
2017-11-28 10:30:56 +01:00
parent a3b96add06
commit 16f7a445ba
21 changed files with 474 additions and 176 deletions

View File

@@ -214,7 +214,6 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
MessageListener listener = (message, pattern) -> {
messages.add(message);
System.out.println("Received message '" + new String(message.getBody()) + "'");
};
Thread t = new Thread() {
@@ -272,7 +271,6 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
final MessageListener listener = (message, pattern) -> {
assertEquals(expectedPattern, new String(pattern));
messages.add(message);
System.out.println("Received message '" + new String(message.getBody()) + "'");
};
Thread th = new Thread() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2018 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.
@@ -37,13 +37,14 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.ReactiveSubscription.ChannelMessage;
import org.springframework.data.redis.connection.ReactiveSubscription.Message;
import org.springframework.data.redis.connection.ReactiveSubscription.PatternMessage;
/**
* Unit tests for {@link LettuceReactiveSubscription}.
*
*
* @author Mark Paluch
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public class LettuceReactiveSubscriptionUnitTests {
@@ -193,7 +194,7 @@ public class LettuceReactiveSubscriptionUnitTests {
when(commandsMock.observePatterns()).thenReturn(Flux.never());
StepVerifier.create(subscription.receive()).then(() -> {
subscription.terminate().subscribe();
subscription.cancel().subscribe();
}).expectError(CancellationException.class).verify();
assertThat(subscription.getPatterns()).isEmpty();
@@ -211,7 +212,7 @@ public class LettuceReactiveSubscriptionUnitTests {
when(commandsMock.observeChannels()).thenReturn(Flux.never());
when(commandsMock.observePatterns()).thenReturn(emitter);
Flux<ChannelMessage<ByteBuffer, ByteBuffer>> receive = subscription.receive();
Flux<Message<ByteBuffer, ByteBuffer>> receive = subscription.receive();
Disposable subscribe = receive.subscribe();
assertThat(emitter.downstreamCount()).isEqualTo(1);
@@ -221,13 +222,15 @@ public class LettuceReactiveSubscriptionUnitTests {
}
private static io.lettuce.core.pubsub.api.reactive.ChannelMessage<ByteBuffer, ByteBuffer> createChannelMessage(
String channel, String body) {
return new io.lettuce.core.pubsub.api.reactive.ChannelMessage<>(getByteBuffer(channel), getByteBuffer(body));
String channel, String message) {
return new io.lettuce.core.pubsub.api.reactive.ChannelMessage<>(getByteBuffer(channel), getByteBuffer(message));
}
private static io.lettuce.core.pubsub.api.reactive.PatternMessage<ByteBuffer, ByteBuffer> createPatternMessage(
String pattern, String channel, String body) {
String pattern, String channel, String message) {
return new io.lettuce.core.pubsub.api.reactive.PatternMessage<>(getByteBuffer(pattern), getByteBuffer(channel),
getByteBuffer(body));
getByteBuffer(message));
}
}

View File

@@ -39,6 +39,9 @@ import org.springframework.data.redis.Person;
import org.springframework.data.redis.PersonObjectFactory;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.ReactiveRedisClusterConnection;
import org.springframework.data.redis.connection.ReactiveSubscription.ChannelMessage;
import org.springframework.data.redis.connection.ReactiveSubscription.Message;
import org.springframework.data.redis.connection.ReactiveSubscription.PatternMessage;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.script.DefaultRedisScript;
@@ -386,4 +389,49 @@ public class ReactiveRedisTemplateIntegrationTests<K, V> {
StepVerifier.create(hashOperations.get(key, hashField)).expectNext(hashValue).verifyComplete();
}
@Test // DATAREDIS-612
public void listenToChannelShouldReceiveChannelMessagesCorrectly() throws InterruptedException {
String channel = "my-channel";
V message = valueFactory.instance();
StepVerifier.create(redisTemplate.listenToChannel(channel)) //
.thenAwait(Duration.ofMillis(500)) // just make sure we the subscription completed
.then(() -> redisTemplate.convertAndSend(channel, message).block()) //
.assertNext(received -> {
assertThat(received).isInstanceOf(ChannelMessage.class);
assertThat(received.getMessage()).isEqualTo(message);
assertThat(received.getChannel()).isEqualTo(channel);
}) //
.thenAwait(Duration.ofMillis(10)) //
.thenCancel() //
.verify(Duration.ofSeconds(3));
}
@Test // DATAREDIS-612
public void listenToChannelPatternShouldReceiveChannelMessagesCorrectly() throws InterruptedException {
String channel = "my-channel";
String pattern = "my-*";
V message = valueFactory.instance();
Flux<? extends Message<String, V>> stream = redisTemplate.listenToPattern(pattern);
StepVerifier.create(stream) //
.thenAwait(Duration.ofMillis(500)) // just make sure we the subscription completed
.then(() -> redisTemplate.convertAndSend(channel, message).block()) //
.assertNext(received -> {
assertThat(received).isInstanceOf(PatternMessage.class);
assertThat(received.getMessage()).isEqualTo(message);
assertThat(received.getChannel()).isEqualTo(channel);
assertThat(((PatternMessage) received).getPattern()).isEqualTo(pattern);
}) //
.thenCancel() //
.verify(Duration.ofSeconds(3));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2018 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2018 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.
@@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.*;
import reactor.core.Disposable;
import reactor.test.StepVerifier;
import java.time.Duration;
import java.util.Collection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
@@ -35,6 +36,7 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.data.redis.connection.ReactiveSubscription.ChannelMessage;
import org.springframework.data.redis.connection.ReactiveSubscription.PatternMessage;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@@ -44,7 +46,7 @@ import org.springframework.lang.Nullable;
/**
* Integration tests for {@link ReactiveRedisMessageListenerContainer} via Lettuce.
*
*
* @author Mark Paluch
*/
@RunWith(Parameterized.class)
@@ -52,7 +54,7 @@ public class ReactiveRedisMessageListenerContainerIntegrationTests {
static final String CHANNEL1 = "my-channel";
static final String PATTERN1 = "my-chan*";
public static final String MESSAGE = "hello world";
static final String MESSAGE = "hello world";
private final LettuceConnectionFactory connectionFactory;
private @Nullable RedisConnection connection;
@@ -154,6 +156,45 @@ public class ReactiveRedisMessageListenerContainerIntegrationTests {
container.destroy();
}
@Test // DATAREDIS-612
public void listenToChannelShouldReceiveChannelMessagesCorrectly() throws InterruptedException {
ReactiveRedisTemplate<String, String> template = new ReactiveRedisTemplate<>(connectionFactory,
RedisSerializationContext.string());
StepVerifier.create(template.listenToChannel(CHANNEL1)) //
.thenAwait(Duration.ofMillis(100)) // just make sure we the subscription completed
.then(() -> connection.publish(CHANNEL1.getBytes(), MESSAGE.getBytes())) //
.assertNext(message -> {
assertThat(message).isInstanceOf(ChannelMessage.class);
assertThat(message.getMessage()).isEqualTo(MESSAGE);
assertThat(((ChannelMessage) message).getChannel()).isEqualTo(CHANNEL1);
}) //
.thenCancel() //
.verify();
}
@Test // DATAREDIS-612
public void listenToPatternShouldReceiveMessagesCorrectly() {
ReactiveRedisTemplate<String, String> template = new ReactiveRedisTemplate<>(connectionFactory,
RedisSerializationContext.string());
StepVerifier.create(template.listenToPattern(PATTERN1)) //
.thenAwait(Duration.ofMillis(100)) // just make sure we the subscription completed
.then(() -> connection.publish(CHANNEL1.getBytes(), MESSAGE.getBytes())) //
.assertNext(message -> {
assertThat(message).isInstanceOf(PatternMessage.class);
assertThat(((PatternMessage) message).getPattern()).isEqualTo(PATTERN1);
assertThat(((PatternMessage) message).getChannel()).isEqualTo(CHANNEL1);
assertThat(message.getMessage()).isEqualTo(MESSAGE);
}) //
.thenCancel() //
.verify();
}
private static Runnable awaitSubscription(Supplier<Collection<ReactiveSubscription>> activeSubscriptions) {
return () -> {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2018 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.
@@ -19,6 +19,7 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.redis.util.ByteUtils.*;
import org.springframework.data.redis.connection.ReactiveSubscription.Message;
import reactor.core.Disposable;
import reactor.core.publisher.DirectProcessor;
import reactor.core.publisher.Flux;
@@ -34,9 +35,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.redis.connection.ReactivePubSubCommands;
import org.springframework.data.redis.connection.ReactiveRedisConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.ReactiveRedisPubSubCommands;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.data.redis.connection.ReactiveSubscription.ChannelMessage;
import org.springframework.data.redis.connection.ReactiveSubscription.PatternMessage;
@@ -53,7 +54,7 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
@Mock ReactiveRedisConnectionFactory connectionFactoryMock;
@Mock ReactiveRedisConnection connectionMock;
@Mock ReactiveRedisPubSubCommands commandsMock;
@Mock ReactivePubSubCommands commandsMock;
@Mock ReactiveSubscription subscriptionMock;
@Before
@@ -61,6 +62,7 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
when(connectionFactoryMock.getReactiveConnection()).thenReturn(connectionMock);
when(connectionMock.pubSubCommands()).thenReturn(commandsMock);
when(connectionMock.closeLater()).thenReturn(Mono.empty());
when(commandsMock.createSubscription()).thenReturn(Mono.just(subscriptionMock));
when(subscriptionMock.subscribe(any())).thenReturn(Mono.empty());
when(subscriptionMock.pSubscribe(any())).thenReturn(Mono.empty());
@@ -85,8 +87,8 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
when(subscriptionMock.receive()).thenReturn(Flux.never());
container = createContainer();
StepVerifier.create(container.receive(PatternTopic.of("foo*"), PatternTopic.of("bar*"))).thenRequest(1)
.thenAwait().thenCancel().verify();
StepVerifier.create(container.receive(PatternTopic.of("foo*"), PatternTopic.of("bar*"))).thenRequest(1).thenAwait()
.thenCancel().verify();
verify(subscriptionMock).pSubscribe(getByteBuffer("foo*"), getByteBuffer("bar*"));
}
@@ -117,12 +119,12 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
@Test // DATAREDIS-612
public void shouldEmitChannelMessage() {
DirectProcessor<ChannelMessage<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
DirectProcessor<Message<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
when(subscriptionMock.receive()).thenReturn(processor);
container = createContainer();
Flux<ChannelMessage<String, String>> messageStream = container.receive(ChannelTopic.of("foo"));
Flux<Message<String, String>> messageStream = container.receive(ChannelTopic.of("foo"));
StepVerifier.create(messageStream).then(() -> {
processor.onNext(createChannelMessage("foo", "message"));
@@ -136,7 +138,7 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
@Test // DATAREDIS-612
public void shouldEmitPatternMessage() {
DirectProcessor<ChannelMessage<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
DirectProcessor<Message<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
when(subscriptionMock.receive()).thenReturn(processor);
container = createContainer();
@@ -164,7 +166,7 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
when(subscriptionMock.receive()).thenReturn(DirectProcessor.create());
container = createContainer();
Flux<ChannelMessage<String, String>> messageStream = container.receive(ChannelTopic.of("foo*"));
Flux<Message<String, String>> messageStream = container.receive(ChannelTopic.of("foo*"));
Disposable subscription = messageStream.subscribe();
@@ -184,7 +186,7 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
when(subscriptionMock.receive()).thenReturn(DirectProcessor.create());
container = createContainer();
Flux<ChannelMessage<String, String>> messageStream = container.receive(new ChannelTopic("foo*"));
Flux<Message<String, String>> messageStream = container.receive(new ChannelTopic("foo*"));
Disposable first = messageStream.subscribe();
Disposable second = messageStream.subscribe();
@@ -220,10 +222,10 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
@Test // DATAREDIS-612
public void shouldTerminateSubscriptionsOnShutdown() {
DirectProcessor<ChannelMessage<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
DirectProcessor<Message<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
when(subscriptionMock.receive()).thenReturn(processor);
when(subscriptionMock.terminate()).thenReturn(Mono.defer(() -> {
when(subscriptionMock.cancel()).thenReturn(Mono.defer(() -> {
processor.onError(new CancellationException());
return Mono.empty();
@@ -240,7 +242,7 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
@Test // DATAREDIS-612
public void shouldCleanupDownstream() {
DirectProcessor<ChannelMessage<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
DirectProcessor<Message<ByteBuffer, ByteBuffer>> processor = DirectProcessor.create();
when(subscriptionMock.receive()).thenReturn(processor);
container = createContainer();
@@ -265,6 +267,7 @@ public class ReactiveRedisMessageListenerContainerUnitTests {
private static PatternMessage<ByteBuffer, ByteBuffer, ByteBuffer> createPatternMessage(String pattern, String channel,
String body) {
return new PatternMessage<>(getByteBuffer(pattern), getByteBuffer(channel), getByteBuffer(body));
}
}