Modern, JVM-based framework for building modular, easily testable microservice and serverless applications with compile-time DI and fast startup.
—
Micronaut provides comprehensive reactive programming support built on Reactive Streams specification. The framework's reactive APIs enable non-blocking I/O operations, backpressure handling, and scalable application development.
HTTP controllers can return reactive types for non-blocking request processing.
/**
* Reactive HTTP controller
*/
@Controller("/stream")
public class StreamController {
@Get(value = "/events", produces = MediaType.TEXT_EVENT_STREAM)
Publisher<String> events() {
return Flowable.interval(1, TimeUnit.SECONDS)
.map(i -> "Event " + i);
}
@Get("/data")
Single<ResponseData> getData() {
return dataService.loadAsync();
}
@Post("/process")
Completable processData(@Body DataRequest request) {
return processingService.processAsync(request);
}
}Support for various reactive types from RxJava and other reactive libraries.
/**
* Publisher - 0 to N items
*/
Publisher<T> publisher();
/**
* Single - exactly one item
*/
Single<T> single();
/**
* Maybe - 0 or 1 item
*/
Maybe<T> maybe();
/**
* Completable - no items, completion signal only
*/
Completable completable();
/**
* Flowable - backpressure-aware publisher
*/
Flowable<T> flowable();
/**
* Observable - non-backpressure-aware
*/
Observable<T> observable();HTTP clients provide reactive APIs for non-blocking external service calls.
/**
* Reactive HTTP client operations
*/
@Client("/api")
public interface ReactiveClient {
@Get("/users/{id}")
Single<User> getUser(Long id);
@Get("/users")
Flowable<User> getUsers();
@Post("/users")
Single<User> createUser(@Body User user);
@Delete("/users/{id}")
Completable deleteUser(Long id);
}Support for streaming responses using Server-Sent Events.
/**
* Server-Sent Events controller
*/
@Controller("/sse")
public class EventController {
@Get(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM)
Publisher<Event> streamEvents() {
return Flowable.interval(1, TimeUnit.SECONDS)
.map(i -> Event.of("Event " + i))
.doOnNext(event -> event.id(String.valueOf(i)));
}
}
/**
* Event wrapper for SSE
*/
public final class Event {
public static Event of(Object data);
public Event id(String id);
public Event name(String name);
public Event retry(Duration retry);
public Event comment(String comment);
}Database operations can return reactive types for non-blocking data access.
/**
* Reactive repository interface
*/
@Repository
public interface ReactiveUserRepository {
Single<User> save(User user);
Maybe<User> findById(Long id);
Flowable<User> findAll();
Completable deleteById(Long id);
Single<Long> count();
}// Core reactive types
public interface Publisher<T> {
void subscribe(Subscriber<? super T> s);
}
public interface Subscriber<T> {
void onSubscribe(Subscription s);
void onNext(T t);
void onError(Throwable t);
void onComplete();
}
public interface Subscription {
void request(long n);
void cancel();
}
// Event for Server-Sent Events
public final class Event {
public static Event of(Object data);
public Event id(String id);
public Event name(String name);
public Event retry(Duration retry);
public Event comment(String comment);
}Install with Tessl CLI
npx tessl i tessl/maven-micronaut