2 November 2013

WebLogic 12c Does WebSockets - Getting Started


With the release of WebLogic 12.1.2 websocket support has come to WebLogic. In this blog post we'll show you how to write a simple websockets echo example just to get you started.

Unfortunately the api released with WebLogic is not the JEE7 JSR356 api, which I suspect will come when WebLogic gets JEE7 compliance. On the plus side it's a pretty simple api.

In this blog I'm using NetBeans 7.4 and I've started off by creating a basic web project and I've called it Echo.



Next I create a new Java class called EchoSocket in the echo project.


To make this class a websocket endpoint in WebLogic I add the annotation @WebSocket to the class and ensure we extend WebSocketAdapter.

Adding the @WebSocket annotation means that WebLogic will create a websocket endpoint for this class. However we now need to add some parameters to the annotation to specify the url pattern the websocket should serve so our final annotation looks like.




@WebSocket (
        pathPatterns = {"/echo/*"}
)


There are other additional parameters you can specifiy for this annotation including dispatchPolicy if you wish to use a WebLogic specific dispatch policy for the threads hadnling the requests.

Now turning to the code, we need to override methods on the WebSocketAdapter class to provide our functionality. In particular to echo back the message we need to override onMessage and within this method return the message we receive.


The code for this is shown below;

    @Override
    public void onMessage(WebSocketConnection connection, String payload) {
        try {
            connection.send("Echo from WebLogic " + payload);
        } catch (IOException ex) {
            Logger.getLogger(Echo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Echo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


This is all we need to do on the server side. You can now package that up as a war and deploy to WebLogic.



To test your websocket handler without having to write any javascript browse to http://www.websocket.org/echo.html and in the location field type your WebLogic URL which in my case is ws://127.0.0.1:7001/echo/echo


Click Connect and then Send you should see your response echoed back on the screen.

Congratulations yuou've just written your first WebSocket application with WebLogic 12.1.2.

The full code for the Echo class is below;

package uk.co.c2b2.demo.weblogic.websockets;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import weblogic.websocket.WebSocketAdapter;
import weblogic.websocket.WebSocketConnection;
import weblogic.websocket.annotation.WebSocket;

/**
 *
 * @author steve
 */
@WebSocket (
        pathPatterns = {"/echo/*"}
)
public class Echo extends WebSocketAdapter {

    @Override
    public void onMessage(WebSocketConnection connection, String payload) {
        try {
            connection.send("Echo from WebLogic " + payload);
        } catch (IOException ex) {
            Logger.getLogger(Echo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Echo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}



If you enjoyed this post, why not subscribe to our feed?

Enter your email address:




No comments :

Post a Comment