46
© 2013 IBM Corporation JJUG Cross Community Conference 2014 Fall いよいよ始められる Java EEでのWebSocket 1

いよいよ始められる Java EEでのWebSocket #jjug #jjug_ccc #ccc_r21

Embed Size (px)

DESCRIPTION

JJUG CCC 2014 Fall R2-1 「いよいよ始められる Java EEでのWebSocket」 #jjug #jjug_ccc #ccc_r21 Java EE 7に含まれるWebSocket API。対応するモダン・ブラウザの普及率もいっきにあがり、商用アプリケーションサーバーでのサポートも開始され、いよいよ業務での使用も現実的になってきました。このセッションでは、IBMのアプリケーションサーバーの話題を中心に、開発環境のセットアップから関連ソフトの対応状況など、WebSocketをJava EE環境で使用するための情報をお伝えします。

Citation preview

  • 1. 2013 IBM Corporation JJUG Cross Community Conference 2014 Fall Java EEWebSocket 1

2. JJUG Cross Community Conference 2014 Fall Agenda Java EE 7HTML5 WebSocket WebSocket Java EEWebSocket 2 3. JJUG Cross Community Conference 2014 Fall Java EE 7HTML5 3 4. JJUG Cross Community Conference 2014 Fall Java EE 7 HTML5 4 5. JJUG Cross Community Conference 2014 Fall Java EE HTML5 JSR 344: JavaServer Faces (JSF) 2.2 JSR 353: Java API for JSON Processing (JSONP) 1.0 JSR 356: Java API for WebSocket 1.0 JSR 339: Java API for RESTful Web Services (JAX-RS) 2.0 JSR 345: Enterprise JavaBeans (EJB) 3.2 JSR 346: Contexts and Dependency Injection for Java EE (CDI) 1.1 JSR 907: Java Transaction API (JTA) 1.2 JSR 349: Bean Validation 1.1 JSR 343: Java Message Service (JMS) 2.0 JSR 338: Java Persistence API (JPA) 2.1 JSR 236: Concurrency Utilities for Java EE 1.0 JSR 352: Batch Applications for the Java Platform 1.0 5 6. JJUG Cross Community Conference 2014 Fall 201410 HTML5 HTML HTML5 CSS3 / JavaScript 6 7. JJUG Cross Community Conference 2014 Fall HTML5 MVCMVC 7 Web Browser Application Server Application HTTP Request HTML/JS Controller Session Data View Business LogicModel DomainDomain Web Browser Application Server Application View HTML/JS Controller Data Store Model Model DomainDomain RESTful or WebSocket JSON Business Logic Web Single Page Application (SPA) 8. JJUG Cross Community Conference 2014 Fall HTML5 RESTful JAX-RS 2.0 HTTP WebSocket WebSocket 1.0 Web JSONJavaScript Object Notation JSONP 1.0 JavaScript 8 Web Browser Application Server Application View HTML/JS Controller Data Store Model Model DomainDomain RESTful or Web Socket JSON Business Logic 9. JJUG Cross Community Conference 2014 Fall HTTP HTTP Keep Alive TCP 9 10. JJUG Cross Community Conference 2014 Fall Web Comet 10 11. JJUG Cross Community Conference 2014 Fall WebSocket WebSocket HTTP Protocol SwitchingWebSocket 11 12. JJUG Cross Community Conference 2014 Fall WebSocket URI URI ws:// WebSocketNon-SSL80 wss:// WebSocketSSL443 http80/443 Firewall 12 13. JJUG Cross Community Conference 2014 Fall WebSocket HTTPWebSocket 13 HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat 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 HTTP GETOpening Handshake 101WebSocket 14. JJUG Cross Community Conference 2014 Fall WebSocket 14 WebSocket CONNECTING OPEN CLOSING CLOSED Opening Handshake Closing Handshake TCP TCP 15. JJUG Cross Community Conference 2014 Fall WebSocket WebSocket (endpoint) WebSocket2 (client endpoint and server endpoint) (peer) WebSocket (session) 15 16. JJUG Cross Community Conference 2014 Fall Ajax 16 Comet HTML JS CSS Data#2 Data#1 Data Data#3 HTML JS CSSData#1 Data#2 Data#3 17. JJUG Cross Community Conference 2014 Fall WebSocket 17 Ajax Comet WebSocket HTTP HTTP WebSocket( HTTP) 1TCP (*) 1 (/1) 1 (/1) Pull Push Push () HTTP TCP (*) (*) (*)HTTP KeepAlive 18. JJUG Cross Community Conference 2014 Fall WebSocket 18 19. JJUG Cross Community Conference 2014 Fall WebSocket IETF / RFC 20102draft-hixie-thewebsocketprotocol-75 20105draft-ietf-hybi-thewebsocketprotocol-00 20114draft-ietf-hybi-thewebsocketprotocol-07 20117draft-ietf-hybi-thewebsocketprotocol-10 201112RFC 6455 "The WebSocket Protocol" W3CJavaScript API 20094First Public Draft 20091020128Working Draft 20129 W3C Candidate Recommendation 19 20. JJUG Cross Community Conference 2014 Fall 20 MSIE 10 (2013/2) Chrome 16 (2011/12) Firefox 11 (2012/3) Safari 6 (2012/7) iOS 6.0 (2012/9) Android 4.4 (2013/10) 21. JJUG Cross Community Conference 2014 Fall 21 0%WebSocket NetMarketShare.com Market Share Reports October, 2014 22. JJUG Cross Community Conference 2014 Fall Java EE 20147Oracle WebLogic Server 12.1.3 201412 IBM WebSphere Application Server 8.5.5 Liberty 22 23. JJUG Cross Community Conference 2014 Fall Web IBM HTTP ServerIHS 20148 IHS / WAS Plug-in 8.5.5.3 Load Balancer WAS NDLoad BalancerWebSocket F5 BIG-IPWebSocket 23 WAS WAS IHS Plug-in IHS Plug-in Load Balancer 24. JJUG Cross Community Conference 2014 Fall Proxy WebSocketCONNECT TLS / SSLOK Ajax / Comet 24 _Proxy Usage_: If the client is configured to use a proxy when using the WebSocket Protocol to connect to host /host/ and port /port/, then the client SHOULD connect to that proxy and ask it to open a TCP connection to the host given by /host/ and the port given by /port/. EXAMPLE: For example, if the client uses an HTTP proxy for all traffic, then if it was to try to connect to port 80 on server example.com, it might send the following lines to the proxy server: CONNECT example.com:80 HTTP/1.1 Host: example.com RFC 6455 25. JJUG Cross Community Conference 2014 Fall Java EE WebSocket 25 26. JJUG Cross Community Conference 2014 Fall JSR 356: Java API for WebSocket 1.0 JavaWebSocketAPI Java SE Encoder / Decoder 26 27. JJUG Cross Community Conference 2014 Fall EndpointServerEndpointClientEndpoint WebSocketEndpoint WebSocket Endpoint Java EEEndpoint 27 ClientEndpoint ServerEndpoint Session @OnMessage public void message(... ) @OnOpen public void open(... ) @OnClose public void close(... ) @OnMessage public void message(... ) @OnError public void error(... ) 28. JJUG Cross Community Conference 2014 Fall Endpoint POJO @javax.websocket.server.ServerEndpoint @javax.websocket.ClientEndpoint javax.websocket.Endpoint POJO 28 29. JJUG Cross Community Conference 2014 Fall Endpoint 29 @ServerEndpoint("/echo") public class EchoServiceEndpoint{ Session currentSession = null; // @OnOpen public void onOpen(Session session, EndpointConfig ec) { currentSession = session; } // @OnMessage public void receiveMessage(String msg) throws IOException { // currentSession.getBasicRemote().sendText("Hello" + msg); } // @OnClose public void onClose(Session session, CloseReason reason) { } // @OnError public void onError(Throwable t) { } } 30. JJUG Cross Community Conference 2014 Fall WebSocket 1.0 @ServerEndpoint / @ClientEndpoint / @OnOpen / @OnMessage / @OnError / @OnClose @PathPram URI 30 @OnOpen @OnOpen Public void open(Session session, EndpointConfig conf) {} @OnMessage @OnMessage Public void message(Session session, String msg) {} @OnError @OnError public void error(Session session, Throwable error) {} @OnClose @OnClose Public void close(Session session, CloseReason reason) {} @ServerEndpoint(value = /sample/{name}) Public class SampleEndpoint { @OnOpen public void open(Session session,EndpointConfig ec, @PathParam("name") String name) { } 31. JJUG Cross Community Conference 2014 Fall JVMJVM Endpoint Endpoint WebSocket Servlet 31 Endpoint Servlet 32. JJUG Cross Community Conference 2014 Fall Endpoint javax.websocket.Session Endpoint @Singleton@EJB 32 @ServerEndpoint(value = "/echo") public class EchoServiceEndpoing { private Session currentSession; @OnOpen public void onOpen(Session session, EndpointConfig ec) { currentSession = session; 33. JJUG Cross Community Conference 2014 Fall Endpoint @OnOpen Endpoint / Session 33 34. JJUG Cross Community Conference 2014 Fall SessionRemoteEndpoint RemoteEndpoint2 RemoteEndpoint.Async RemoteEndpoint.Basic Session Servlet / EJB Session 34 if (session.isOpen()) session.getBasicRemote().sendText(msg); 35. JJUG Cross Community Conference 2014 Fall Encoder / Decoder Encoder / Decoder Java 35 ServerEndpoint @OnMessage public void message(... ) @OnOpen public void open(... ) @OnClose public void close(... ) Decoder Encoder String / byte Java 36. JJUG Cross Community Conference 2014 Fall Decoder / Encoder implements @ServerEndpoint / @ClientEndpoint 36 class MyObjectDecoder implements Decoder.Binary { @Override public boolean willDecode(ByteBuffer buf) { ... } @Override public MyObject decode(ByteBuffer arg0) throws DecodeException { ... } @ServerEndpoint(value = "/chat", decoders = MyObjectDecoder.class) public class ChatService { 37. JJUG Cross Community Conference 2014 Fall HTTPHttpSession ConguratormodifyHandshake HandshakeRequestHTTPHttpSession @ServerEndpoint / @ClientEndpoint 37 import javax.websocket.server.ServerEndpointConfig.Configurator; public class ChatConfig extends Configurator { @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest req, HandshakeResponse resp) { @ServerEndpoint(value = "/chat", configurator = ChatConfig.class) public class ChatService { 38. JJUG Cross Community Conference 2014 Fall ConguratorEndpoint EndpointCongUserProperties 38 public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest req, HandshakeResponse resp) { HttpSession session = (HttpSession)request.getHttpSession(); Map userProps = config.getUserProperties(); userProps.put("MyID", (session != null)? session.getAttribute("MyID") : "No ID"); @OnOpen public void onOpen(Session session, EndpointConfig config) { Map userProps = config.getUserProperties(); String myID = (String)userProps.get("MyID"); Configurator Endpoint 39. JJUG Cross Community Conference 2014 Fall ConguratorEndpointHttpSession HandshakeRequestHttpSession Attribute HttpSession HttpSession WebSocket Congurator HttpSession 39 40. JJUG Cross Community Conference 2014 Fall IBM developerWorksWebSocket 40 Java EE 7 WebSocket http://www.ibm.com/developerworks/jp/websphere/library/was/javaee7_appguide/1.html 41. JJUG Cross Community Conference 2014 Fall WebSocket 41 42. JJUG Cross Community Conference 2014 Fall WASLiberty 42 WAS V8.5.5 Liberty & WDT Web Profile Java EE 6Web Profile JAX-WS, JAX-RS, JMS Unzip + + Unzip server.xml : 60MB 100MB : 5 WDT) Eclipse Eclipse OSS 43. JJUG Cross Community Conference 2014 Fall APIFeature 43 ServletJSPJDBCSSL JAX-RS sessionDatabase jsp-2.2 jdbc-4.0 jaxrs-1.1 sessionDatabase-1.0 ssl-1.0 server.xml JSON 44. JJUG Cross Community Conference 2014 Fall WebSocketBeta Java EE 7 LibertyBeta websocket-1.0 WebSocket 44 jsp-2.2 jdbc-4.0 websocket-1.0 45. JJUG Cross Community Conference 2014 Fall Eclipse websphere liberty IBM WebSphere Application Server Liberty Prole Developer Tools for Luna Beta Liberty 45 46. JJUG Cross Community Conference 2014 Fall WebSocketHTML5 WebSocket WebSphere Application Server Liberty WebSocket 46