Java EE 7 (Lyon JUG & Alpes JUG - March 2014)

Click here to load reader

  • date post

    08-May-2015
  • Category

    Technology

  • view

    1.810
  • download

    0

Embed Size (px)

Transcript of Java EE 7 (Lyon JUG & Alpes JUG - March 2014)

  • 1.Whats New in the Java EE Platform Alpes JUG - March 2014 ! David Delabassee @delabassee Software Evangelist - Oracle

2. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!2 Program Agenda A look at some of the important new features of Java EE 7 3. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!3 Java EE 7 Momentum ACTIVEPROJECTS 26 Active and transparent mailing lists JSRsADOPTED 22 JUGs 19 Adopt a JSR PROMOTEDBUILDS 89 GlassFish YOU 187 COMPANIES 32 EXPERTS SPECLEADS 16 ACTIVEJSRs 14 4. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!4 Java EE 7 Momentum 5. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!5 Java EE 7 Themes ENTERPRISE EDITION Batch Concurrency Simplified JMS More annotated POJOs Less boilerplate code Cohesive integrated platform DEVELOPER PRODUCTIVITY WebSockets JSON Servlet 3.1 NIO REST MEETING ENTERPRISE DEMANDS Java EE 7 6. Copyright 2014, Oracle and/or its affiliates. All rights reserved. Java EE 7 JSRs New or Updated JPA 2.1 JAX-RS 2.0 EJB 3.2 JMS 2.0 Servlet 3.1 EL 3.0 JSF 2.2 CDI 1.1 Bean Validation 1.1 WebSocket 1.0 JSON 1.0 Batch Applications 1.0 Concurrency Utilities 1.0 !6 7. Copyright 2014, Oracle and/or its affiliates. All rights reserved. Java EE 7 Maintenance Releases Common Annotations 1.2 JTA 1.2 Interceptors 1.2 Connector 1.7 JSP 2.3 JASPIC 1.2 JACC 1.4 JavaMail 1.5 Web Services 1.4 !7 8. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!8 JSON Processing 1.0 API to parse and generate JSON Streaming API (javax.json.stream) Low-level, efficient way to parse/generate JSON Similar to StAX API in XML world Object model API (javax.json) Simple, easy to use high-level API Similar to DOM API in XML world 9. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!9 JSON Processing 1.0 Created using Json.createParser()! Json.createParserFactory().createParser()! Parses JSON in streaming way from input sources Event event = parser.next(); // START_OBJECT! event = parser.next(); // KEY_NAME! event = parser.next(); // VALUE_STRING! Parser state events START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY, KEY_NAME, VALUE_STRING, VALUE_NUMBER, Streaming API: Parsing 10. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!10 JSON Processing 1.0 {! firstName:John,lastName: Smith,age: 25,! phoneNumber: [! {type: home, number: 212 555-1234 },! {type: fax, number: 646 555-4567 }! ]! } Streaming Parser 11. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!11 JSON Processing 1.0 {! firstName:John,lastName: Smith,age: 25,! phoneNumber: [! {type: home, number: 212 555-1234 },! {type: fax, number: 646 555-4567 }! ]! } Streaming Parser START_OBJECT 12. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!12 JSON Processing 1.0 {! firstName:John,lastName: Smith,age: 25,! phoneNumber: [! {type: home, number: 212 555-1234 },! {type: fax, number: 646 555-4567 }! ]! } Streaming Parser KEY_NAME 13. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!13 JSON Processing 1.0 {! firstName:John,lastName: Smith,age: 25,! phoneNumber: [! {type: home, number: 212 555-1234 },! {type: fax, number: 646 555-4567 }! ]! } Streaming Parser VALUE_STRING 14. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!14 JSON Processing 1.0 {! firstName:John,lastName: Smith,age: 25,! phoneNumber: [! {type: home, number: 212 555-1234 },! {type: fax, number: 646 555-4567 }! ]! } Streaming Parser VALUE_NUMBER 15. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!15 JSON Processing 1.0 {! firstName:John,lastName: Smith,age: 25,! phoneNumber: [! {type: home, number: 212 555-1234 },! {type: fax, number: 646 555-4567 }! ]! } Streaming Parser START_ARRAY 16. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!16 JSON Processing 1.0 {! firstName:John,lastName: Smith,age: 25,! phoneNumber: [! {type: home, number: 212 555-1234 },! {type: fax, number: 646 555-4567 }! ]! } Streaming Parser END_ARRAY 17. Copyright 2014, Oracle and/or its affiliates. All rights reserved. JSON Processing 1.0 JsonGenerator gen = Json.createGenerator! .writeStartObject()! .write("firstName", "John") ! .write("lastName", "Smith") ! .write("age", 25) ! .writeStartArray(phones)! .writeStartObject()! !! .write(type",home") ! .write(number, 222)! .writeEnd()! .writeStartObject() ! .writeEnd()! .writeEnd()! .writeEnd();! ! Using Streaming API to generate JSON ! { firstName:John,! lastName:Smith,! age:25, ! phones:[ ! {type:home,number:},! {type:fax,number:} ]! } !17 18. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!18 Interactive Web Sites Flavors of Server Push Polling Long polling AJAX ! ! Complex, inefficient, wasteful 19. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!19 WebSockets to the rescue Bidirectional full-duplex messaging Over a single TCP connection IETF defined protocol: RFC 6455 Part of HTML5 W3C defined JavaScript API Adoption http://caniuse.com/websockets Bring interactivity to the Web 20. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!20 WebSockets Handshake Request Http Request! GET /mychat HTTP/1.1! Host: server.example.com! Upgrade: websocket! Connection: Upgrade! Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==! Sec-WebSocket-Protocol: megachat, chat! Sec-WebSocket-Extensions : compress, mux! Sec-WebSocket-Version: 13! Origin: http://example.com 21. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!21 WebSockets Handshake Response Http Response! HTTP/1.1 101 Switching Protocols! Upgrade: websocket! Connection: Upgrade! Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=! Sec-WebSocket-Protocol: chat! Sec-WebSocket-Extensions: compress, mux 22. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!22 Java API for WebSocket 1.0 WebSocket Java client & server API Part of Java EE 7 Annotation-based & interface-based programming model Under consideration for Java SE 9 Reference Implementation http://tyrus.java.net Supported in GlassFish 4.0 JSR 356 23. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!23 Java API for WebSocket 1.0 Establish a WebSocket connection Send messages backwards and forwards End the connection Whats the basic idea? 24. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!24 Java API for WebSocket 1.0 Session Represents the active conversation Endpoint Intercepts websocket lifecycle events RemoteEndpoint Represents the other end of the conversation MessageHandler Handles incoming messages for endpoint Main API classes 25. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!25 WebSocket Endpoints public class MyClient extends Endpoint {! public void onOpen(Session session, EndpointConfig ec) {! session.addMessageHandler(new MessageHandler.Whole() {! public void onMessage(String payload) {! System.out.println("From the server : " + payload);! }! }! session.getBasicRemote().sendText("Hello!");! }! ! public void onClose(Session session, CloseReason closeReason) {! super.onClose(session, closeReason);! }! } Programmatic API 26. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!26 WebSocket Endpoints as POJOs @ClientEndpoint! public class MyClient { ! @OnOpen! public void onOpen(Session session) {! session.getBasicRemote().sendText(Hello);! }! @OnMessage! public void onMessage(String payload, Session session) { ! System.out.println("From the server : " + payload);! } ! } Annotated client endpoint 27. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!27 WebSocket Endpoints as POJOs @ServerEndpoint(/chat")! public class ChatServer { !! static Set peers = Collections.synchronizedSet();!!! ! ! ! @OnOpen! !!! public void onOpen(Session peer) {! peers.add(peer);! }! ! @OnMessage! !!! public void onMessage (String message, Session client) { ! for (Session peer : peers) {! peer.getBasicRemote().sendObject(message);! } ! }! ! @OnClose! !!! public void onClose(Session peer) {! peers.remove(peer);! }! } Annotated server endpoint 28. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!28 Java API for WebSocket 1.0 Payload Text Binary Ping-Pong Synchronous / Asynchronous Whole message / Sequence of partial messages No delivery guarantee Messaging 29. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!29 Java API for WebSocket 1.0 Use Sessions RemoteEndpoint interface mySession.getBasicRemote().sendText(Hello); ! Return a value from @OnMessage method @OnMessage! public String echo(String incomingMessage) {! return(Hello);! } Sending messages 30. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!30 Java API for WebSocket 1.0 Means of sending RemoteEndpoint method to use as whole string sendText(String message) as binary data sendBinary(ByteBuffer message) in string fragments sendText(String part, boolean last) in binary data fragments sendBinary(ByteBuffer part, boolean last) as a blocking stream of text Writer getSendWriter()) as a blocking stream of binary data OutputStream getSendStream() as a custom object of type T sendObject(T customObject) 31. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!31 Java API for WebSocket 1.0 Annotation - @onMessage @OnMessage! public void whenGettingText(String message)! ! Implement a MessageHandler, add it to the Session MyMessageHandler implements MessageHandler.Whole! ! session.addMessageHandler( new MyMessageHandler() ); Receiving messages 32. Copyright 2014, Oracle and/or its affiliates. All rights reserved.!32 Java API for WebSocket 1.0 Encoder Object to Binary: Encoder.Binary, Encoder.BinaryStream! Object to Text: Encoder.Text, Encoder.TextStream! Decoder Text to Object: Decoder.Text, Decoder.