![Page 1: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/1.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Oracle Coherence and WebLogic 12c
Delivering Real Time Push at Scale
Steve Millidge
![Page 2: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/2.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
About Me
• Founder of C2B2
– Leading Independent Middleware Experts
– Non-functional Experts
• Vendor Neutral
– Red Hat (JBoss), Oracle (Fusion), VMWare (vFabric), Open Source (Apache)
• 20 Years Middleware Expertise
• 15 years Field Consultancy
![Page 3: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/3.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Agenda
• Introduction to Web Sockets and Push
• WebSockets in WebLogic 12c
• Introduction to Coherence
• Test Demo
• Code walkthrough - Hooking up to Coherence
• Summary
![Page 4: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/4.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Standard HTTP model
•Client requests data – server responds
![Page 5: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/5.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
The problems with HTTP
• HTTP is a request-response protocol
• HTTP is half-duplex – one way traffic
• HTTP is stateless – lots of redundant data
• New connection required for each transaction
![Page 6: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/6.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Push to browser
![Page 7: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/7.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Simulated Push – HTTP Polling
• Regular requests at a set interval
• Near real-time
• Server events may occur between requests
![Page 8: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/8.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Simulated Push – Long polling
• Connection is kept open
• Response is blocked until an event occurs or a timeout occurs
• Resource hungry on the server
![Page 9: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/9.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
HTTP Streaming
• Long lived HTTP connection
• Or XMLHttpRequest connection
• Browser needs to close and reconnect the streaming channel to release memory
![Page 10: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/10.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Reverse AJAX/Comet
• Utilises long polling or HTTP Streaming techniques
• Complex development
• Poor scalability
![Page 11: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/11.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
The Future - Web Sockets
• New to HTML 5
• Enables Full Duplex communication between a browser and a Server
• Allows Web Servers to push updates to browsers
• Better than “long polling”
• Establishes a Dedicated Socket to the Backend Web Socket Server
![Page 12: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/12.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Web Socket standards
• This is all in flux
• Rfc 6455 defines the protocol
• W3C SSE http://dev.w3.org/html5/eventsource/
• W3C WebSockets http://dev.w3.org/html5/websockets/
![Page 13: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/13.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Browser Support
• Chrome 4+
• Internet Explorer 10+
• Firefox 4+
• Opera 10.7+
• Safari 5+
![Page 14: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/14.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Benefits over old techniques
• Reduced latency
• Reduced network traffic
• Reduced CPU/memory usage on the server
• Scalable
• Simplified development
![Page 15: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/15.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Web Socket Protocol
•Client
•GET /chat HTTP/1.1
•Host: server.example.com
Upgrade: websocket
•Connection: Upgrade
•Sec-WebSocket-Key:
dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
•Sec-WebSocket-Protocol:
chat, superchat
•Sec-WebSocket-Version: 13
•Server
•HTTP/1.1 101 Switching
•Protocols Upgrade:
websocket Connection:
Upgrade Sec-WebSocket-
Accept:
s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
•Sec-WebSocket-Protocol:
chat
![Page 16: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/16.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
From the RFC
•Conceptually, WebSocket is really just a layer on top of TCP that does the following:
• adds a web origin-based security model for browsers
• adds an addressing and protocol naming mechanism to support multiple services on one port and multiple host names on one IP address
• layers a framing mechanism on top of TCP to get back to the IP packet mechanism that TCP is built on, but without length limits
• includes an additional closing handshake in-band that is designed to work in the presence of proxies and other intermediaries
![Page 17: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/17.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
JavaScript API
•Web Socket
•WebSocket(location,protocol)
•Function onmessage
•Function onopen
•Function onclose
•Function onerror
•close()
•send(data)
•Server Sent Events
• EventSource(location)
•Function onmessage
•Function onopen
•Function onerror
![Page 18: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/18.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
W3C Definition [Constructor(in DOMString url, in optional DOMString protocol)]
interface WebSocket {
readonly attribute DOMString URL;
// ready state
const unsigned short CONNECTING = 0;
const unsigned short OPEN = 1;
const unsigned short CLOSED = 2;
readonly attribute unsigned short readyState;
readonly attribute unsigned long bufferedAmount;
// networking
attribute Function onopen;
attribute Function onmessage;
attribute Function onclose;
boolean send(in DOMString data);
void close();
};
WebSocket implements EventTarget;
![Page 19: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/19.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
WebSockets in WebLogic
![Page 20: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/20.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
WebLogic Key Classes
• Note this is only in WebLogic 12c
WebSocket Listener
WebSocket Context
WebSocketConnection
WebSocketConnection
WebSocketConnection
![Page 21: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/21.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
WebLogic Annotations
@WebSocket (
pathPatterns = {"/chat/*"},
dispatchPolicy = "ChatWorkManager",
timeout = 30,
maxConnections = 1000 )
public class MyListener implements WebSocketListener { ... }
![Page 22: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/22.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
JSR 356
• @ServerEndpoint(path="/echo") public class EchoBean { @OnMessage public String echo(String message) { return message + " (from your server)"; } }
![Page 23: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/23.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Oracle Coherence Real Time Push @ Scale?
![Page 24: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/24.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
JCache
• JCache standard api for caches
• JSR 107 (Coming in JEE7)
• Provides Map Semantics for Cache
– put(Key,Object)
– Object get(Key)
• Most Caches support this api
• Coherence supports the api
![Page 25: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/25.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Data Cache
• Data Caches aren’t New
– Hibernate Session Cache
– Entity Bean Cache
– JPA Cache
– Custom Caches
– Open Source Caches
• Typically Cache Database Data
• Data Grids are Cache on Steroids!
![Page 26: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/26.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Cache Partitioning
Application
Cache
Application
Cache
Application
Cache
Application
Cache
PU
T B
B
B
GET
B
B B C C
PU
T C
C
![Page 27: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/27.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
HA Cache Partitioning
Application
Cache
Application
Cache
Application
Cache
Application
Cache
PU
T B
B
B B
NODE CRASH!!
! B
![Page 28: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/28.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Typical Coherence Grid
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
Application
Cache
![Page 29: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/29.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Coherence Partitioned Cache
• Linear Scalability
– 2 hops for Read (Worst Case)
– 2 hops for Write (Worst Case)
• High Availability
– Configurable Duplicates
• Location Independent Access
– Grid knows where data is
• More Nodes = More Data in Memory
![Page 30: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/30.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Coherence Key Features
• Elasticity
• Grow and Shrink the Cluster
• Grid Execution • Push code around the cluster
• Grid Queries
• Near Cache
• Continuous Query Cache
• Many Caching Strategies
•
![Page 31: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/31.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Grid Events Subsystem
Application
Cache
Application
Cache
Application
Cache
Application
Cache Cache Listener
![Page 32: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/32.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Coherence Event Listeners
• Coherence classes MapEvent, MapListener
• Each time a record is changed, Coherence raises a MapEvent
• Override entryUpdated, entryInserted and entryDeleted to process these events
• MapListeners can have filters
![Page 33: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/33.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Events
• E_ALL – All Events
• E_INSERTED – Inserts
• E_DELETED – Deletes
• E_UPDATED – Updated
• E_UPDATED_ENTERED – Updated and now matched
• E_UPDATED_LEFT – Updated and now not
matched
• E_UPDATED_WITHIN – Updated still matched
• E_KEYSET – All updated which change the
matching set
![Page 34: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/34.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
JSR 107 API
•boolean registerCacheEntryListener(
• CacheEntryListener cacheEntryListener);
•Fired on
• Expired
• Removed
• Updated
• Read
![Page 35: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/35.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Best Practice Architecture
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
Application Cach
e
JEE Cluster Node
JEE Cluster Node
Load Balancer
JEE Cluster Node
JEE Cluster Node
JEE Cluster Node
![Page 36: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/36.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Stock Ticker Architecture
WebLogic 12c
Application Cache
StockTicker App
Application Cache
![Page 37: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/37.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Demo
http://demo.c2b2.co.uk:7080 Follow the Link
![Page 38: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/38.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Stock Ticker Classes
WebLogic 12c WebSocket
Engine
Index.jsp
Stock Ticker
(WebSocketAdapter)
WAR File
Named Cache
PushUpdates
![Page 39: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/39.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
PushUpdates
public static void main(String args[]) throws Exception {
// attach to Coherence
CacheFactory.ensureCluster();
NamedCache cache = CacheFactory.getCache("Stocks");
// create a random stock and stick it in Coherence
while(true) {
Stock stock = new Stock("C2B2","C2B2",Math.random() * 100.0);
cache.put("C2B2", stock);
int sleepTime = (int)(500*Math.random() + 500);
Thread.currentThread().sleep(sleepTime);
}
}
![Page 40: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/40.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Ticker @WebSocket (
pathPatterns = {"graph/*"},
timeout = 3600
)
public class Ticker extends WebSocketAdapter implements MapListener
{
public Ticker() {
try {
jaxb = JAXBContext.newInstance(Stock.class);
} catch (JAXBException ex) {
Logger.getLogger(Ticker.class.getName()).log(Level.SEVERE, null, ex);
}
}
![Page 41: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/41.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Ticker init
@Override
public void init(WebSocketContext context) {
super.init(context);
CacheFactory.ensureCluster();
cache = CacheFactory.getCache("Stocks");
System.out.println("Got Cache " + cache);
cache.addMapListener(this);
}
![Page 42: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/42.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Ticker entryUpdated
public void entryUpdated(MapEvent me) {
// marshall to JSON
Stock stock = (Stock) me.getNewValue();
Marshaller m = jaxb.createMarshaller();
m.setProperty("eclipselink.media-type", "application/json");
StringWriter sw = new StringWriter(30);
m.marshal(stock, sw);
// now write to socket
Set<WebSocketConnection> conns = getWebSocketContext().getWebSocketConnections();
for (WebSocketConnection conn : conns) {
conn.send(sw.toString());
}
}
![Page 43: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/43.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
index.jsp
var chart;
document.chart = new Highcharts.Chart({
….
})
websocket.onmessage = function(event) {
var object = JSON.parse(event.data);
var x = (new Date()).getTime();
var y = object.price;
document.chart.series[0].addPoint([x,y],true,true,false);
}
![Page 44: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/44.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved
Summary
• Reduced latency, network traffic and CPU/memory usage on the server
• Highly scalable
• When linked to Coherence, provide enterprise scale, event driven, real time push
![Page 45: Oracle Coherence & WebLogic 12c Web Sockets: Delivering Real Time Push at Scale](https://reader034.vdocument.in/reader034/viewer/2022052522/554a0f1bb4c905825d8b48b9/html5/thumbnails/45.jpg)
© C2B2 Consulting Limited 2013 All Rights Reserved