Marina Sprava, Public Relations manager at Jelastic since October 2011, has technical engineering background in systems of control and automation. Scientific interests: cloud computing, cloud hosting, agile methodologies and medical imaging. Marina is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

Running WebSocket Applications on TomEE: Step-by-step Tutorial

07.11.2013
| 3699 views |
  • submit to reddit

WebSocket is a relatively new protocol and tool component for web application development. It allows you to create applications without any HTTP overhead. The main benefits of using WebSockets are:

  • A real-time, full duplex communication between the client and the server. This means that the client and server can communicate in both routes at ones.
  • A single interface for application development, which allows  you to save a lot of your time for development.
  • Applications can handle more concurrent users and a greater message volume, with less infrastructure.
  • WebSockets can replace long polling technology, which is also used for real-time products.

In this tutorial you’ll see how WebSocket technology works in the cloud; to be more exact, in the cloud with a highly available cluster.

Create environment

1. Log in into the Jelastic dashboard and click Create environment.

2. Select TomEE as your application server and specify the cloudlet limit for it. Switch on High Availability to provide perfect fail-over capabilities for your environment and Public IPv4 to enable WebSockets to work properly.

TomEE websocket tutorial

2. After switching on high availability (HA) NGINX load balancer will be added to your environment topology automatically. Click on NGINX, specify the cloudlet limit for it and switch on Public IP, which is needed for your WebSocket Java application functionality. Then specify the name of your environment and click on the Create button.

NGINX websocket env

In a few minutes your highly available environment with TomEE will be created.

TomEE Java websockets

Create application

1. We use Echo for testing, a simple Java application bundled into Tomcat 7. It sends simple messages to the server which echoes it back. Here’s the code:

package websocket.echo;
 
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
 
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
public class EchoMessage extends WebSocketServlet {
 
private static final long serialVersionUID = 1L;
 private volatile int byteBufSize;
 private volatile int charBufSize;
 
@Override
 public void init() throws ServletException {
 super.init();
 byteBufSize = getInitParameterIntValue("byteBufferMaxSize", 2097152);
 charBufSize = getInitParameterIntValue("charBufferMaxSize", 2097152);
 }
 
public int getInitParameterIntValue(String name, int defaultValue) {
 String val = this.getInitParameter(name);
 int result;
 if(null != val) {
 try {
 result = Integer.parseInt(val);
 }catch (Exception x) {
 result = defaultValue;
 }
 } else {
 result = defaultValue;
 }
 
return result;
 }
 
@Override
 protected StreamInbound createWebSocketInbound(String subProtocol,
 HttpServletRequest request) {
 return new EchoMessageInbound(byteBufSize,charBufSize);
 }
 
private static final class EchoMessageInbound extends MessageInbound {
 
public EchoMessageInbound(int byteBufferMaxSize, int charBufferMaxSize) {
 super();
 setByteBufferMaxSize(byteBufferMaxSize);
 setCharBufferMaxSize(charBufferMaxSize);
 }
 
@Override
 protected void onBinaryMessage(ByteBuffer message) throws IOException {
 getWsOutbound().writeBinaryMessage(message);
 }
 
@Override
 protected void onTextMessage(CharBuffer message) throws IOException {
 getWsOutbound().writeTextMessage(message);
 }
 }
}

2. Now you have to package your application into a WAR file for its further deployment.

You can download this package with a few Tomcat websocket samples for testing.

Deploy application

1. Go back to the Jelastic dashboard and upload your app to the deployment manager.

Java websockets upload

2. Deploy the uploaded package to your environment. If you use Tomcat sample application like we do, deploy the package to the context, named examples.

TomEE websocket server deploy

Now you can open your application in a web browser using Public IPs of your TomEE servers and test it.

TomEE websocket server application

See!  It’s not complicated and your WebSocket works like a charm.

The instruction above is fully suitable for Tomcat 7 as well.

As you can see it’s quite easy to get your WebSocket up and running on Jelastic with its rich set of tools. If you have had any experience with WebSockets on our platform, please give us your feedback in the comments section below.

Published at DZone with permission of Marina Sprava, 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.)