JSR365是java制定的websocket编程规范,属于Java EE 7的一部分。下面这段描述摘录自官网对JSR365的说明:
JSR 356, Java API for WebSocket, specifies the API that Java developers can use when they want to integrate WebSockets into their applications—both on the server side as well as on the Java client side. Every implementation of the WebSocket protocol that claims to be compliant with JSR 356 must implement this API. As a consequence, developers can write their WebSocket-based applications independent of the underlying WebSocket implementation. This is a huge benefit, because it prevents a vendor-lock and allows for more choices and freedom of libraries and application servers.
JSR 356 is a part of the upcoming Java EE 7 standard; hence, all Java EE 7–compliant application servers will have an implementation of the WebSocket protocol that adheres to the JSR 356 standard. Once they are established, WebSocket client and server peers are symmetrical. The difference between a client API and a server API is, therefore, minimal. JSR 356 defines a Java client API as well, which is a subset of the full API required in Java EE 7.
websocket和socket一样,既有客户端编程,也有服务端编程,JSR365制定了websocket客户端和服务端编程常用的API。我的另一篇文章介绍了使用Tomcat自己的websocket也能够实现web推送,但是那篇文章中的代码只能运行在tomcat容器。我们使用JSR365编写的websocket程序,能够在所有web容器中运行,只要这个容器实现了J2EE 7规范。我使用的是Tomcat7.0.62,这个版本的Tomcat实现了J2EE 7规范。
为了使用JSR365,我们需要在pom.xml加入编译期的依赖:
<dependency>
<groupId>javax.websocket</grou