Skip to content

Commit

Permalink
Rename mongo repository to reactive mongo repository
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-tarjanyi committed May 6, 2018
1 parent 96b3faa commit 3efd798
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.webfluxdemo;

import com.example.webfluxdemo.model.Person;
import com.example.webfluxdemo.repository.MongoRepository;
import com.example.webfluxdemo.repository.ReactiveMongoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
Expand Down Expand Up @@ -45,19 +45,19 @@ public CommandLineRunner initRedis(ReactiveRedisTemplate<String, String> reactiv
}

@Bean
public CommandLineRunner savePeopleToMongo(MongoRepository mongoRepository)
public CommandLineRunner savePeopleToMongo(ReactiveMongoRepository reactiveMongoRepository)
{
return args ->
{
mongoRepository.deleteAll().thenMany(mongoRepository.saveAll(Set.of(
reactiveMongoRepository.deleteAll().thenMany(reactiveMongoRepository.saveAll(Set.of(
new Person(1, "Jack", 45, "Oklahoma"),
new Person(2, "John", 24, "Chicago"),
new Person(6, "Smith", 11, "London"),
new Person(5, "Rose", 39, "Kuala Lumpur"),
new Person(7, "Alexander", 44, "Amsterdam"),
new Person(3, "Jane", 32, "Brian"),
new Person(4, "Brian", 48, "New York"))))
.subscribe(person -> log.info("Saved person: " + person));
.subscribe(person -> log.info("Saved person: " + person));
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.webfluxdemo.controller;

import com.example.webfluxdemo.model.Person;
import com.example.webfluxdemo.repository.MongoRepository;
import com.example.webfluxdemo.repository.ReactiveMongoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.http.HttpStatus;
Expand All @@ -22,12 +22,12 @@
public class PersonController
{
private final ReactiveRedisTemplate<String, String> reactiveRedisTemplate;
private final MongoRepository mongoRepository;
private final ReactiveMongoRepository reactiveMongoRepository;

public PersonController(ReactiveRedisTemplate<String, String> reactiveRedisTemplate, MongoRepository mongoRepository)
public PersonController(ReactiveRedisTemplate<String, String> reactiveRedisTemplate, ReactiveMongoRepository reactiveMongoRepository)
{
this.reactiveRedisTemplate = reactiveRedisTemplate;
this.mongoRepository = mongoRepository;
this.reactiveMongoRepository = reactiveMongoRepository;
}

@GetMapping(value = "/slow")
Expand All @@ -44,14 +44,14 @@ public Flux<Person> getPersonsByStringNumber(@RequestParam Set<String> numbers)
return Flux.fromIterable(numbers)
.flatMap(number -> reactiveRedisTemplate.opsForValue().get(number).doOnNext(log::info))
.map(Integer::valueOf)
.flatMap(id -> mongoRepository.findById(id).doOnNext(person -> log.info(person.toString())))
.flatMap(id -> reactiveMongoRepository.findById(id).doOnNext(person -> log.info(person.toString())))
.switchIfEmpty(Mono.error(new IllegalArgumentException("Not found person.")));
}

@GetMapping(value = "/people-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@GetMapping(value = "/people-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) // Server Sent Events (SSE)
public Flux<Person> getAllAsStream()
{
return mongoRepository.findAll().delayElements(Duration.ofMillis(1000));
return reactiveMongoRepository.findAll().delayElements(Duration.ofMillis(1000));
}

@ExceptionHandler(IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.example.webfluxdemo.model.Person;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Mono;

public interface MongoRepository extends ReactiveCrudRepository<Person, Integer>
public interface ReactiveMongoRepository extends ReactiveCrudRepository<Person, Integer>
{
Mono<Person> findById(int id);
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
spring.data.mongodb.host=localhost
spring.redis.host=localhost
spring.data.mongodb.host=192.168.99.100
spring.redis.host=192.168.99.100

0 comments on commit 3efd798

Please sign in to comment.