Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

20
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 1

description

JAX-RS/WebSocket lab at JavaOne Latin America 2012

Transcript of Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Page 1: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.1

Page 2: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.2

Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocketReza Rahman/Arun Gupta/Bruno BorgesJava EE/GlassFish Evangelists

Page 3: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 4: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4

Program Agenda

About This Lab

Quick Intro to the Technologies Used

Lab Exercises

Getting Started

Resources

Page 5: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5

About This Lab

Follow the lab guide Exercises are self-paced Raise your hand if you get stuck – we are here to help To get most of the lab try to understand the code, don’t just blindly

copy-paste

Page 6: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6

JAX-RS 2/Jersey

Java API for RESTful Web Services– Annotation-based API for RESTful web services

New in JAX-RS 2– Client API

– Filters/interceptors

– Server-side content negotiation

– Asynchronous processing

Description

Page 7: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7

JAX-RS 2/JerseyClient API

// Get instance of Client

Client client = ClientFactory.newClient();

// Get account balance

String bal = client.target("http://.../atm/{cardId}/balance")

.pathParam("cardId", "111122223333")

.queryParam("pin", "9876")

.request("text/plain").get(String.class);

Page 8: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8

JAX-RS 2/Jersey

JavaOne sessions:– JAX-RS 2: New and Noteworthy in the RESTful Web Services API

Reza Rahman Keynote Hall - Thursday, 3 PM

On the web:– Specification project: http://jax-rs-spec.java.net

– Implementation project: http://jersey.java.net

– Twitter: @gf_jersey

Where to get more info

Page 9: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.9

Java API for WebSocket

Annotation-based API for utilizing WebSocket protocol in Java web applications

– Planned to be part of Java EE 7

Allows defining WebSocket endpoints– Handling onOpen, onClose, onError and onMessage events

– Bi-directional communication between peers

Support for encoders/decoders to map message content to/from Java objects

Description

Page 10: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.10

Java API for WebSocket

@WebSocketEndpoint("/echo")

public class EchoBean {

@WebSocketMessage

public String echo(String message) {

System.out.println("Message received: " + message);

return message + " (from your server)";

}

}

Example – Simple Endpoint

Page 11: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11

Java API for WebSocket

@WebSocketEndpoint(”/drawing/”, decoders = ShapeCoding.class, encoders = ShapeCoding.class,)public class DrawingWebSocket { @WebSocketMessage public void shapeCreated(Shape shape, Session session) { … } }

public class ShapeCoding implements Decoder.Text<Shape>, Encoder.Text<Shape> { public Shape decode(String s) throws DecodeException { … } public boolean willDecode(String s) { … } public String encode(Shape object) throws EncodeException { … }}

Example – Decoder/Encoder

Page 12: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12

Java API for WebSocket

JavaOne Sessions– HTML5 WebSocket and Java, Arun Gupta

Thursday, 10 AM, Mezanino: Sala 12

On The Web– Specification Project: http://websocket-spec.java.net

– Implementation: http://tyrus.java.net

Where to get more information

Page 13: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13

Java API for JSON Processing

Streaming API to produce/consume JSON– Similar to StAX API in XML world

Object model API to represent JSON– Similar to DOM API in XML world

Aligns with Java EE 7 schedules

JSR-353

Page 14: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14

JSR-353: Java API for JSON Processing

JsonReader – reads JsonObject/JsonArray from i/o

JsonReader/JsonWriter

try(JsonReader reader = new JsonReader(io)) {

JsonObject jsonObj = reader.readObject();

}

JsonWriter – writes JsonObject/JsonArray to i/o

try(JsonWriter writer = new JsonWriter(io)) {

writer.writeObject(jsonObj);

}

Page 15: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15

Resources

Projects– Specification Project - http://json-processing-spec.java.net

– RI Project - http://jsonp.java.net

Latest Javadoc– http://json-processing-spec.java.net/nonav/releases/1.0/edr/javadocs/

index.html

Page 16: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16

Lab Exercises

Drawing Board web application:– Exercise 1: Exposing RESTful API ~ 30 minutes

– Exercise 2: Adding Server-Sent Events ~ 30 minutes

– Exercise 3: Adding WebSockets ~ 30 minutes

Simple Drawing Board client (optional):– Exercise 4: Implementing a Simple Java Client

Page 17: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17

Getting Started

Launch HOL4461 virtual machine in VirtualBox (if not already started) Lab files installed under:

– C:\hol-sse-websocket-master

Open lab-guide.pdf Follow the instructions

Page 18: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18

Additional Resources

GitHub project with this lab – https://github.com/jersey/hol-sse-websocket

Jersey – http://jersey.java.net Tyrus – http://tyrus.java.net JSON Processing – http://jsonp.java.net

Page 19: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.19

Graphic Section Divider

Page 20: Developing JAX-RS Web Applications Utilizing Server-Sent Events and WebSocket

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.20