Search in sources :

Example 1 with ReactiveSubscription

use of org.springframework.data.redis.connection.ReactiveSubscription in project redisson by redisson.

the class RedissonSubscribeReactiveTest method testUnSubscribe.

@Test
public void testUnSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();
    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    Awaitility.await().atMost(Duration.ONE_SECOND).until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    ss.unsubscribe();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
}
Also used : Arrays(java.util.Arrays) Duration(org.awaitility.Duration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ReactiveStringRedisTemplate(org.springframework.data.redis.core.ReactiveStringRedisTemplate) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) Awaitility(org.awaitility.Awaitility) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with ReactiveSubscription

use of org.springframework.data.redis.connection.ReactiveSubscription in project redisson by redisson.

the class RedissonSubscribeReactiveTest method testSubscribe.

@Test
public void testSubscribe() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    ReactiveRedisConnection connection = factory.getReactiveConnection();
    Mono<ReactiveSubscription> s = connection.pubSubCommands().createSubscription();
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    ReactiveSubscription ss = s.block();
    ss.subscribe(ByteBuffer.wrap("test".getBytes())).block();
    ss.receive().doOnEach(message -> {
        msg.set(message.get().getMessage().array());
    }).subscribe();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
    Awaitility.await().atMost(Duration.ONE_SECOND).until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    ss.unsubscribe();
    connection.pubSubCommands().publish(ByteBuffer.wrap("test".getBytes()), ByteBuffer.wrap("msg".getBytes())).block();
}
Also used : Arrays(java.util.Arrays) Duration(org.awaitility.Duration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ReactiveStringRedisTemplate(org.springframework.data.redis.core.ReactiveStringRedisTemplate) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) Awaitility(org.awaitility.Awaitility) ReactiveRedisConnection(org.springframework.data.redis.connection.ReactiveRedisConnection) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 Arrays (java.util.Arrays)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Awaitility (org.awaitility.Awaitility)2 Duration (org.awaitility.Duration)2 Test (org.junit.Test)2 ReactiveRedisConnection (org.springframework.data.redis.connection.ReactiveRedisConnection)2 ReactiveSubscription (org.springframework.data.redis.connection.ReactiveSubscription)2 ReactiveStringRedisTemplate (org.springframework.data.redis.core.ReactiveStringRedisTemplate)2 ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)2 Mono (reactor.core.publisher.Mono)2