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 specify for this annotation including dispatch Policy if you wish to use a WebLogic specific dispatch policy for the threads handling the requests.

Now turning to the code, we need to override methods on the WebSocket Adapter 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.

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 you’ve just written your first WebSocket application with WebLogic 12.1.2.