Example of using coroutines with Spring MVC

Overview

This is an example of using coroutines with Spring MVC (not webflux!)

DemoController uses https://github.com/joost-de-vries/spring-coroutine, but since that library was published to bintray, I copied the class SpringCoroutineScope and dependencies here.

The /boomerang/{x} endpoint waits for x seconds and then returns x back. If x is 0 then it throws an exception.

Looking at the implementation I suspected that, by reusing the same scope instance in a Spring bean, if one of the requests fails it would cancel the parent job and other parallel or subsequent requests would also fail.

This can be reproduced by changing back:

@Suppress("FunctionName")
fun SpringScope(dispatcher: CoroutineDispatcher = Dispatchers.Default, job: Job = SupervisorJob()): SpringScope =
    SpringScope(dispatcher + job)

to

@Suppress("FunctionName")
fun SpringScope(dispatcher: CoroutineDispatcher = Dispatchers.Default, job: Job = Job()): SpringScope =
    SpringScope(dispatcher + job)

Obviously, using a SupervisorJob fixes this.

Here is a use case that reproduces the aforementioned problem:

#running the app:
./gradlew clean run

#let's do some requests
curl http://localhost:8080/boomerang/1

curl http://localhost:8080/boomerang/10

#if we call it in less than 10 seconds, the previous request will also fail
curl http://localhost:8080/boomerang/0
#this also fails
curl http://localhost:8080/boomerang/10
You might also like...
grpc stream fullstack example(spring+kotlin / next.js + typescript)

grpc-stream-fullstack chat application build with grpc named qhat Prerequisites server sync .proto files on src/main/proto/grpc/qhat/ $ ./gradlew sync

Spring boot kotlin example

Spring Boot Kotlin Example Gradle 配置 代理配置 当内网环境需要代理时,在项目根目录添加文件 gradle.propertie

Microservice-grpc-multi-language-example - gRPC communication on multiple language demonstration (spring kotlin, go, .NET core 6) 🎓 Learning Kotlin Coroutines for Android by example. 🚀 Sample implementations for real-world Android use cases. 🛠 Unit tests included!
🎓 Learning Kotlin Coroutines for Android by example. 🚀 Sample implementations for real-world Android use cases. 🛠 Unit tests included!

Kotlin Coroutines - Use Cases on Android 🎓 Learning Kotlin Coroutines for Android by example. 🚀 Sample implementations for real-world Android use ca

A sample skeleton backend app built using Spring Boot kotlin, Expedia Kotlin Graphql, Reactive Web that can be deployed to Google App Engine Flexible environmennt

spring-kotlin-gql-gae This is a sample skeleton of a backend app that was built using: Spring Boot(Kotlin) Reactive Web Sprinng Data R2DBC with MYSQL

API for a library using Kotlin, Spring-boot and covered by test

Library API This API is to create Books and be able to borrow from them I'm using: Spring Boot version 2.5.6 Kotlin 1.5.31 Java 11 I'm implementing us

Push notification application using One Signal + Spring Boot + Kotlin

Spring Boot push notification system using One Signal This project was created to demonstrate a simple application built with Kotlin + Spring Boot inc

In this Repo i create public apis to serve apps, like muslim apps using Spring, kotlin, and microservices

spring-freelance-apis-kotlin In this Repo i create public apis to serve apps, like muslim apps using Spring, kotlin, and microservices This repo for l

A generic library for quick development in Spring Boot using Java Generics.
A generic library for quick development in Spring Boot using Java Generics.

slinky Now in Kotlin! A generic library for quick development in Spring Boot using Java Generics. Why "slinky"? A slinky is a precompressed helical sp

Owner
Tiberiu Tofan
Tiberiu Tofan
Spring-graphql-getting-started - Spring for GraphQL provides support for Spring applications built on GraphQL Java

Getting Started with GraphQL and Spring Boot Spring for GraphQL provides support

Shinya 0 Feb 2, 2022
An implementation of MediatR on JVM for Spring using Kotlin coroutines

Kpring MediatR In this project, an attempt has been made to implement the mediator pattern on the JVM with simplicity using Kotlin with native corouti

Mahdi Bohloul 4 Aug 6, 2022
Simple Todo list API using Spring-Boot, Webflux, Kotlin, Coroutines, & PostgreSQL

Simple Todo list API using Spring-Boot, Webflux, Kotlin, Coroutines, & PostgreSQL

Sami Eljabali 9 Jul 26, 2022
This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

Maksim Kostromin 2 Nov 29, 2021
Spring-with-maven - Spring Boot App with Postgresql and maven

Spring Boot Api Aplikasi ini dibuat menggunakan bahasa kotlin dan untuk database

Aldi Aulia Rosyad 1 Jan 12, 2022
Reactive setup with Spring WebFlux , Kotlin, Postgres and Spring Data R2DBC

Reactive Spring with Kotlin and Pg Spring WebFlux with Netty instead of Spring Web with Tomcat Mono and Flux in all layers (controller, service, repo)

Bimal Raj Gyawali 7 Dec 9, 2022
Spring-kotlin - Learning API Rest with Kotlin, Spring and PostgreSQL

Kotlin, Spring, PostgreSQL and Liquibase Database Migrations Learning Kotlin for

Andre L S Ferreira 2 Feb 14, 2022
Android login spring - Android login against spring backend

Android Jetpack Compose login implementation with JWT tokens against our own bac

null 1 Feb 13, 2022
Spring Boot Example with Kotlin and Domain Driven Design

Spring Boot Kotlin Domain Driven Design Example Just an example project where ddd is implemented with some other great patterns and architecture from

Fernando Guevara Sanchez 16 Jun 29, 2022
Spring for Kotlin Example

Spring for Kotlin Example This project is for studying the spring using by kotlin. Required docker-compose Running project docker-compose -f docker-co

null 0 Nov 10, 2021