Git Product home page Git Product logo

spring-boot-async's Introduction

Build Status

spring-boot-async

The main goal of this project is to explore basic features of @Async spring annotation and compare results with non-concurrent approach.

References: https://spring.io/guides/gs/async-method/
References: http://www.baeldung.com/spring-async

preface

This project shows how to create asynchronous queries using Spring.

Our approach is to run expensive jobs in the background and wait for the results using Java’s CompletableFuture interface.

manual

  1. Enable asynchronous support:

    @Configuration
    @EnableAsync
    class AsyncConfig {
        @Bean
        Executor asyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(4);
            executor.setMaxPoolSize(4);
            executor.setQueueCapacity(500);
            executor.setThreadNamePrefix("EmailSender-");
            executor.initialize();
            return executor;
        }
    }
    

    Remark: asyncExecutor() is used to customize default behaviour and is not mandatory.

  2. Annotate method with @Async, and set return type to CompletableFuture<XXX> where XXX is a wanted return type, for example:

    String customMethod() {
    ...
    }
    

    should be transformed to:

    CompletableFuture<String> customMethod() {
    ...
    }    
    
  3. Consume it with Completable API, for example:

    • CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[]{})).join();
    • CompletableFuture.allOf(completableFuture1, completableFuture2).join();
  4. Extract requested return (note that get() throws checked exception):

    • XXX xxx = completableFuture.join()

project description

  • email-service - microservice responsible for sending emails to given users
    • EmailController - REST controller; receives login-message map, then asks slow-user-service for emails and sends messages
    • in EmailService we have the same methods:
      • @Async method - asyncSend - concurrently sends messages
      • non-concurrent method - send
    • in AppRunner we simulate interactions and compare times
  • slow-user-service
    • UserController returns User bean (login, name, email...) for given login
    • in UserRepository we sleep thread for user.repository.delay.seconds (configurable in application.properties) and then return requested user

tests

Coverage: 93%

spring-boot-async's People

Contributors

mtumilowicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.