Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

WebLogic 12c Does WebSockets - Getting Started

DZone's Guide to

WebLogic 12c Does WebSockets - Getting Started

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90 day trial 3Scale by Red Hat

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.


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 you’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);
  }
  }
}

Discover how you can achielve enterpriese agility with microservices and API management

Topics:

Published at DZone with permission of Steve Millidge. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}