#215 - Add reactive examples for MongoDB, Apache Cassandra and Redis.
This commit is contained in:
committed by
Oliver Gierke
parent
cf5d9f3562
commit
20879e7fa3
8
redis/reactive/README.md
Normal file
8
redis/reactive/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Spring Data Redis 2.0 - Reactive examples
|
||||
|
||||
This project contains samples of reactive data access features with Spring Data (Redis).
|
||||
|
||||
## Prerequisites
|
||||
|
||||
This project contains samples of specific features of Spring Data Redis using reactive infrastructure.
|
||||
|
||||
49
redis/reactive/pom.xml
Normal file
49
redis/reactive/pom.xml
Normal file
@@ -0,0 +1,49 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-redis-reactive</artifactId>
|
||||
<name>Spring Data Redis - Reactive support</name>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-redis-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spring-data-releasetrain.version>Kay-BUILD-SNAPSHOT</spring-data-releasetrain.version>
|
||||
<spring.version>5.0.0.M3</spring.version>
|
||||
<reactor.version>3.0.3.RELEASE</reactor.version>
|
||||
<lettuce.version>5.0.0.Beta1</lettuce.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>biz.paluch.redis</groupId>
|
||||
<artifactId>lettuce</artifactId>
|
||||
<version>${lettuce.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-redis-example-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
* 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 example.springdata.redis;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class RedisTestConfiguration {
|
||||
|
||||
@Autowired RedisConnectionFactory factory;
|
||||
|
||||
@Bean
|
||||
public RedisConnectionFactory redisConnectionFactory() {
|
||||
return new LettuceConnectionFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear database before shut down.
|
||||
*/
|
||||
public @PreDestroy void flushTestDb() {
|
||||
factory.getConnection().flushDb();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
* 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 example.springdata.redis.commands;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.connection.ReactiveListCommands.PopResult;
|
||||
import org.springframework.data.redis.connection.ReactiveRedisConnection;
|
||||
import org.springframework.data.redis.connection.ReactiveStringCommands.SetCommand;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import example.springdata.redis.RedisTestConfiguration;
|
||||
import example.springdata.redis.test.util.RequiresRedisServer;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Show usage of reactive operations on Redis keys using low level API provided by {@link RedisConnectionFactory}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = RedisTestConfiguration.class)
|
||||
public class KeyOperationsTests {
|
||||
|
||||
// we only want to run this tests when redis is up an running
|
||||
public static @ClassRule RequiresRedisServer requiresServer = RequiresRedisServer.onLocalhost();
|
||||
|
||||
private static final String PREFIX = KeyOperationsTests.class.getSimpleName();
|
||||
private static final String KEY_PATTERN = PREFIX + "*";
|
||||
|
||||
@Autowired RedisConnectionFactory connectionFactory;
|
||||
|
||||
private ReactiveRedisConnection connection;
|
||||
private RedisSerializer<String> serializer = new StringRedisSerializer();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.connection = connectionFactory.getReactiveConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses {@code KEYS} command for loading all matching keys. <br />
|
||||
* Note that {@code KEYS} is a blocking command that potentially might affect other operations execution time. <br />
|
||||
* All keys will be loaded within <strong>one single</strong> operation.
|
||||
*/
|
||||
@Test
|
||||
public void iterateOverKeysMatchingPrefixUsingKeysCommand() {
|
||||
|
||||
generateRandomKeys(50);
|
||||
|
||||
this.connection.keyCommands() //
|
||||
.keys(ByteBuffer.wrap(serializer.serialize(KEY_PATTERN))) //
|
||||
.flatMap(Flux::fromIterable) //
|
||||
.doOnNext(byteBuffer -> System.out.println(toString(byteBuffer))) //
|
||||
.count() //
|
||||
.doOnSuccess(count -> System.out.println(String.format("Total No. found: %s", count))) //
|
||||
.block();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses {@code RPUSH} to store an item inside a list and {@code BRPOP} <br />
|
||||
*/
|
||||
@Test
|
||||
public void storeToListAndPop() {
|
||||
|
||||
Mono<PopResult> popResult = this.connection.listCommands()
|
||||
.brPop(Collections.singletonList(ByteBuffer.wrap("list".getBytes())), Duration.ofSeconds(5));
|
||||
|
||||
Mono<Long> llen = this.connection.listCommands().lLen(ByteBuffer.wrap("list".getBytes()));
|
||||
|
||||
this.connection.listCommands() //
|
||||
.rPush(ByteBuffer.wrap("list".getBytes()), Collections.singletonList(ByteBuffer.wrap("item".getBytes())))
|
||||
.flatMap(l -> popResult) //
|
||||
.doOnNext(result -> System.out.println(toString(result.getValue()))) //
|
||||
.flatMap(result -> llen) //
|
||||
.doOnNext(count -> System.out.println(String.format("Total items in list left: %s", count))) //
|
||||
.then() //
|
||||
.block();
|
||||
}
|
||||
|
||||
private void generateRandomKeys(int nrKeys) {
|
||||
|
||||
Flux<String> keyFlux = Flux.range(0, nrKeys).map(i -> (PREFIX + "-" + i));
|
||||
|
||||
Flux<SetCommand> generator = keyFlux.map(String::getBytes).map(ByteBuffer::wrap) //
|
||||
.map(key -> SetCommand.set(key) //
|
||||
.value(ByteBuffer.wrap(UUID.randomUUID().toString().getBytes())));
|
||||
|
||||
this.connection.stringCommands() //
|
||||
.set(generator) //
|
||||
.then() //
|
||||
.block();
|
||||
|
||||
}
|
||||
|
||||
private static String toString(ByteBuffer byteBuffer) {
|
||||
|
||||
byte[] bytes = new byte[byteBuffer.remaining()];
|
||||
byteBuffer.get(bytes);
|
||||
return new String(bytes);
|
||||
}
|
||||
}
|
||||
1
redis/reactive/src/test/resources/application.properties
Normal file
1
redis/reactive/src/test/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
logging.level.root=WARN
|
||||
Reference in New Issue
Block a user