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 @@ -14,6 +14,7 @@
package io.americanexpress.data.book.config;

import io.americanexpress.synapse.data.redis.config.BaseRedisDataConfig;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
Expand Down
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,67 @@
/*
* 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);
}

@Override
public ReactiveRedisTemplate<String, BookEntity> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
return new ReactiveRedisTemplate<>(factory, getSerializationContext());
}

@Override
protected RedisSerializationContext<String, BookEntity> getSerializationContext() {
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
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,71 @@
/*
* 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;

/**
* The number of copies.
*/
private int numberOfCopies;

public BookEntity(String title, String author) {
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
this.title = title;
this.author = author;
}

public BookEntity() {}
whoswendy marked this conversation as resolved.
Show resolved Hide resolved

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public int getNumberOfCopies() {
return numberOfCopies;
}

public void setNumberOfCopies(int numberOfCopies) {
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
this.numberOfCopies = numberOfCopies;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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;

@Repository
public class BookRepository {
whoswendy marked this conversation as resolved.
Show resolved Hide resolved

private final ReactiveRedisOperations<String, BookEntity> reactiveRedisOperations;

private final String BOOKS = "books";
whoswendy marked this conversation as resolved.
Show resolved Hide resolved

public BookRepository(ReactiveRedisOperations<String, BookEntity> reactiveRedisOperations) {
this.reactiveRedisOperations = reactiveRedisOperations;
}

public Mono<BookEntity> findByTitle(String title) {
return reactiveRedisOperations.<String, BookEntity>opsForHash()
.values(BOOKS)
.filter(book -> book.getTitle().equals(title))
.singleOrEmpty();
}
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);

}

public Mono<BookEntity> findById(String id) {
return reactiveRedisOperations.<String, BookEntity>opsForHash().get(BOOKS, id);
}

public Flux<BookEntity> findAll() {
return this.reactiveRedisOperations.<String, BookEntity>opsForHash().values(BOOKS);
}
whoswendy marked this conversation as resolved.
Show resolved Hide resolved
}

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