sábado, 7 de diciembre de 2019

Reactive Spring MongoDB flux-flix-service

Spring Reactive MongoDB flux-flix-service


package com.bext.fluxflixservice;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Mono;

import java.util.Random;
import java.util.UUID;
import java.util.stream.Stream;

@SpringBootApplication
public class FluxFlixServiceApplication {


        public static void main(String[] args) {
        SpringApplication.run(FluxFlixServiceApplication.class, args);
    }

    @Bean
    CommandLineRunner demo(PeliculaRepository peliculaRepository) {
        return args -> {
            //Mono mono = Mono.just("peliculaRepository.count(): " );
            peliculaRepository.count().map(x->"Numero de Registros de Pelicula: "+ x ).subscribe(System.out::println);

            peliculaRepository.deleteAll().subscribe(null, null, () ->
                    Stream.of("Buscando a Nemo", "Ciudad de Dios", "Hable con ella",
                            "El Señor de los Anilllos", "Kandahar (2001)", "La mirada de Ulises", "Pulp Fiction", "Matrix")
                            .map( nombre -> new Pelicula(UUID.randomUUID().toString(), nombre, generoRandom()))
                            .forEach( peli -> peliculaRepository.save(peli).subscribe(System.out::println))
            );

        };
    }

    private String generoRandom() {
        String[] generos = "accion,terror,comedia,documental,arte".split(",");
        return generos[new Random().nextInt(generos.length)];
    }

}

interface PeliculaRepository extends ReactiveMongoRepository<Pelicula, String> {

}

@Document
@AllArgsConstructor
@ToString
@NoArgsConstructor
@Data
class Pelicula {
    @Id
    private String id;
    private String titulo , genero;
}

Run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.0.RELEASE)

2019-12-07 19:49:42.338  INFO 24896 --- [           main] c.b.f.FluxFlixServiceApplication         : Starting FluxFlixServiceApplication on DESKTOP-NLF0058 with PID 24896 (D:\proy\flux-flix-service\target\classes started by bext in D:\proy\flux-flix-service)
2019-12-07 19:49:42.339  INFO 24896 --- [           main] c.b.f.FluxFlixServiceApplication         : No active profile set, falling back to default profiles: default
2019-12-07 19:49:42.710  INFO 24896 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-12-07 19:49:42.749  INFO 24896 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 36ms. Found 1 repository interfaces.
2019-12-07 19:49:42.755  INFO 24896 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-12-07 19:49:42.758  INFO 24896 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 2ms. Found 0 repository interfaces.
2019-12-07 19:49:43.011  INFO 24896 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-12-07 19:49:43.107  INFO 24896 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-12-07 19:49:43.122  INFO 24896 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:63}] to localhost:27017
2019-12-07 19:49:43.126  INFO 24896 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 1]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1876100}
2019-12-07 19:49:43.156  WARN 24896 --- [           main] o.s.data.convert.CustomConversions       : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-12-07 19:49:43.156  WARN 24896 --- [           main] o.s.data.convert.CustomConversions       : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-12-07 19:49:43.197  WARN 24896 --- [           main] o.s.data.convert.CustomConversions       : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-12-07 19:49:43.197  WARN 24896 --- [           main] o.s.data.convert.CustomConversions       : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
2019-12-07 19:49:43.459  INFO 24896 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:64}] to localhost:27017
2019-12-07 19:49:43.461  INFO 24896 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 1]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1322099}
2019-12-07 19:49:43.472  INFO 24896 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2019-12-07 19:49:43.475  INFO 24896 --- [           main] c.b.f.FluxFlixServiceApplication         : Started FluxFlixServiceApplication in 1.331 seconds (JVM running for 1.903)
2019-12-07 19:49:43.534  INFO 24896 --- [ntLoopGroup-2-2] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:65}] to localhost:27017
Numero de Registros de Pelicula: 0
2019-12-07 19:49:43.541  INFO 24896 --- [ntLoopGroup-2-3] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:4, serverValue:66}] to localhost:27017
2019-12-07 19:49:43.583  INFO 24896 --- [ntLoopGroup-2-4] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:5, serverValue:67}] to localhost:27017
2019-12-07 19:49:43.591  INFO 24896 --- [ntLoopGroup-2-5] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:6, serverValue:68}] to localhost:27017
2019-12-07 19:49:43.598  INFO 24896 --- [ntLoopGroup-2-6] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:7, serverValue:69}] to localhost:27017
2019-12-07 19:49:43.600  INFO 24896 --- [ntLoopGroup-2-7] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:8, serverValue:70}] to localhost:27017
2019-12-07 19:49:43.607  INFO 24896 --- [ntLoopGroup-2-8] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:9, serverValue:71}] to localhost:27017
2019-12-07 19:49:43.614  INFO 24896 --- [ntLoopGroup-2-9] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:10, serverValue:72}] to localhost:27017
Pelicula(id=37516675-5439-4a9e-89d1-402f3a781a55, titulo=Kandahar (2001), genero=documental)
Pelicula(id=b045c87e-7a58-430a-9212-30bce5a1aa28, titulo=Pulp Fiction, genero=terror)
Pelicula(id=a251905e-30e9-4820-af74-b887615bd2f5, titulo=Matrix, genero=accion)
Pelicula(id=91203906-658a-49d1-991b-89e643e78c2c, titulo=Hable con ella, genero=documental)
Pelicula(id=3424fd82-d2c7-46bf-aa92-7425c4be06a4, titulo=La mirada de Ulises, genero=accion)
Pelicula(id=21b663d6-cc8b-4c6b-8057-b35162fd7a0b, titulo=Buscando a Nemo, genero=accion)
Pelicula(id=bfbf8ab7-3e2e-417b-a15e-dc6c465e7362, titulo=Ciudad de Dios, genero=documental)
Pelicula(id=68b655e9-7ae5-4c8d-8b72-ea32e501240a, titulo=El Señor de los Anilllos, genero=terror)


Exploring the content of MongoDB with Compass tool, the records are created





eot

No hay comentarios:

Publicar un comentario