Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/reactive data redis #242

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions data/data-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<module>sample-data-mysql-book</module>
<module>sample-data-mysql-reactive-book</module>
<module>sample-data-redis-book</module>
<module>sample-data-redis-reactive-book</module>
</modules>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
#
spring.data.redis.database=0
spring.data.redis.host=localhost
spring.data.redis.port=16379
spring.data.redis.password=password
spring.data.redis.port=6379
spring.data.redis.password=
spring.data.redis.timeout=60000
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class BookDataConfigTest {
private RedisServer redisServer;

public BookDataConfigTest(Environment environment) {
this.redisServer = new RedisServer(Integer.parseInt(environment.getRequiredProperty("spring.redis.port")));
this.redisServer = new RedisServer(Integer.parseInt(environment.getRequiredProperty("spring.data.redis.port")));
}

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
* {@code BookDataTestConfig} class contains configurations for tests.
*/
@Configuration
@Import({BookDataConfig.class})
@Import({BookDataConfigTest.class})
public class BookDataTestConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class BookRepositoryIT {
private BookRepository bookRepository;

@Test
public void save_givenValidBook_expectedSavedBookSuccess() {
UUID id = UUID.randomUUID();
void save_givenValidBook_expectedSavedBookSuccess() {
BookEntity bookEntity = new BookEntity("Alice Wonderland", "Lewis Carroll");
BookEntity saved = bookRepository.save(bookEntity);
assertNotNull(saved);
Expand Down
38 changes: 38 additions & 0 deletions data/data-samples/sample-data-redis-reactive-book/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
<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>
<parent>
<artifactId>data-samples</artifactId>
<groupId>io.americanexpress.synapse</groupId>
<version>0.3.14-SNAPSHOT</version>
</parent>

<artifactId>sample-data-redis-reactive-book</artifactId>

<dependencies>
<!-- Synapse dependencies -->
<dependency>
<groupId>io.americanexpress.synapse</groupId>
<artifactId>synapse-data-redis</artifactId>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
</dependency>
<!-- Third party dependencies -->
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* 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 io.americanexpress.data.book.config;

import io.americanexpress.data.book.entity.BookEntity;
import io.americanexpress.synapse.data.redis.config.BaseReactiveRedisDataConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* {@code BookDataConfig} is the configuration class to load all the properties for the book data module.
*/
@Configuration
@PropertySource("classpath:data-book-application.properties")
@ComponentScan(basePackages = BookDataConfig.PACKAGE_NAME)
@EnableRedisRepositories(basePackages = BookDataConfig.PACKAGE_NAME)
public class BookDataConfig extends BaseReactiveRedisDataConfig {

/**
* The Package name.
*/
static final String PACKAGE_NAME = "io.americanexpress.data.book";

/**
* The {@link BookDataConfig} overloaded constructor.
* @param environment the environment
*/
public BookDataConfig(Environment environment) {
super(environment);
}

/**
* Overriding method to configure redis template for serialization/deserialization for key, value mapping of String and {@link BookEntity} type.
*
* @param factory the redis connection factory
* @return the reactive redis template specifically for <Stirng, BookEntity>
*/
@Override
public ReactiveRedisTemplate<String, BookEntity> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
return new ReactiveRedisTemplate<>(factory, getSerializationContext());
}

/**
* Overriding method to provide configuration for serialization/deserialization of {@link BookEntity}.
*
* @return the redis serialization context
*/
@Override
protected RedisSerializationContext<String, BookEntity> getSerializationContext() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can we simply this

return RedisSerializationContext
.<String, BookEntity>newSerializationContext(new StringRedisSerializer())
.key(new StringRedisSerializer())
.value(new GenericToStringSerializer<>(BookEntity.class))
.hashKey(new StringRedisSerializer())
.hashValue(new GenericJackson2JsonRedisSerializer())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* 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 io.americanexpress.data.book.entity;

import io.americanexpress.synapse.data.redis.entity.BaseEntity;
import org.springframework.data.redis.core.RedisHash;

/**
* {@code BookEntity} class represents the domain of the books table.
*/
@RedisHash("books")
public class BookEntity extends BaseEntity {

/**
* The title.
*/
private String title;

/**
* The author.
*/
private String author;

/**
* Instantiates a new Book entity.
*
* @param title the title
* @param author the author
*/
public BookEntity(String title, String author) {
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
this.title = title;
this.author = author;
}

/**
* Instantiates a new Book entity.
*/
public BookEntity() {}
whoswendy marked this conversation as resolved.
Show resolved Hide resolved

/**
* Gets title.
*
* @return the title
*/
public String getTitle() {
return title;
}

/**
* Sets title.
*
* @param title the title
*/
public void setTitle(String title) {
this.title = title;
}

/**
* Gets author.
*
* @return the author
*/
public String getAuthor() {
return author;
}

/**
* Sets author.
*
* @param author the author
*/
public void setAuthor(String author) {
this.author = author;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* 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 io.americanexpress.data.book.repository;

import io.americanexpress.data.book.entity.BookEntity;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.UUID;

/**
* {@code BookRepository} is used to access redis store by using {@link ReactiveRedisOperations}.
* Spring-data-redis does not provide built in reactive repository so need to create access methods using ReactiveRedisOperations.
*/
@Repository
public class BookRepository {
whoswendy marked this conversation as resolved.
Show resolved Hide resolved

/**
* Used to access data in redis store.
*/
private final ReactiveRedisOperations<String, BookEntity> reactiveRedisOperations;

/**
* The constant BOOKS.
*/
private static final String BOOKS = "books";

/**
* Instantiates a new Book repository.
*
* @param reactiveRedisOperations the reactive redis operations
*/
public BookRepository(ReactiveRedisOperations<String, BookEntity> reactiveRedisOperations) {
this.reactiveRedisOperations = reactiveRedisOperations;
}

/**
* Find by title.
*
* @param title the title
* @return the mono BookEntity if book exists with title
*/
public Mono<BookEntity> findByTitle(String title) {
return reactiveRedisOperations.<String, BookEntity>opsForHash()
.values(BOOKS)
.filter(book -> book.getTitle().equals(title))
.singleOrEmpty();
}

/**
* Save bookEntity to redis store.
*
* @param book the bookEntity
* @return the mono BookEntity if saved successfully
*/
public Mono<BookEntity> save(BookEntity book) {
if (book.getIdentifier() == null) {
String id = UUID.randomUUID().toString();
book.setIdentifier(id);
}
return reactiveRedisOperations.<String, BookEntity>opsForHash().put(BOOKS, book.getIdentifier(), book)
.log()
.map(p -> book);

}

/**
* Find by id.
*
* @param id the id of the bookEntity in redis store.
* @return the mono BookEntity if id found
*/
public Mono<BookEntity> findById(String id) {
return reactiveRedisOperations.<String, BookEntity>opsForHash().get(BOOKS, id);
}

/**
* Find all
*
* @return the flux of BookEntity
*/
public Flux<BookEntity> findAll() {
return this.reactiveRedisOperations.<String, BookEntity>opsForHash().values(BOOKS);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
# or implied. See the License for the specific language governing permissions and limitations under
# the License.
#
#spring.redis.host=localhost
#spring.redis.port=6370
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
spring.data.redis.database=0
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=
spring.data.redis.timeout=60000
11 changes: 5 additions & 6 deletions data/synapse-data-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@

<dependencies>
<!-- Spring Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
<version>2.7.5</version>
</dependency>
<!-- Third Party Dependencies -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
<type>jar</type>
</dependency>
</dependencies>

Expand Down
Loading