#DevoxxUS
JAX-RS 2.1 Reloaded
Santiago Pericas-Geertsen
JAX-RS Co-Spec Lead
#jax-rs @spericas
#DevoxxUS
Agenda
•  Reactive Extensions
•  Server-Sent Events
•  Non-Blocking IO
#jax-rs @spericas
#DevoxxUS
Reactive Extensions
#jax-rs @spericas
#DevoxxUS
Asynchronous Processing in 2.0
Server side:
•  Using @Suspended and AsyncResponse
•  Resume execution on a different thread
Client side:
•  Future<T>
•  InvocationCallback<T>
4
#DevoxxUS
Example Using Future<T>
5
What’s the
problem
here?
Client client = ClientBuilder.newClient();
WebTarget target = client.target(“http://…”);
Future<String> f =
target.request().async().get(String.class);
// some time later …
String user = f.get();
#DevoxxUS
Example using InvocationCallback<T>
6
WebTarget target = client.target(“http://…”);
target.request().async().get(
new InvocationCallback<String>() {
@Override
public void completed(String user) {
// do something
}
@Override
public void failed(Throwable t) {
// do something
} });
#DevoxxUS
Example using InvocationCallback<T>
7
target1.request().async().get(new
InvocationCallback<String>() {
public void completed(String user) {
target2.request().header(“user”, user).async().get(
new InvocationCallback<String>() {
public void completed(String quote) {
System.out.println(quote);
}
public void failed(Throwable t) {
// do something
}
}); }
public void failed(Throwable t) {
// do something
}}));
#DevoxxUS
Some use cases for Async Computations
•  Compose two or more asynchronous tasks
•  Combine the output of two or more asynchronous tasks
•  Consume value of asynchronous task
•  Wait for all tasks in a collection to complete
•  Wait for any of the tasks in a collection to complete
And many more …
#DevoxxUS
Meet CompletionStage<T> in JAX-RS
9
CompletionStage<String> cs1 =
target1.request().rx().get(String.class);
CompletionStage<String> cs2 =
cs1.thenCompose(user ->
target2.request().header(“user”, user)
.rx().get(String.class));
cs2.thenAccept(quote -> System.out.println(quote));
#DevoxxUS
What about other Rx libraries?
10
Client client =
client.register(ObservableRxInvokerProvider.class);



Observable<String> of = 

client.target("forecast/{destination}”)
.resolveTemplate("destination", "mars”)
.request()
.rx(ObservableRxInvoker.class)
.get(String.class);
of.subscribe(System.out::println);
Register a
Provider
Override
default Invoker
#DevoxxUS
Server-Sent Events Summary
•  New invoker to support Rx
•  Default support for CompletionStage
•  Extension mechanism for other Rx libraries
11
#DevoxxUS
Server-Sent Events
#jax-rs @spericas
#DevoxxUS
Server-Sent Events
•  Originally W3C (HTML5), now maintainted by WHATWG
•  HTTP-based protocol for one-way server to client messaging
•  Special media type text/event-stream
13
#DevoxxUS
Client API
14
try (SseEventSource source =
SseEventSource.target(“http://…”).build()) {
source.subscribe(System.out::println);
source.open();

Thread.sleep(500); 

} catch (InterruptedException e) {

// falls through

}
Publisher
#DevoxxUS
Server API
15
@GET

@Produces(“text/event-stream”)

public void eventStream(
@Context SseEventSink eventSink,

@Context Sse sse) {
executor.execute(() -> {
try (SseEventSink sink = eventSink) {

eventSink.onNext(sse.newEvent("event1"));
eventSink.onNext(sse.newEvent("event2"));
eventSink.close();
} catch (IOException e) {

// handle exception

} });}
Subscriber
#DevoxxUS
Broadcasting (1 of 2)
16
@Path("/")

@Singleton
public class SseResource {
private final Sse sse;
private final SseBroadcaster sseBroadcaster;
public SseResource(@Context Sse sse) {
this.sse = sse;
this.sseBroadcaster = sse.newBroadcaster();
}

…
One Publisher
Lifecycle
controlled by
App
#DevoxxUS
Broadcasting (2 of 2)
17
@GET @Path(“subscribe”)
@Produces(MediaType.SERVER_SENT_EVENTS)
public void subscribe(@Context SseEventSink eventSink) {
eventSink.onNext(sse.newEvent("welcome!"));
sseBroadcaster.subscribe(eventSink);
}

@POST @Path(“broadcast”)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void broadcast(@FormParam("event") String event) {

sseBroadcaster.broadcast(sse.newEvent(event));
} … }
Many
Subscribers
#DevoxxUS
Reactive Extensions Summary
•  New types SseEventSink, SseEventSource, Sse and
SseBroadcaster
•  Sse and SseEventSink’s lifecycle controlled by runtime
•  Singleton scope useful for broadcasting
18
#DevoxxUS
Non-Blocking IO
#jax-rs @spericas
#DevoxxUS
Motivation
•  Certain apps need more control over IO
•  Higher throughput is hard with blocking IO
•  Precedence with StreamingOutput
20
#DevoxxUS
StreamingOutput in JAX-RS
21
@GET
public Response get() {
return Response.ok(new StreamingOutput() {
@Override
public void write(OutputStream out) throws … {
out.write(…);
}
}).build();
}
Direct access
to stream
Still blocking!
#DevoxxUS
First NIO Proposal
22
return Response.ok().entity(

out -> {

try {

final int n = in.read(buffer);

if (n >= 0) {

out.write(buffer, 0, n);

return true; // more to write

}

in.close();

return false; // we're done

} catch (IOException e) { … }

}).build();
Write handler
#DevoxxUS
First Proposal Limitations
•  Byte streams are not sufficient in JAX-RS
•  We want POJOs!
•  What about JAX-RS readers, writers and interceptors?
•  Poor integration with other third-party libraries and APIs
23
#DevoxxUS
Current Proposal: Flows
•  Standardized in Java 9
•  Originaly appeared in Reactive Streams
•  Not just bytes, POJOs too
•  Possibility of integration with third-party libraries and APIs
24
#DevoxxUS
Simple Concepts
25
Publisher
Subscriber
Processor
Publisher
Subscriber
0
N
Processor
#DevoxxUS
Why and when to NIO?
•  Non-blocking code tends to be more involved
•  Beneficial for large payloads
•  Large payloads often involve collections
•  In Flow terms, a collection of Pojos is a Publisher<Pojo>
26
#DevoxxUS
Processing a collection
27
@POST
@Consumes(“application/json”)
void process(Publisher<Pojo> pojos,
@Suspended AsyncResponse response) {
pojos.subscribe(new Subscriber<Pojo> {
public onNext(Pojo pojo) {
// process single pojo
}
public onComplete() {
return response.ok().build();
}
…
} }
Asynchronous
by nature
#DevoxxUS
MessageBodyReader for Pojo?
•  MessageBodyReader for Pojo or Publisher<Pojo>?
•  We need a new type of Reader: NioBodyReader
•  Knows how to process a collection of Pojos
•  May use MessageBodyReader for each Pojo
•  (Ditto for MessageBodyWriters …)
28
#DevoxxUS
Pojo Processor in REST
29
@POST
@Consumes(“application/json”)
@Produces(“application/json”)
Publisher<Pojo> process(Publisher<Pojo> pojos,
@Suspended AsyncResponse response) {
Processor<Pojo> pp = new MyProcessor(response);
pojos.subscribe(pp);
return pp;
}
Processors are
publishers!
#DevoxxUS
Third-party Libraries
30
@GET
@Produces(“application/json”)
Publisher<Pojo> process(
@QueryParam(“predicate”) String predicate) {
return DB.get(Pojo.class).filter(predicate);
}
Third-party
Library
#DevoxxUS
What about Filters and Interceptors?
•  Need special support for NIO
•  For example, additional methods and contexts
•  Discussion still ongoing
•  May impact how NioBodyReader/NioBodyWriter are defined
31
#DevoxxUS
And NIO Clients?
•  Will be supported as well
•  Need re-usability of readers and writers for NIO
•  Likely using a new client Invoker
32
#DevoxxUS
NIO Client
33
WebTarget resource = target(“…”).path(“pojos”);
Publisher<Pojo> pojos =
resource.request().nio().get(Pojo.class);
pojos.subscribe(new Subscriber<Pojo> {
public onNext(Pojo pojo) {
// process single pojo
}
…
});
NioBodyReader
#DevoxxUS
Tentative Vocabulary
•  Publisher = Source
•  Subscriber = Sink
•  Does this ring a bell?
34
#DevoxxUS
SSE is a special case of Flow!
•  Where what flows are SSE protocol messages
•  Initial SSE proposal may be impacted
•  Return a Source instead of injecting a Sink
35
#DevoxxUS
Non-Blocking IO Summary
•  Based on Flows
•  New readers/writers for collections
•  Support for Flows without depending on Java 9
•  Support for Pojos, not just byte streams
•  Publisher<Pojo> and Publisher<ByteArray>
36
#DevoxxUS
Public Release In Progress
•  Support for NIO and Flows
•  Some minor changes to SSE possible
•  Stay tuned!
37

More Related Content

PDF
Introduction to Node.js Platform
PDF
the Spring 4 update
PPTX
Java EE 8
PDF
50 features of Java EE 7 in 50 minutes at JavaZone 2014
PDF
Spring Boot Revisited with KoFu and JaFu
PDF
50 features of Java EE 7 in 50 minutes at Geecon 2014
PPTX
Node js for enterprise
PDF
Micronaut For Single Page Apps
Introduction to Node.js Platform
the Spring 4 update
Java EE 8
50 features of Java EE 7 in 50 minutes at JavaZone 2014
Spring Boot Revisited with KoFu and JaFu
50 features of Java EE 7 in 50 minutes at Geecon 2014
Node js for enterprise
Micronaut For Single Page Apps

What's hot (20)

PPTX
Play + scala + reactive mongo
PDF
Servlet sessions
ODP
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
PDF
Getting Started with WebSockets and Server-Sent Events
PDF
Simple REST with Dropwizard
PDF
Lecture 7 Web Services JAX-WS & JAX-RS
PDF
Asynchronous web apps with the Play Framework 2.0
PPTX
Why Play Framework is fast
PDF
WebLogic in Practice: SSL Configuration
PDF
The Making of the Oracle R2DBC Driver and How to Take Your Code from Synchron...
PPTX
Dropwizard Internals
PDF
Spring.io
PDF
Deployment of WebObjects applications on FreeBSD
PDF
Spring4 whats up doc?
PDF
The First Contact with Java EE 7
PDF
Building Reactive Microservices with Vert.x
PDF
Microservices in Scala: Play Framework
PDF
Grizzly 20080925 V2
PDF
Se lancer dans l'aventure microservices avec Spring Cloud - Julien Roy
Play + scala + reactive mongo
Servlet sessions
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Getting Started with WebSockets and Server-Sent Events
Simple REST with Dropwizard
Lecture 7 Web Services JAX-WS & JAX-RS
Asynchronous web apps with the Play Framework 2.0
Why Play Framework is fast
WebLogic in Practice: SSL Configuration
The Making of the Oracle R2DBC Driver and How to Take Your Code from Synchron...
Dropwizard Internals
Spring.io
Deployment of WebObjects applications on FreeBSD
Spring4 whats up doc?
The First Contact with Java EE 7
Building Reactive Microservices with Vert.x
Microservices in Scala: Play Framework
Grizzly 20080925 V2
Se lancer dans l'aventure microservices avec Spring Cloud - Julien Roy
Ad

Similar to JAX-RS 2.1 Reloaded @ Devoxx (20)

ODP
RESTing with JAX-RS
PDF
112815 java ee8_davidd
PDF
IPT Reactive Java IoT Demo - BGOUG 2018
PPTX
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
PDF
Con fess 2013-sse-websockets-json-bhakti
PDF
JAX RS and CDI bike the reactive bridge
PDF
JAX-RS.next
PDF
JAX RS 2.0 - OTN Bangalore 2013
PPTX
Async servers and clients in Rest.li
PDF
reactive_programming_for_java_developers.pdf
PDF
Reactive programming with Pivotal's reactor
PDF
Intro to Reactive Programming
PPTX
Don't Wait! Develop Responsive Applications with Java EE7 Instead
PPTX
Architectural Patterns - Interactive and Event Handling Patterns
PDF
Intro To Reactive Programming
PPTX
Asynchronous Web Programming with HTML5 WebSockets and Java
PPT
Don't Wait! Develop responsive applications with Java EE7 instead
PDF
Java EE 7 (Lyon JUG & Alpes JUG - March 2014)
PPTX
Essential API Facade Patterns: Synchronous to Asynchronous Conversion (Episod...
PDF
Web Technologies in Java EE 7
RESTing with JAX-RS
112815 java ee8_davidd
IPT Reactive Java IoT Demo - BGOUG 2018
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
Con fess 2013-sse-websockets-json-bhakti
JAX RS and CDI bike the reactive bridge
JAX-RS.next
JAX RS 2.0 - OTN Bangalore 2013
Async servers and clients in Rest.li
reactive_programming_for_java_developers.pdf
Reactive programming with Pivotal's reactor
Intro to Reactive Programming
Don't Wait! Develop Responsive Applications with Java EE7 Instead
Architectural Patterns - Interactive and Event Handling Patterns
Intro To Reactive Programming
Asynchronous Web Programming with HTML5 WebSockets and Java
Don't Wait! Develop responsive applications with Java EE7 instead
Java EE 7 (Lyon JUG & Alpes JUG - March 2014)
Essential API Facade Patterns: Synchronous to Asynchronous Conversion (Episod...
Web Technologies in Java EE 7
Ad

Recently uploaded (20)

PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PPTX
Tech Workshop Escape Room Tech Workshop
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PPTX
CNN LeNet5 Architecture: Neural Networks
PDF
MCP Security Tutorial - Beginner to Advanced
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PPTX
Introduction to Windows Operating System
PDF
Time Tracking Features That Teams and Organizations Actually Need
PPTX
Trending Python Topics for Data Visualization in 2025
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Website Design Services for Small Businesses.pdf
Weekly report ppt - harsh dattuprasad patel.pptx
Tech Workshop Escape Room Tech Workshop
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Monitoring Stack: Grafana, Loki & Promtail
Wondershare Recoverit Full Crack New Version (Latest 2025)
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
DNT Brochure 2025 – ISV Solutions @ D365
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
CNN LeNet5 Architecture: Neural Networks
MCP Security Tutorial - Beginner to Advanced
Computer Software and OS of computer science of grade 11.pptx
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Introduction to Windows Operating System
Time Tracking Features That Teams and Organizations Actually Need
Trending Python Topics for Data Visualization in 2025
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Website Design Services for Small Businesses.pdf

JAX-RS 2.1 Reloaded @ Devoxx