Weitere ähnliche Inhalte Ähnlich wie Extending JMS to Web Devices over HTML5 WebSockets - JavaOne 2011 (20) Mehr von Peter Moskovits (9) Kürzlich hochgeladen (20) Extending JMS to Web Devices over HTML5 WebSockets - JavaOne 20111. Extending the
Java Message Service
to Web devices
over HTML5 WebSockets
Chris Barrow, Peter Moskovits
Kaazing Corporation
JavaOne - October 5, 2011
1 JavaOne 2011 © 2011 – Kaazing Corporation
2. Agenda
l HTML5 WebSockets vs. HTTP
l Java Message Service
l Implementing JMS over WebSockets
l Demos and code examples
l Kaazing WebSocket Gateway, JMS Edition
l Questions
2 JavaOne 2011 © 2011 – Kaazing Corporation
3. Networked Applications
Full duplex
TCP socket
Thick Client Back-end
server
3 JavaOne 2011 © 2011 – Kaazing Corporation
4. Networked Applications
Full duplex
TCP socket
Thick Client Back-end
server
Full duplex
Half Duplex
HTTP
Browser
Middleware Back-end
server
4 JavaOne 2011 © 2011 – Kaazing Corporation
5. HTTP Workarounds & Hacks
Full duplex
Half Duplex
Browser
Middleware Back-end
server
5 JavaOne 2011 © 2011 – Kaazing Corporation
6. HTML5 WebSocket
l Extends TCP across the web
l Full-duplex, single socket, very low overhead
l Shares port with HTTP (80/443)
Enables new classes of networked apps
l
l W3C API (JavaScript)
l IETF Protocol
Full duplex
WebSocket
Browser Back-end
server
6 JavaOne 2011 © 2011 – Kaazing Corporation
7. HTTP vs. WebSocket
HTTP WebSocket
Overhead 1000s of bytes (headers) 2-6 bytes/frame (typical)
Latency New connection each time None: Reuses existing
connection
Latency (polling) Must wait for next request No waiting
Latency (long polling) None, if server is holding No waiting
a pending request (+ time
to set up new connection)
7 JavaOne 2011 © 2011 – Kaazing Corporation
8. Overheard…
"Reducing kilobytes of data to 2 bytes… and
reducing latency from 150ms to 50ms is far more
than marginal. In fact, these two factors alone are
enough to make WebSocket seriously interesting to
Google."
Ian Hickson (Google, HTML5 Spec Lead)
8 JavaOne 2011 © 2011 – Kaazing Corporation
9. Using the WebSocket API
Java
//Create and connect new WebSocket
WebSocket mySocket = new WebSocket();
// Associate listener
webSocket.addWebSocketListener(
new WebSocketListener() {
@Override
public void onMessage(WebSocketEvent event) {
alert(event.getData());
}
@Override
public void onClose(WebSocketEvent closedEvent) {}
};
mySocket.connect(new URI("ws://www.WebSocket.org"));
mySocket.send("Hello world!");
9 JavaOne 2011 © 2011 – Kaazing Corporation
10. WebSocket Browser Support
Browser native support
§ Chrome
§ Firefox (need to turn on)
§ Opera 10.7 (need to turn on)
§ Safari
§ Internet Explorer 9+ Beta
10 JavaOne 2011 © 2011 – Kaazing Corporation
11. JMS Over WebSocket
WebSocket
Browser Back-end
server
11 JavaOne 2011 © 2011 – Kaazing Corporation
12. JMS Over WebSocket
WebSocket
Browser Back-end
server
JMS
WebSocket
Browser Back-end
server
12 JavaOne 2011 © 2011 – Kaazing Corporation
14. Why JMS?
l WebSocket is a good start but doesn't offer
much help to the application developer
l JMS well established standard, widely available
l Offers important features over pure WebSocket:
l Publish and subscribe (Topics) for broadcast
l Point to point (Queues) for command processing
l Transactions
l Guaranteed delivery
l Structured data
14 JavaOne 2011 © 2011 – Kaazing Corporation
15. Why JMS over WebSocket?
Typical Current JMS Architecture
Firewall
J2EE
Applica+on
Server
Web Container EJB Container
SOAP over HTTP JMS
Web
Service EJB
WEB
JSP
Message Broker
HTTP (JMS Provider)
15 JavaOne 2011 © 2011 – Kaazing Corporation
16. Why JMS over WebSocket?
Typical Current JMS Architecture
Firewall
J2EE
Applica+on
Server
Web Container EJB Container
SOAP over HTTP JMS
Web
Service EJB
WEB
JSP
Message Broker
HTTP (JMS Provider)
New WebSocket-based Architecture
Firewall
JMS Over
WebSocket
16 JavaOne 2011 © 2011 – Kaazing Corporation
18. JMS over WebSocket
1. JMS Client library (in browser, web device)
2. Protocol
3. Server (bridge between broker & web)
4. Message Broker (JMS Provider)
18 JavaOne 2011 © 2011 – Kaazing Corporation
19. JMS Client Library
● Implements the JMS objects and API
l Initiate WebSocket connection
l Encode outgoing JMS messages in STOMP
l Decode and deliver incoming messages
● Manage connections and sessions, stop and
start, message listeners, reconnect
● A single WebSocket connection suffices
(full duplex, asynchronous receive)
19 JavaOne 2011 © 2011 – Kaazing Corporation
20. The Protocol
l STOMP
l Text-based wire encoding, so suitable for multiple
client languages (Java, JavaScript, ...)
l http://stomp.codehaus.org & http://stomp.github.com!
l Wireline encoding into STOMP:
l CONNECTnNAME:namen PASS:password (once)
l Encode JMS properties as key-value pairs:
SENDnprop1:valuenprop2
-
- MESSAGEnprop1:valuenprop2
l SUBSCRIBEndestination:/topic/stock
l BEGIN, COMMIT, ABORT
20 JavaOne 2011 © 2011 – Kaazing Corporation
21. JMS Client Library
● session.createConsumer
l → SUBSCRIBE destination:xxx
l Await RECEIPT
● producer.send
l → SEND prop1:val1... <text>
l Await RECEIPT
● Separate thread(s): read MESSAGE's
l Route to appropriate MessageListener
l Send ACK
21 JavaOne 2011 © 2011 – Kaazing Corporation
22. JMS Client Library
● Transactions
l Group message sends and Acks
l Single unit of work
● Auto-reconnect
l For resilience to network failures
l Reissue SUBSCRIBEs
● Other client languages:
l Javascript, Flash, .Net
22 JavaOne 2011 © 2011 – Kaazing Corporation
23. The Server
● Bridge between Web and Message Broker
l Accepts WebSocket connections
l Connects to JMS provider
● Converts incoming STOMP frames to JMS API
calls
● Routes messages from provider back to clients
● Must manage flow control
23 JavaOne 2011 © 2011 – Kaazing Corporation
24. The Server
l Simplest implementation:
l Each client connection → 1 broker Connection and
Session
l Each client subscription (message consumer) → 1
consumer in broker
l More scalable:
l 1 connection for many clients
l Single topic subscription for many clients
24 JavaOne 2011 © 2011 – Kaazing Corporation
25. Demos
http://portfolio.kaazing.me!
http://forex.kaazing.me!
25 JavaOne 2011 © 2011 – Kaazing Corporation
26. Portfolio Demo
● "stock" topic for stock price updates
l MessageConsumer stockConsumer!
l MessageListener: portfolioModel.updateStock()!
● Temporary queue for command responses
l responseQueue = session.createTemporaryQueue()!
● "command" queue to send commands
l producer.send (replyTo=responseQueue)
l command property (get_portfolio, get_balance, buy, sell)
26 JavaOne 2011 © 2011 – Kaazing Corporation
27. Code Examples
l Creating a connection (omitting exception handling)
env.put(Context.INITIAL_CONTEXT_FACTORY,
StompInitialContextFactory.class.getName());
env.put(Context.PROVIDER_URL,
“ws://myhost:80/stomp.jms”);
InitialContext
ctx
=
new
InitialContext(env);
ConnectionFactory
cf
=
(ConnectionFactory)ctx.lookup(“ConnectionFactory”);
Connection
connection
=
cf.createConnection();
l Destination look up
Topic
stockTopic
=
(Topic)ctx.lookup(“/topic/stock”);
27 JavaOne 2011 © 2011 – Kaazing Corporation
28. Code Examples
l From there it's all just standard JMS API
Session
session
=
connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageConsumer
stockConsumer
=
session.createConsumer(stockTopic);
//
Listen
for
stock
price
changes
stockConsumer.setMessageListener(new
MessageListener()
{
@Override
public
void
onMessage(Message
message)
{
try
{
String
stockData
=
((TextMessage)message).getText();
portfolioModel.updateStock(new
Stock(stockData));
}
catch(JMSException
e)
{
}
}
}
connection.start();
28 JavaOne 2011 © 2011 – Kaazing Corporation
29. A Real World Solution:
Kaazing WebSocket Gateway
29 JavaOne 2011 © 2011 – Kaazing Corporation
30. WebSocket Client Support
l Java and JavaFX
l JavaScript
l Microsoft Silverlight
and .NET
l Adobe Flex (Flash)
30 JavaOne 2011 © 2011 – Kaazing Corporation
31. WebSocket Emulation
Emulation for browsers with no native WebSocket support:
l Kaazing WebSocket Gateway: makes WebSocket work in all
browsers today:
l Internet Explorer 6, 7, 8
l Firefox 3.6
l And more . . .
31 JavaOne 2011 © 2011 – Kaazing Corporation
32. Kaazing JMS Edition
l Messages traverse firewalls and proxies
l Fast, secure communication over the Internet
l Message distribution to massive numbers of clients
thanks to resource offloading (fan-out)
32 JavaOne 2011 © 2011 – Kaazing Corporation
33. Resource Offloading
● Connection + Session offload (fan out)
l Reduces broker resource usage
l Minimal connections, sessions to broker
● Java NIO (Apache Mina framework)
● Subscription offload
l Multiplexed over shared connection, single message
consumer
l Gateway tracks subscribed topics and queues from
all connected clients
l Gateway handles individual client acknowledgments
33 JavaOne 2011 © 2011 – Kaazing Corporation
35. JMS Gateway Features
● “stomp.jms” service
l Clients speak STOMP to the service.
l Service uses vendor's JMS API to talk to broker
l Connection, session and subscription offload
● “stomp.proxy” service
l Clients speak STOMP to the service
l Service uses STOMP to talk to “stomp.jms” service
running on another Gateway
l Further offload
● Gateway clusters for High Availability
35 JavaOne 2011 © 2011 – Kaazing Corporation
36. Using Multiple Gateways
● Hundreds of thousands of clients or more can be
supported by using a network of gateways
● “stomp.proxy” gateways at the edge (near users)
l Route messages to central gateway running
“stomp.jms” service
l Connection offloading (single connection to central
gateway)
● Most network traffic is local
● Use gateway clusters for HA
36 JavaOne 2011 © 2011 – Kaazing Corporation
38. Writing Applications
l JMS API is available to code running within the browser
or mobile device
l Gateway provides client libraries
l Written like a Java JMS client
l Similar concepts and APIs for each client technology
(JavaScript, Flash, .Net)
l Application needs to include Kaazing JMS library files
(jars, .js)
38 JavaOne 2011 © 2011 – Kaazing Corporation
40. Take-aways
l HTML5 WebSockets offers a paradigm shift for highly
interactive and scalable Web applications
l High level protocols can be extended beyond the
firewall all the way to Web devices:
l JMS
l XMPP
l AMQP
l Production solution with live customers available today
40 JavaOne 2011 © 2011 – Kaazing Corporation
41. Conclusion
“We’re committed to providing real-time
access to critical management information
across the enterprise...
Kaazing solves the industry’s fundamental issues
which have kept yesterday’s web architecture
from meeting these needs.”
Quintin Gomez, CTO of ITRS Group, 28 Sep 11
41 JavaOne 2011 © 2011 – Kaazing Corporation