跳至主要内容

博文

目前显示的是标签为“jaxrs”的博文

JAX-RS 2.1: Server Sent Event

Server Sent Event Jersey itself supports SSE for years, now it is standardized as a part of JAXRS 2.1. A simple SSE example. @Path ( " events " ) @RequestScoped public class SseResource { @GET @Produces ( MediaType . SERVER_SENT_EVENTS ) public void eventStream ( @Context Sse sse , @Context SseEventSink eventSink ) { // Resource method is invoked when a client subscribes to an event stream. // That implies that sending events will most likely happen from different // context - thread / event handler / etc, so common implementation of the // resource method will store the eventSink instance and the application // logic will retrieve it when an event should be emitted to the client. // sending events: eventSink . send(sse . newEvent( " event1 " )); } } Notice, you should declare @Produces value as text/event-stream ( via MediaType.SERVER_SENT_EVENTS ). SseEve...

JAX-RS 2.1: Reactive Client

Reactive Client In JAXRS 2.0, a client to handle async resources looks like. public class AsyncClient { public final static void main ( String [] args ) throws Exception { WebTarget target = ClientBuilder . newClient() . target( " http://localhost:8080/jaxrs-async/rest/ejb " ); Future< String > future = target . request() .async() .get( String . class); System . out . println( " ejb resource future: " + future . get()); target . request() .async() .get( AsyncClient . responseInvocationCallback()); } private static InvocationCallback< Response > responseInvocationCallback () { return new InvocationCallback< Response > () { @Override public void completed ( Response res ) { System . out . println( " Status: " + res . getStatusInfo()); ...

JAX-RS 2.1: Async improvements

Async improvements In JAXRS 2.0, you can build an asynchronous RESTful APIs as the following. @GET public void getAsync( final @Suspended AsyncResponse res) { res . setTimeoutHandler( (ar) - > { ar . resume( Response . status( Response . Status . SERVICE_UNAVAILABLE ) .entity( " Operation timed out --- please try again. " ) . build()); } ); res . setTimeout( 1000 , TimeUnit . MILLISECONDS ); executor . submit(() - > { // do long run operations. try { LOG . log( Level . INFO , " execute long run task in AsyncResource " ); // Thread.sleep(new Random().nextInt(1005)); Thread . sleep( 500 ); } catch ( InterruptedException ex) { LOG . log( Level . SEVERE , " error : " + ex . getMessage()); } res . resume( Response . ok( " asynchronous resource " ) . build()); }); } Inject @Suspended AsyncResponse res as the method parameter, call res.resume() to return response result asynchrono...

What is new in Java EE8(by example)

A developer's notes about upgrading to Java EE 8 Java EE 8 brings a plenty of new features which are valuable to build modern applications. I have spent some time on updating myself to the newest Java EE 8 technology stack. This mini book is my learning notes when I refreshed my knowledge to Java EE 8. It will not cover the existing content in Java EE 7. If you are new to Java EE or need a comprehensive guide of Java EE, I suggest you read the official Java EE Tutorial carefully. This mini book will cover JSF 2.3, CDI 2.0, JSON-B, Java EE Secuirty API 1.0, Servlet 4.0, JAX-RS 2.1 etc. Sample codes All sample codes mentioned in this book can be found here . And the source codes of this book itself are also hosted on my github, check here . Read it online This book will be synchronized to Gitbook, go here to read it online. Contribution This is an open source book, if you have some suggestions or find some issues (even grammar errors, I am a non-English gu...