Updating to latest WebSocket API - Starting with GlassFish b78 (TOTD #209)


Java API for WebSocket (JSR 356) has recently gone through a major refactoring. The changes are integrated in Tyrus b12 and now available in GlassFish b78. This Tip Of The Day (TOTD) shows the change log for WebSocket endpoint in Collaborative Whiteboard Sample (explained in TOTD #189) between GlassFish b76 and b78.

First of all, everything stays in javax.websocket.* and javax.websocket.server.* package.

There are name changes in annotations, APIs, class renames, and some behavior change as well. Pavel's blog provides a comprehensive list of changes and the annotation changes are listed here as well:

Name before b78
Name after b78
@WebSocketEndpoint
@ServerEndpoint
@WebSocketClient
@ClientEndpoint
@WebSocketMessage
@OnMessage
@WebSocketPathParam @PathParam
@WebSocketOpen
@OnOpen
@WebSocketClose
@OnClose
@WebSocketError
@OnError


Here is the change log for updating Whiteboard.java:

--- <html>Whiteboard.java (<b>849f919</b>)</html>
+++ <html><b>Current File</b></html>
@@ -45,50 +45,48 @@
import java.util.HashSet;
import java.util.Set;
import javax.websocket.EncodeException;
+import javax.websocket.OnClose;
+import javax.websocket.OnMessage;
+import javax.websocket.OnOpen;
import javax.websocket.Session;
-import javax.websocket.WebSocketClose;
-import javax.websocket.WebSocketMessage;
-import javax.websocket.WebSocketOpen;
-import javax.websocket.server.WebSocketEndpoint;
+import javax.websocket.server.ServerEndpoint;

/**
* @author Arun Gupta
*/
-@WebSocketEndpoint(value = "/websocket",
+@ServerEndpoint(value = "/websocket",
encoders = {FigureEncoder.class},
decoders = {FigureDecoder.class})
public class Whiteboard {

private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>());

- @WebSocketOpen
+ @OnOpen
public void onOpen(Session peer) {
peers.add(peer);
}

- @WebSocketClose
+ @OnClose
public void onClose(Session peer) {
peers.remove(peer);
}

- @WebSocketMessage
+ @OnMessage
public void boradcastFigure(Figure figure, Session session) throws IOException, EncodeException {
System.out.println("boradcastFigure: " + figure);
for (Session peer : peers) {
if (!peer.equals(session)) {
- peer.getRemote().sendObject(figure);
+ peer.getBasicRemote().sendObject(figure);
}
}
}

- @WebSocketMessage
+ @OnMessage
public void broadcastSnapshot(ByteBuffer data, Session session) throws IOException {
System.out.println("broadcastBinary: " + data);
for (Session peer : peers) {
if (!peer.equals(session)) {
- peer.getRemote().sendBytes(data);
+ peer.getBasicRemote().sendBinary(data);
}
}
}

The updated source code for the sample is available here and runs on GlassFish b78.

Hopefully this gives you a good idea on how to migrate your WebSocket applications from the older to the newer API. The changes are still evolving and the specification (PDF) and javadocs (ZIP) are the definitive source for the latest information. The complete set of latest spec downloads are available here.

As always, send the spec feedback to users@websocket-spec.java.net and implementation feedback to users@tyrus.java.net.

Comments:

Wow ! That a really big change ! Thanks for the note.

Posted by Neil on February 28, 2013 at 10:51 PM PST #

Hi Arun,

Usually, I make use of "diff -cw" on AIX server for better readability while calculating the differences.

So far, I understand, you would have either used some syntax highlighter or manually did coloring of red/green. Ignoring that, how to generate the diff, the way you presented in this blog.

Thanks,
NV

Posted by NV on March 04, 2013 at 04:44 AM PST #

Hi Arun,

Thanks for this important information.
I was trying Glassfish b81 (which already contains these changes) with NetBeans-dev-web-main-javaee7-175-on-20130321-full. The Websocket wizard on Netbeans are still creating the endpoint implementations with the former API names.

Posted by Cenerino on March 23, 2013 at 08:53 AM PDT #

NetBeans, GlassFish and WebSocket API are all evolving rapidly. I recommend tracking http://netbeans.org/bugzilla/show_bug.cgi?id=227320 for changes coming to NetBeans in this area.

Posted by Arun Gupta on March 25, 2013 at 07:54 AM PDT #

Hi,

On updating the POM file its showing an error:
The POM for javax:javaee-api:jar:7.0-b78 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details.

Posted by guest on April 22, 2013 at 02:02 AM PDT #

Hi,

I have updated my POM and got a warning message :
The POM for javax:javaee-api:jar:7.0-b78 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details

I'm working on a project using java-web-api. So updating the POM is resulting more warning messages and consequently failing to build :

WARNING: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
WARNING: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled

Posted by guest on April 22, 2013 at 02:06 AM PDT #

This was a known issue and has been fixed in subsequent builds. Can you change the number to 84 and try again ?

Posted by guest on April 22, 2013 at 02:20 AM PDT #

i'm trying to build the example but i receive "cmd is not recognized as an internal or external command". I checked my path variable and it was set to system32 . Where's the problem ?

Posted by guest on June 29, 2013 at 06:08 AM PDT #

wonderful article..Thankyou for helping others :)

Posted by guest on August 07, 2013 at 11:19 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today