Upload
bruno-borges
View
919
Download
9
Tags:
Embed Size (px)
DESCRIPTION
WebLogic 12c (12.1.3) already bundles some Java EE 7 APIs with commercial support. Mostly for modern
Citation preview
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
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
• Bruno Borges
– Principal Product Manager, Java Evangelist – Oracle Latin America
– @brunoborges – [email protected]
Speaker
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
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.0 Servlet 3.1
JSF 2.2 EL 3.0 JSP
JSTL
Bean
Valid
ation 1
.1
Inte
rce
pto
rs 1
.2
CD
I 1.1
Concurr
ency 1
.0
JPA 2.1
JTA 1.2 EJB 3.2 JMS 2.0
Batch 1.0
JCA 1.7
JavaMail 1.5
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
WebLogic Server 12.1.3 Mobile, Developer Productivity
WLS 12.1.3 Clients
HTM
L5
clien
ts
ADF Mobile
Proxies
OTD
Apache
OHS
Web Sockets (JSR 356)
TopLink Data Services
Server-Sent Events
JAX-RS 2.0
WebSocket Emulation
We
bSo
cket Em
ulatio
n
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
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
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
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.oracle.weblogic.jpa21support_1.0.0.0_2-1.jar
$> opatch apply $PATH_TO_PATCH_17814796
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
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
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>
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();
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");
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 {...}
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();
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)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
JSON Processing API 1.0 Standard Java API for processing JSON structures
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
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
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
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
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
JAX-RS 2.0 A Standard Java API to build RESTful services and clients
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
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);
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) {...}
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(); } }
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Java API for WebSocket 1.0 Developing WebSocket Applications using the Java Standard
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
WebSocket Overview
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
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
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
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
The WebSocket Protocol Opening Handshake
We
bLo
gic
Serv
er
12
.1.3
HTTP
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
The WebSocket Protocol Protocol Upgrade
We
bLo
gic
Serv
er
12
.1.3
WebSocket
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
We
bLo
gic
Serv
er
12
.1.3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
The WebSocket Protocol Closing Handshake
We
bLo
gic
Serv
er
12
.1.3
Close Frame
Data Frame
Data Frame
Data Frame
Close Frame
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
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
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); } }
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)); } }
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; } }
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) { } } }); } }
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)
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());
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)
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>
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) { } } }); } }
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)
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(); }
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)); }
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();
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>
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+)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
WebSocket Protocol Fallback in WLS 12.1.3 Enabling WebSocket Use Across All Environments
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>
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Bonus: Server-sent Events Feature of Jersey, JAX-RS Reference Implementation
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
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);
}
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
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
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
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |