Java EE 7 for WebLogic 12c Developers

2,806 views

Published on

WebLogic 12c (12.1.3) already bundles some Java EE 7 APIs with commercial support. Mostly for modern

0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,806
On SlideShare
0
From Embeds
0
Number of Embeds
89
Actions
Shares
0
Downloads
101
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Java EE 7 for WebLogic 12c Developers

  1. 1. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Java EE 7 for WebLogic 12c Developers An overview of supported APIs in 12.1.3 and other features Bruno Borges Principal Product Manager and Java Evangelist Oracle Latin America August, 2014
  2. 2. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Bruno Borges – Principal Product Manager, Java Evangelist – Oracle Latin America – @brunoborges – bruno.borges@oracle.com Speaker
  3. 3. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 4 Java Enterprise Edition Platform Java EE 7 DEVELOPER PRODUCTIVITY Java EE 7 – Batch – Concurrency – Simplified JMS – More annotated POJOs – Less boilerplate code – Cohesive integrated platform – WebSockets – JSON – Servlet 3.1 NIO – REST MEETING ENTERPRISE DEMANDS
  4. 4. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 5 Java EE 7 APIs JAX-RS 2.0 JSON-P 1.0 Web Socket 1.0Servlet 3.1 JSF 2.2EL 3.0 JSP JSTL BeanValidation1.1 Interceptors1.2 CDI1.1 Concurrency1.0 JPA 2.1 JTA 1.2 EJB 3.2 JMS 2.0 Batch 1.0 JCA 1.7 JavaMail 1.5
  5. 5. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | WebLogic Server 12.1.3 Mobile, Developer Productivity WLS 12.1.3Clients HTML5 clients ADF Mobile Proxies OTD Apache OHS Web Sockets (JSR 356) TopLink Data Services Server-Sent Events JAX-RS 2.0 WebSocket Emulation WebSocket Emulation JAX-RS 2.0, WebSocket 1.0 JSON Processing API 1.0 JPA 2.1 Server-Sent Events WebSocket Emulation JPA-RS JPA Change Notification Database JSON Programming API HTTP/S, JSON/XML WebSocket, Server-Sent Events, Long polling Java EE 7 APIs Additional WebLogic Value-Add Oracle Confidential – Internal/Restricted/Highly Restricted 6
  6. 6. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 8 APIs supported by WebLogic 12.1.3 Java EE 7 APIs JAX-RS 2.0 JSON-P 1.0 Web Socket 1.0 JPA 2.1 Enabled by Default in WebLogic 12.1.3 Deployed as Shared Library PRE_CLASSPATH Adjustment/Patch
  7. 7. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Enabling JAX-RS 2.0 in WebLogic 12.1.3 • JAX-RS 2.0 – Defaults to JAX-RS 1.1 (Java EE 6) – Deploy jax-rs-2.0.war as shared library – Reference it from weblogic.xml in applications Deployed as Shared Library <weblogic-web-app> <library-ref> <library-name>jax-rs</library-name> <specification-version>2.0</specification-version> </library-ref> </weblogic-web-app> $> java weblogic.Deployer -username weblogic -password welcome1 -library –deploy -targets AdminServer,Cluster1,DynClusterA $MW_HOME/wlserver/common/deployable-libraries/jax-rs-2.0.war
  8. 8. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Enabling JPA 2.1 in WebLogic 12.1.3 • JPA 2.1 – Defaults to JPA 2.0 (Java EE 6) – By adjusting PRE_CLASSPATH variable inside commEnv.sh • Or apply patch 17814796 (MOS; OPatch) – Application’s persistence.xml descriptor must set JPA version = 2.1 Adjusted PRE_CLASSPATH PRE_CLASSPATH=$MW_HOME/oracle_common/modules/javax .persistence_2.1.jar:$MW_HOME/wlserver/modules/com.oracl e.weblogic.jpa21support_1.0.0.0_2-1.jar $> opatch apply $PATH_TO_PATCH_17814796
  9. 9. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Java Persistence API 2.1 Enhanced version of standard Java API for Object Relational Mapping
  10. 10. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JPA 2.1 • Schema Generation – javax.persistence.schema-generation.* properties • Unsynchronized Persistence Contexts • Bulk update/delete using Criteria • User-defined functions using FUNCTION • Stored Procedure Query
  11. 11. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JPA 2.1 – Schema Generation <persistence ... version="2.1"> <persistence-unit ...> <properties> <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> <property name="javax.persistence.schema-generation.scripts.create-target" value="create.sql"/> <property name="javax.persistence.sql-load-script-source" value="insert.sql"/> </properties> </persistence-unit>
  12. 12. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JPA 2.1 – Unsynchronized Context • Decide if/when data must be written to the database @PersistenceContext(synchronization = SynchronizationType.UNSYNCHRONIZED) private EntityManager em; ... em.persist(book); ... em.joinTransaction();
  13. 13. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JPA 2.1 – Bulk Update/Delete • Bulk Update – CriteriaUpdate<Employee> q = cb.createCriteriaUpdate(Employee.class); Root<Employee> emp = q.from(Employee.class); .set(e.get(Employee_.salary), cb.prod(e.get(Employee_.salary), 1.1f)) .set(e.get(Employee_.status), "full_time“) .where(cb.lt(emp.get(Emploee_.salary), 10000)); • Bulk Delete – CriteriaDelete<PhoneNumber> q = cb.createCriteriaDelete(PhoneNumber.class); Root<PhoneNumber> p = q.from(PhoneNumber.class); q.where(cb.equal(p.get(PhoneNumber_.status), "out_of_service");
  14. 14. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JPA 2.1 – Define Named Stored Procedures @Entity @NamedStoredProcedureQuery(name = "archiveOldBooks", procedureName = "sp_archive_books", parameters = { @StoredProcedureParameter(name = ”date", mode = IN, type = Date.class), @StoredProcedureParameter(name = "warehouse", mode = IN, type = String.class) }) public class Book {...}
  15. 15. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JPA 2.1 – Call Named Stored Procedures StoredProcedureQuery query = em.createNamedStoredProcedureQuery("ReadAddressById"); query.setParameter("P_ADDRESS_ID", 12345); List<Address> result = query.getResultList();
  16. 16. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JPA 2.1 – Call SQL Functions from JPQL SELECT e FROM Employee e WHERE FUNCTION(‘isLongTermEmployee’, e.startDate)
  17. 17. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSON Processing API 1.0 Standard Java API for processing JSON structures
  18. 18. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSON-P 1.0 • API to parse and generate JSON • Streaming API – Low-level, efficient way to parse/generate JSON – Provides pluggability for parsers/generators • Object Model API – Simple, easy-to-use high-level API – Implemented on top of Streaming API • Binding JSON to Java objects forthcoming Standard API for JSON Processing
  19. 19. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSON-P 1.0 "phoneNumber": [ { "type": "home", "number": ”408-123-4567” }, { "type": ”work", "number": ”408-987-6543” } ] Example of JSON structure
  20. 20. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSON-P 1.0 JsonGenerator jg = Json.createGenerator(...); jg.writeStartArray("phoneNumber“) .writeStartObject() .write("type", "home“) .write("number", "408-123-4567“) .writeEnd() .writeStartObject() .write("type", ”work“) .write("number", "408-987-6543“) .writeEnd() .writeEnd(); jg.close(); Example of Object Model API for JSON-P
  21. 21. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSON-P 1.0 { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] } Iterator<Event> it = parser.iterator(); Event event = it.next(); // START_OBJECT event = it.next(); // KEY_NAME event = it.next(); // VALUE_STRING String name = parser.getString(); // "John” Example of Stream API for JSON-P
  22. 22. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JAX-RS 2.0 A Standard Java API to build RESTful services and clients
  23. 23. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JAX-RS – Java API for RESTful Web Services 2.0 • Client API • Message Filters and Entity Interceptors • Asynchronous Processing – Server and Client • Common Configuration
  24. 24. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Client API for JAX-RS 2.0 // Get instance of Client Client client = ClientBuilder.newClient(); // Get customer name for the shipped products String name = client.target(“../orders/{orderId}/customer”) .resolveTemplate(”orderId", ”10”) .queryParam(”shipped", ”true”) .request() .get(String.class);
  25. 25. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Async Client API for JAX-RS 2.0 Future<String> future = ClientBuilder.newClient() .target("http://www.foo.com/book") .request() .async() .get(String.class); try { String body = future.get(1, TimeUnit.MINUTES); } catch (InterruptedException | ExecutionException e) {...}
  26. 26. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Async Server API for JAX-RS 2.0 @Path("/async") public class AsyncResource { @GET public void asyncGet(final @Suspended AsyncResponse asyncResp) { new Thread(new Runnable() { public void run() { String result = veryExpensiveOperation(); asyncResp.resume(result); } }).start(); }}
  27. 27. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Java API for WebSocket 1.0 Developing WebSocket Applications using the Java Standard
  28. 28. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | WebSocket Overview
  29. 29. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | WebSocket Overview The WebSocket API in HTML5 • Enables use of WebSocket in HTML5/web applications • Open/Close connections • Send/Receive messages • Fully asynchronous • Event driven, listen and respond to events
  30. 30. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | WebSocket Overview Browser Support for WebSocket API •Broad base of support for WebSocket in modern browsers •Firefox 28+, Chrome 33+, Safari 7+, IE 10+ •Including mobile browsers http://caniuse.com/#search=websocket
  31. 31. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | The WebSocket Protocol (RFC 6455) Protocol Overview • Defines handshake and data transfer • Introduces new URI schemes – ws-URI = “ws:” “//” host [:port] path [ “?” query ] – wss-URI = “wss:” “//” host [:port] path [ “?” query ] – # MUST be escaped as %23 (URI fragments are meaningless) • Imposes an Origin Security Model • Supports extensibility – Tunnel other protocol by supporting sub-protocols, extend protocol features by supporting extensions
  32. 32. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | The WebSocket Protocol Opening Handshake WebLogicServer12.1.3 HTTP
  33. 33. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | The WebSocket Protocol Protocol Upgrade WebLogicServer12.1.3 WebSocket
  34. 34. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | The WebSocket Protocol Keep Alive: Ping and Pong Ping Frame Pong Frame Ping Frame Pong Frame WebLogicServer12.1.3
  35. 35. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | The WebSocket Protocol Closing Handshake WebLogicServer12.1.3 Close Frame Data Frame Data Frame Data Frame Close Frame
  36. 36. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Developing WebSocket Applications using the Java Standard
  37. 37. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 • Creating a WebSocket requires a Java class that acts as an Endpoint – Participate in lifecycle: establish connections with peers, handle errors, close connections – Send and receive messages • Endpoints can be created from: – Annotation based approach using WebSocket Annotations, @ServerEndpoint – Programmatic API where Java class extends WebSocket API class • Endpoints can be defined as: – Clients which connect to one server only at a time – Servers which many clients can connect to at any time
  38. 38. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Annotated ServerEndpoint Example import javax.websocket.OnMessage; import javax.websocket.server.ServerEndpoint; @ServerEndpoint("/echo") public class EchoServer { @OnMessage public String echo(String incomingMessage) { return String.format( "I got this (%s) message so I am sending it back!”, incomingMessage); } }
  39. 39. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Annotated ServerEndpoint Example @ServerEndpoint("/echo") public class EchoServer { @OnMessage public void onMessage(Session session, String message) { session.getOpenSessions() .forEach(s -> s.getBasicRemote().sendText(message)); } }
  40. 40. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Annotated Path Parameter Example @ServerEndpoint("/users/{username}") public class UserEndpoint { @OnMessage public String handle(String message, @PathParam("username") String user) { return “message “ + message + “ from user “ + user; } }
  41. 41. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Programmatic Endpoint Example public class ProgrammaticEchoServer extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig endpointConfig) { mySession.addMessageHandler(new MessageHandler.Whole<String>() { public void onMessage(String text) { try { mySession.getBasicRemote() .sendText(“I got this (“ + text + “)” + “so I am sending it back !”); } catch (IOException ioe) { } } }); } }
  42. 42. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Responding to Lifecycle Events Annotation Programmatic API Open Event @OnOpen public void onOpen(Session session, EndpointConfig config) Message Event @OnMessage • Create instance of relevant javax.websocket.MessageHandler • Register with Session Error Event @OnError public void onError(Session session, Throwable throwable) Close Event @OnClose public void onClose(Session session, CloseReason reason)
  43. 43. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Receiving Message from Peers • Annotate a suitable method with @OnMessage • Implement an appropriate MessageHandler interface • Add it to the Session @OnMessage Void handleText(String message) @OnMessage public void handlePartialText( String message, boolean isLast) SimpleHandler implements MessageHandler.Whole<String> { ... } session.addMessageHandler( new SimpleHandler());
  44. 44. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Receiving Message from Peer with @OnMessage Message Type Annotated Method Form Text public void handleText(String message) public void handleReader(Reader r) public void handleTextPieces(String message, boolean isLast) Binary public void handleBinary(ByteBuffer bytebuf) public void handleStream(InputStream is) public void handleBinaryPieces(ByteBuffer bytebuf, boolean isLast) Any Object public void handleObject(CustomObject co)
  45. 45. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Receiving Message Programmatically from Peers Message Type Message Handler Interface Text MessageHandler.Whole<String> MessageHandler.Whole<Reader> MessageHandler.Partial<String> Binary MessageHandler.Whole<ByteBuffer> MessageHandler.Partial<ByteBuffer> MessageHandler.Whole<InputStream> Any Object MessageHandler.Whole<CustomObject>
  46. 46. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Programmatic Endpoint Example public class ProgrammaticEchoServer extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig endpointConfig) { mySession.addMessageHandler(new MessageHandler.Whole<String>() { public void onMessage(String text) { try { mySession.getBasicRemote() .sendText(“I got this (“ + text + “)” + “so I am sending it back !”); } catch (IOException ioe) { } } }); } }
  47. 47. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Asynchronous and Synchronous Sending Modes Mode Usage Interface and Method Synchronous • Most commonly use mode • Send methods block until transmission of message is complete RemoteEndpoint.Basic void sendText(String message) Asynchronous • Send methods return immediately before transmission • Future and Callback options for completion handling RemoteEndpoint.Async Future sendText(String message) void sendText(String message, SendHandler handler)
  48. 48. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Example Encoder public class AuctionMessageEncoder implements Encoder.Text<AuctionMessage> { @Override public String encode(AuctionMessage object) throws EncodeException { JsonObject json = null; switch (object.getType()) { case AuctionMessage.AUCTION_TIME_RESPONSE: json = Json.createObjectBuilder() .add("type", object.getType()) .add("communicationId", object.getCommunicationId()) .add("data", (int) object.getData()).build(); break; } return json.toString(); }
  49. 49. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Example Decoder public class AuctionMessageDecoder implements Decoder.Text<AuctionMessage> { @Override public AuctionMessage decode(String s) { JsonReader jsonReader = Json.createReader(new StringReader(s)); JsonObject json = jsonReader.readObject(); return new AuctionMessage(json.getString("type"), json.getString("communicationId"), json.getString("data")); } @Override public boolean willDecode(String s) { return s.contains(AuctionMessage.BID_REQUEST) || s.contains(AuctionMessage.AUCTION_LIST_REQUEST) || s.contains(AuctionMessage.LOGIN_REQUEST)); }
  50. 50. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Configuring Encoders and Decoders • Annotation • Specify encoder and decoder values on @ServerEndpoint • Programmatic • Add encoder/decoder classes to ServerEndpointConfig object @ServerEndpoint( decoders = { AuctionMessageDecoder.class }, encoders = { AuctionMessageEncoder.class } ) List<Encoder> encoders = new List<>(); encoders.add(AuctionMessageEncoder.class); encoders.add(AuctionMessageDecoder.class); ServerEndpointConfig config = ServerEndpointConfig.Builder .create(AuctionEndpoint.class, "/bids”) .encoders(encoders) .build();
  51. 51. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JSR-356: Java API for WebSocket 1.0 Uses Servlet Security model - Example of secured WebSocket in web.xml <security-constraint> <web-resource-collection> <web-resource-name>Secure Customer WebSocket Endpoint</web-resource-name> <url-pattern>/customer/feed</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>CUSTOMERS</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config> <security-role> <role-name>CUSTOMERS</role-name> </security-role>
  52. 52. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Tyrus – Reference Implementation • Official Page – http://tyrus.java.net • User Guide (1.8) – https://tyrus.java.net/documentation/1.8/user-guide.html • Used by – GlassFish 4.0+ – WebLogic 12c (12.1.3+)
  53. 53. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | WebSocket Protocol Fallback in WLS 12.1.3 Enabling WebSocket Use Across All Environments
  54. 54. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Configure Web Applications for WebSockets Fallback Support • Import orasocket.js in HTML • Modify web.xml • No changes required in existing , compatible code!! <web-app version="3.0"> <context-param> <description>Enable fallback mechanism</description> <param-name>com.oracle.tyrus.fallback.enabled</param-name> <param-value>true</param-value> </context-param> </web-app>
  55. 55. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Bonus: Server-sent Events Feature of Jersey, JAX-RS Reference Implementation
  56. 56. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Server-sent Events • Part of HTML5 Specification • Server Push Notifications – Client opens connection with Server – Server sends message to Client – Client can’t send messages to Server – Connection is maintained opened until either Server or Client closes it
  57. 57. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | SSE Client Side var url = ‘webresources/items/events’; var source = new EventSource(url); source.onmessage = function (event) { console.log(event.data); } source.addEventListener(“size”, function(event) { console.log(event.name + ‘ ‘ + event.data); }
  58. 58. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | SSE Server-Side – Implemented with Jersey private final SseBroadcaster BROADCASTER = new SseBroadcaster(); @GET @Path("events”) @Produces(SseFeature.SERVER_SENT_EVENTS) public EventOutput fruitEvents() { final EventOutput eventOutput = new EventOutput(); BROADCASTER.add(eventOutput); return eventOutput; } Still pending standard API
  59. 59. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | SSE Server-Side – Implemented with Jersey @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void addFruit(@FormParam("fruit")String fruit) { fruitsList.add(fruit); // Broadcasting an un-named event with the name of the newly added item in data BROADCASTER.broadcast(new OutboundEvent.Builder().data(String.class, fruit).build()); // Broadcasting a named "add" event with the current size of the items collection in data BROADCASTER.broadcast(new OutboundEvent.Builder().name("size").data(Integer.class, fruitsList.size()).build()); } Still pending standard API
  60. 60. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Differences between WebSockets and SSE WebSockets Server Sent Events Over a custom protocol Over simple HTTP Full Duplex, Bi-directional Server-Push Only, Client->Server is out-of-band (higher latency) Native support in most browsers Can be poly-filled to backport Not straight forward protocol Simpler protocol Pre-defined message handlers Arbitrary events Application-specific Built-in support for re-connection and event id Require server and/or proxy configurations No server or proxy changes required ArrayBuffer and Blob No support for binary types
  61. 61. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

×