Skip to content

A simple framework to enable non-blocking on spring rest controllers and services

License

Notifications You must be signed in to change notification settings

dondragon2/spring-nonblocking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-nonblocking

A simple framework to enable non-blocking on spring rest controllers and services

Installation:


Maven

<dependency>
    <groupId>io.github.dondragon2</groupId>
    <artifactId>spring-nonblocking</artifactId>
    <version>0.1.2</version>
</dependency>

Gradle

compile group: 'io.github.dondragon2', name: 'spring-nonblocking', version: '0.1.2'

Usage:


There are 2 ways of using spring-nonblocking

Annotation (recommended)

@RestController
public class ExampleResource {
    
    @Autowired
    private ExampleService exampleService;

    /*
        Note: return type must be "Object" to avoid ClassCastException
    */
    @NonBlocking
    @GetMapping("/greetings-non-blocking")
    public Object greeting() {
        return this.exampleService.longProcess();     	        
    }
    

    @GetMapping("/greetings-blocking")
    public String greetingBlocking() {   
        return this.exampleService.longProcess();   	        
    }
}

@Service
public class ExampleService {
    public String longProcess() {
        //simulate long running process
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
        }
        return "Hello World"; 
    }
}

Inheritance

@RestController
public class ExampleResource {
    
    @Autowired
    private ExampleService exampleService;

    @GetMapping("/greetings-non-blocking")
    public Observable greeting() {
        return this.exampleService.nonBlockingProcess();     	        
    }
    
    @GetMapping("/greetings-blocking")
    public String greetingBlocking() {   
        return this.exampleService.longProcess();   	        
    }
}

@Service
public class ExampleService extends NonBlockingService {
    public Observable nonBlockingProcess() {
        return this.process(() -> longProcess());
    }
    
    public String longProcess() {
        //simulate long running process
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
        }
        return "Hello World";
    }
}

About

A simple framework to enable non-blocking on spring rest controllers and services

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages