Why doesn't work and block subscription happen in Spring Jet Mongo?

I've created a project from Spring Initializr , by Kotlin, Gradle, M7and Web-reactive.

I did a small project:

data class Person (val id: String)

@Component class PersonHandler(val template: ReactiveMongoTemplate) 
{
    init
    {
        println("Initializing")

        val jim: Mono<Person> =  template.save(Person("Jim"))
        val john: Mono<Person> = template.save(Person("John"))
        val jack: Mono<Person> = template.save(Person("Jack"))

        launch(jim)
        launch(john)
        launch(jack)

        println("Finished Initializing")
    }

    fun launch(mono: Mono<Person>)
    {
        mono.subscribe({println(it.id)}, {println("Error")}) // This works
        // mono.block()  This just hangs
    } 
}

I am trying to save three people in a database. The method savereturns only Monowhich needs to be executed. If I try to execute it by simply signing up, everything will be fine:

Initializing
Finished Initializing
2017-12-21 13:14:39.513  INFO 17278 --- [      Thread-13] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:158}] to localhost:27017
2017-12-21 13:14:39.515  INFO 17278 --- [      Thread-12] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:4, serverValue:159}] to localhost:27017
2017-12-21 13:14:39.520  INFO 17278 --- [      Thread-14] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:5, serverValue:160}] to localhost:27017
Jim
Jack
John

However, when I use blockinstead subscribe, the application freezes:

Initializing
2017-12-21 13:16:47.200  INFO 17463 --- [      Thread-14] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:163}] to localhost:27017

If I query the database manually, I see that Jim is saved, but not Jack and John.

, - ? , , , block.

, ,

template

. . .

https://github.com/martin-drozdik/spring-mongo-bug-example

+4
1

, / Spring Framework.

-, subscribe block:

  • subscribe . , , , .
  • block - : .

, , :

val jim: Mono<Person> =  template.save(Person("Jim"))
val john: Mono<Person> = template.save(Person("John"))
val jack: Mono<Person> = template.save(Person("Jack"))
jim.then(john).then(jack).block();

, block . , Spring. , , .

( Java/ Spring Boot/Spring Data Reactive Mongo) , https://jira.spring.io?

+2

Source: https://habr.com/ru/post/1691182/


All Articles