X

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

Guest Author


Java API for WebSocket (href="http://jcp.org/en/jsr/detail?id=356">JSR 356) has
recently gone through a href="http://java.net/projects/websocket-spec/lists/jsr356-experts/archive/2013-02/message/58">major
refactoring. The changes are href="https://blogs.oracle.com/PavelBucek/entry/tyrus_1_0_b12_released">integrated
in Tyrus b12 and now available in href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b78.zip">GlassFish
b78. This Tip Of The Day
(TOTD) shows the change log for WebSocket endpoint in href="http://java.net/projects/websocket-spec/lists/jsr356-experts/archive/2013-02/message/58">Collaborative
Whiteboard Sample (explained in href="https://blogs.oracle.com/arungupta/entry/collaborative_whiteboard_using_websocket_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. href="https://blogs.oracle.com/PavelBucek/entry/tyrus_1_0_b12_released">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 href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/19981716c3701796ea6934f07f3ef420/totd189_whiteboard_glassfish78.zip">available
here and runs on href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b78.zip">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 href="http://java.net/projects/websocket-spec/downloads/download/Spec%20javadoc%20Drafts/v013/JavaWebSocket_JSR356_013.pdf">specification
(PDF) and href="http://java.net/projects/websocket-spec/downloads/download/Spec%20javadoc%20Drafts/v013/JavaWebSocket_JSR356_013_apidoc.zip">javadocs
(ZIP) are the definitive source for the latest information. The
complete set of latest spec downloads are href="http://java.net/projects/websocket-spec/downloads/directory/Spec%20javadoc%20Drafts/v013">available
here.



As always, send the spec feedback to href="mailto:users@websocket-spec.java.net">users@websocket-spec.java.net
and implementation feedback to href="mailto:users@tyrus.java.net">users@tyrus.java.net.


Join the discussion

Comments ( 9 )
  • Neil Friday, March 1, 2013

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


  • NV Monday, March 4, 2013

    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


  • Cenerino Saturday, March 23, 2013

    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.


  • Arun Gupta Monday, March 25, 2013

    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.


  • guest Monday, April 22, 2013

    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.


  • guest Monday, April 22, 2013

    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


  • guest Monday, April 22, 2013

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


  • guest Saturday, June 29, 2013

    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 ?


  • guest Thursday, August 8, 2013

    wonderful article..Thankyou for helping others :)


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.