Performance Zone is brought to you in partnership with:

Gerard Davison is a Senior Principal Software Engineer working at Oracle in the UK on SOAP and REST tooling. Currently he is contributing in the area of WADL generation and client generation in the Jersey project and is maintaining the Abbot swing automation project. He also maintain a small holding of Hudson nodes run all those tests. He graduated from the University of Reading with a degree in Human Cybernetic and can't help looking for feedback loops. Gerard is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

Using Java WebSockets, JSR 356, and JSON mapped to POJO's

05.02.2013
| 12855 views |
  • submit to reddit

I have been playing around with Tyrus, the reference implementation of the JSR 356 WebSocket for Java spec. Because I was looking at test tooling I was interested in running both the client and the server side in Java. So no HTML5 in this blog post I am afraid.

In this example we want to sent JSON back and forth and because I am old fashioned like that I want to be able to bind to a POJO object. I am going to use Jackson for this so my maven file looks like this:

<dependencies>
    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0-rc3</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-client</artifactId>
        <version>1.0-rc3</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-server</artifactId>
        <version>1.0-rc3</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly</artifactId>
        <version>1.0-rc3</version>
    </dependency>
    

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.2.0</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.2.0</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.2.0</version>
    </dependency>

  </dependencies>


So the first things we need to do is to define an implementations of the Encode/Decoder interfaces to do this work for us. This is going to do some simple reflection to workout what the bean class is. Like with JAX-WS it is easier to put them on the same class. Note that we use the streaming version of the interface and are only handling text content. (Ignoring the ability to send binary data for the moment)
    package websocket;  
      
    import com.fasterxml.jackson.databind.ObjectMapper;  
      
    import java.io.IOException;  
    import java.io.Reader;  
    import java.io.Writer;  
      
    import java.lang.reflect.ParameterizedType;  
    import java.lang.reflect.Type;  
      
    import javax.websocket.DecodeException;  
    import javax.websocket.Decoder;  
    import javax.websocket.EncodeException;  
    import javax.websocket.Encoder;  
    import javax.websocket.EndpointConfig;  
      
    public abstract class JSONCoder<T>  
      implements Encoder.TextStream<T>, Decoder.TextStream<T>{  
      
      
        private Class<T> _type;  
          
        // When configured my read in that ObjectMapper is not thread safe  
        //  
        private ThreadLocal<ObjectMapper> _mapper = new ThreadLocal<ObjectMapper>() {  
      
            @Override  
            protected ObjectMapper initialValue() {  
                return new ObjectMapper();  
            }  
        };  
          
      
        @Override  
        public void init(EndpointConfig endpointConfig) {  
          
            ParameterizedType $thisClass = (ParameterizedType) this.getClass().getGenericSuperclass();  
            Type $T = $thisClass.getActualTypeArguments()[0];  
            if ($T instanceof Class) {  
                _type = (Class<T>)$T;  
            }  
            else if ($T instanceof ParameterizedType) {  
                _type = (Class<T>)((ParameterizedType)$T).getRawType();  
            }  
        }  
      
        @Override  
        public void encode(T object, Writer writer) throws EncodeException, IOException {  
            _mapper.get().writeValue(writer, object);  
        }  
      
        @Override  
        public T decode(Reader reader) throws DecodeException, IOException {  
            return _mapper.get().readValue(reader, _type);  
        }  
      
        @Override  
        public void destroy() {  
              
        }  
      
    }  
The bean class is really quite simple with a static subclass of the Coder that we can use later.
    package websocket;  
      
    public class EchoBean {  
          
          
        public static class EchoBeanCode extends  
           JSONCoder<EchoBean> {  
              
        }  
          
          
        private String _message;  
        private String _reply;  
      
      
        public EchoBean() {  
              
        }  
      
        public EchoBean(String _message) {  
            super();  
            this._message = _message;  
        }  
      
      
        public void setMessage(String _message) {  
            this._message = _message;  
        }  
      
        public String getMessage() {  
            return _message;  
        }  
      
      
        public void setReply(String _reply) {  
            this._reply = _reply;  
        }  
      
        public String getReply() {  
            return _reply;  
        }  
      
    }  
So new we need to implement our server endpoint, you can go one of two way either annotating a POJO or extending Endpoint. I am going with the first for the server and the second for the client. Really all this service does is to post the message back to the client. Note the registration of the encode and decoder. The same class in this case.
    package websocket;  
      
    import java.io.IOException;  
      
    import javax.websocket.EncodeException;  
    import javax.websocket.EndpointConfig;  
    import javax.websocket.OnMessage;  
    import javax.websocket.OnOpen;  
    import javax.websocket.Session;  
    import javax.websocket.server.ServerEndpoint;  
    import static java.lang.System.out;  
      
    @ServerEndpoint(value="/echo",  
                    encoders = {EchoBean.EchoBeanCode.class},  
                    decoders = {EchoBean.EchoBeanCode.class})  
    public class EchoBeanService  
    {  
          
        @OnMessage  
        public void echo (EchoBean bean, Session peer) throws IOException, EncodeException {  
            //  
            bean.setReply("Server says " + bean.getMessage());  
            out.println("Sending message to client");  
            peer.getBasicRemote().sendObject(bean);  
        }  
      
        @OnOpen  
        public void onOpen(final Session session, EndpointConfig endpointConfig) {  
            out.println("Server connected "  + session + " " + endpointConfig);  
        }  
    }  
Lets look at a client bean, this time extending the standard Endpoint class and adding a specific listener for a message. In this case when the message is received the connection is simply closed to make our test case simple. In the real world managing this connection would obviously be more complicated.
    package websocket;  
      
    import java.io.IOException;  
      
    import javax.websocket.ClientEndpoint;  
    import javax.websocket.CloseReason;  
    import javax.websocket.EncodeException;  
    import javax.websocket.Endpoint;  
    import javax.websocket.EndpointConfig;  
    import javax.websocket.MessageHandler;  
    import javax.websocket.Session;  
      
    import static java.lang.System.out;  
      
    @ClientEndpoint(encoders = {EchoBean.EchoBeanCode.class},  
                    decoders = {EchoBean.EchoBeanCode.class})  
    public class EchoBeanClient   
      extends Endpoint  
    {  
        public void onOpen(final Session session, EndpointConfig endpointConfig) {  
      
            out.println("Client Connection open "  + session + " " + endpointConfig);  
              
            // Add a listener to capture the returning event  
            //  
              
            session.addMessageHandler(new MessageHandler.Whole<echobean>() {  
      
                @Override  
                public void onMessage(EchoBean bean) {  
                    out.println("Message from server : " + bean.getReply());  
                      
                    out.println("Closing connection");  
                    try {  
                        session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "All fine"));  
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                }  
            });  
              
            // Once we are connected we can now safely send out initial message to the server  
            //  
              
            out.println("Sending message to server");  
            try {  
                EchoBean bean = new EchoBean("Hello");  
                session.getBasicRemote().sendObject(bean);  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch (EncodeException e) {  
                e.printStackTrace();  
            }  
      
        }  
    }  
    </echobean>  
Now running the WebSocket standalone is really quite straightforward with Tyrus, you simple instantiate a Server and start it. Be aware this starts daemon threads so you need to make sure if this is in a main method that you do something to keep the JVM alive.
    import org.glassfish.tyrus.server.Server;  
      
    Server server = new Server("localhost", 8025, "/", EchoBeanService.class);  
    server.start();  
So the client is relatively simple; but as we are doing the declarative method we need to explicitly register the encoders and decoders when registering the client class.
    import javax.websocket.ClientEndpointConfig;  
    import javax.websocket.Decoder;  
    import javax.websocket.Encoder;  
    import javax.websocket.Session;  
      
    import org.glassfish.tyrus.client.ClientManager;  
      
      
    // Right now we have to create a client, which will send a message then close  
    // when it has received a reply  
    //  
      
    ClientManager client = ClientManager.createClient();  
    EchoBeanClient beanClient = new EchoBeanClient();  
      
    Session session = client.connectToServer(  
            beanClient,   
            ClientEndpointConfig.Builder.create()  
             .encoders(Arrays.<Class<? extends Encoder>>asList(EchoBean.EchoBeanCode.class))  
             .decoders(Arrays.<Class<? extends Decoder>>asList(EchoBean.EchoBeanCode.class))  
             .build(),  
            URI.create("ws://localhost:8025/echo"));  
              
              
    // Wait until things are closed down  
              
    while (session.isOpen()) {  
        out.println("Waiting");  
        TimeUnit.MILLISECONDS.sleep(10);  
    }  
Now the output of this looks like the following:
Server connected SessionImpl{uri=/echo, id='e7739cc8-1ce5-4c26-ad5f-88a24c688799', endpoint=EndpointWrapper{endpointClass=null, endpoint=org.glassfish.tyrus.core.AnnotatedEndpoint@1ce5bc9, uri='/echo', contextPath='/'}} javax.websocket.server.DefaultServerEndpointConfig@ec120d
Waiting
Client Connection open SessionImpl{uri=ws://localhost:8025/echo, id='7428be2b-6f8a-4c40-a0c4-b1c8b22e1338', endpoint=EndpointWrapper{endpointClass=null, endpoint=websocket.EchoBeanClient@404c85, uri='ws://localhost:8025/echo', contextPath='ws://localhost:8025/echo'}} javax.websocket.DefaultClientEndpointConfig@15fdf14
Sending message to server
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Sending message to client
Message from server : Server says Hello
Closing connection
Waiting

Interestingly the first time this is run the there is a pause, I suspect this is due to Jackson setting itself up but I haven't had time to profile. I did find that this long delay on occurred on the first post - although obviously this is going to be slower than just passing plain text messages in general. Whether the different is significant to you depends on your application.

It would be interesting to compare the performance of the plain text with a JSON stream API such as that provided by the new JSR and of course the version that binds those values to a JSON POJO. Something for another day perhaps.


Published at DZone with permission of Gerard Davison, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Vidhyadharan De... replied on Thu, 2013/05/09 - 10:48pm

Hi ,

Good start up article. Could you provide full source code EchoBeanClient  .

Thanks

Vidhya 

Gerard Davison replied on Fri, 2013/05/10 - 3:15am

Vidhya,

Appart from a main method to wrap the final two snippets there is no other code. This is a slight escaping issue in the EchoBeanClient that I have asked the DZone people to fix; but otherwise all the code is there.


Gerard

Vidhyadharan De... replied on Mon, 2013/05/13 - 6:18am

Yeah Thanks Gerard.. I had executed the sample successfully 

Thanks,

Vidhya

Jason Kilgrow replied on Mon, 2013/05/13 - 1:14pm

I guess I'm stupid. What is that '$' notation that you have, for example, here:

Type $T = $thisClass.getActualTypeArguments()[0]; 

Gerard Davison replied on Tue, 2013/05/14 - 5:03am

Jason,

Nothing special but sometimes I use it when crossing over to a different meta level. So when you end up doing reflection to you have $someField for the Field object that represents a give field and someField for the actual value of that field.

It also helps a lot when writing code generators to make this distinction; but that is a far less common use case.

Gerard


Thai Dang Vu replied on Fri, 2013/05/24 - 2:57pm

 I don't understand this: "When configured my read in that ObjectMapper is not thread safe". I think ObjectMapper is thread-safe .

Gerard Davison replied on Tue, 2013/05/28 - 4:49am in response to: Thai Dang Vu

It looks like you are right, thanks for the link.

Gerard

Philipp Keck replied on Thu, 2014/07/24 - 12:00pm

Unfortunately, this does not seem to work with the Undertow WebSockets implementation (used in WildFly).

They determine the en-/decoder type from the parameter of the encode/decode method, which is T and due to type erasure resolves to Object. So the container thinks that there were lots of Object-encoders and -decoders, but when a coder is actually needed, it cannot found.

Do you have an idea to solve this (besides overriding the method in every sub-class)?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.