TomcatExpert

WebSockets in Tomcat 7

posted by fhanik on April 23, 2012 10:22 PM

With the 7.0.27 release the Apache Tomcat team introduced a WebSocket implementation. WebSocket has received a lot of hype, and has been much anticipated by Tomcat users. Let’s take a quick look at what web sockets are, what benefits and limitations they have and how they are implemented in Apache Tomcat 7.

What is a WebSocket?

WebSocket is considered the next step in evolution of web communication. Over time, communication has evolved in steps to reduce the time and data throughput for the application to update a user’s browser. The evolution has looked a little like this:

  • Entire page reloads
  • Component reloads using AJAX Processing
  • Comet communication
    • Long poll– similar to AJAX, but not holding a thread on the server
    • Bi directional- two way communication over the same TCP

Each of these steps had their benefits and challenges. Apache Tomcat 6 implements bi-directional communication over HTTP using its Comet Processor. This implementation allowed for asynchronous event driven request processing as well as bi-directional communication. This implementation had a few limitations.

  1. Given that HTTP was intended to be request/response protocol rather than a bi directional protocol, proxies and other intermediaries may not work properly, and only forward packets in one direction at any given time.
  2. The programming model becomes more difficult as this introduces multi threading to the servlet developer
  3. Non-standard APIs made adoption difficult

Comet proved that the proverb “build it and they will come” is not necessarily true. Each major servlet container had its own implementation and non-standard APIs. Adoption was fairly sporadic.

The Servlet 3.0 release introduced a new feature called Asynchronous Servlets. This implementation standardized and simplified most Comet implementations by removing the bi-directional requirement of the protocol but retaining the ability to disassociate a container thread from the HTTP request. We can compare this feature to the server version of client AJAX calls. It lets a request hang until a response is ready to be delivered without using worker thread on the container. This feature offers little new over Apache Tomcat’s Comet Processor in terms of functionality. The fact that it is a standard Servlet API, gives frameworks an easier way to build on top of it.

Asynchronous servlets address some of the need in web communication, but not all, and the lack of bi-directional communication made its use still limited. If you take standard API aside, it may have been considered step back from most Comet implementations.

WebSockets is another attempt to standardize an idea of having asynchronous, event driven and bi-directional communication over HTTP. The standardization comes in form of a JavaScript API as well as a communication protocol. While this highlights the lack of a standard server-side API the Servlet 3.1 expert group are already discussing including some basic level of support for this and discussions are active as we are writing this article. Whether that will evolve into a full WebSocket API remains to be seen. In the meantime major servlet container are publishing non- standard APIs, Tomcat being no different.

So let’s summarize what a WebSocket implementation gives us:

  1. Bi-directional communication over a HTTP port by upgrading/switching protocols. Initiated by a HTTP request.
  2. Message/Frame based communication
  3. Intended to work with proxies and intermediaries

What does it look Like?

On the protocol level there is one major difference from what has been done previously, and that is that the WebSocket switches the protocol away from HTTP. This is where a proxy can change its behavior and support this type of messaging as well. Whether this is in theory only still remains to be seen. If we look at a WebSocket client initiating a request we can see that the intermediary has to, at a minimum,

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade

Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

understand the “Connection:upgrade” headers. The response contains the critical status code of 101

HTTP/1.1 101
Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

indicating that a protocol switch has been confirmed and is now expected. After this handshake is complete the client and server move away from request/response communication and can send messages independently of each other.

Does the WebSocket protocol address all the limitations of previous Comet implementations?

In short: not completely. First, proxies and intermediaries may still not work – so deployments using Wide Area Networks (WANs) are questionable. Secondly, there are still no standard Java APIs. However, given the buzz around WebSocket, this may be addressed soon.

Another thing to be careful for is that every HTTP/TCP connection requires the initial handshake and introduces a message roundtrip before you even send or receive data over the WebSocket protocol. If you’re not careful, and have very short interactions, WebSocket may be too much overhead. WebSocket are ideal for longer running conversations between client and server.

The good news is that WebSocket has benefited from a lot more attention, especially client side and cross language than any other previous attempt at enhancing web communication. It has also received a competitor for bi-directional communication, and that is SPDY, an implementation also underway within the Apache Tomcat project.

Summary

WebSocket is a straightforward implementation of bi-directional communication over HTTP. The API is part of the HTML 5 specification and so far many different server side implementations are available. It is not without risks, since the Java APIs are yet to be standardized so implementations can vary. Additionally, working with proxies and intermediaries may still not work, however it is important to note that WebSocket utilizes the Upgrade feature of HTTP to switch protocols after the initial request/response handshake has been completed. This gives future proxies an option to reject or support the WebSocket protocol over WAN.

The first released version of WebSocket in Apache Tomcat was released with Apache Tomcat 7.0.27.

Filip Hanik is a Senior Software Engineer for the SpringSource Division of VMware, Inc. (NYSE: VMW) and a key participant in the company's Apache Tomcat initiatives. Filip brings 15 years of extensive experience in architecture, design and development of distributed application frameworks and containers and is recognized for his top-quality system development skills and continuous participation of Open Source development projects. Filip is an Apache Software Foundation member and a committer to the Apache Tomcat project where he is a leading authority on Tomcat clustering and a key contributor to the core of the platform. Filip has made contributions to software initiatives for Walmart.com, Sony Music, France Telecom and has held a variety of senior software engineering positions with technology companies in both the United States and Sweden. He received his education at Chalmers University of Technology in Gothenburg, Sweden where he majored in Computer Science and Computer Engineering.

Comments

Tomcat maven plugin for version 7.0.27

Hello Filip,

I wonder where to download a maven tomcat plugin that works with the referred version.

BTW, awesome functionality delivered by the Tomcat team guys.

jD @ http://pragmatikroo.blogspot.com

This is an awesome post. Just

This is an awesome post. Just one of the very best post I’ve ever seen. What a really good and awesome post. Keep up your work on articles! Power Qube - OneSolar

Robinjack

I can see that you are a specialist at your field! I am propelling a site soon, and your data will be exceptionally valuable for me.. A debt of gratitude is in order for all your help and wishing all of you the accomplishment in your business. cat spraying

sl786982

It has sufficient amps to weld the iron resurrection of effortless maintenance after a total drain. medical symptom checker

sl786982

sl786982

If you are looking for cheapest Korg keyboards for sale then this is the site for you. Find the widest range of Korg Keyboard reviews. online medical symptom checker

sl786982

I appreciate everything you

I appreciate everything you have added to my knowledge base. Admiring the time and effort you put into your blog and detailed information you offer. Thanks. 3kw solar system - VicSolar

qwewq

Very informative blog. Great.

Watch The Mummy Online
Watch Alien Covenant Online
Watch Kong Skull Island Online
Watch Logan Online
Watch Beauty and the Beast Online
Watch Cars 3 Online
Watch The Circle Online
Watch A United Kingdom Online
Watch Split Online
Watch Underworld Blood Wars Online
Watch Resident Evil The Final Chapter Online
Watch The Shack Online
Watch King Arthur Legend of the Sword Online
Watch All Eyez on Me Online
Watch The Fate of the Furious Online
Watch Sing Online
Watch Doctor Strange Online
Watch It Comes at Night Online
Watch Jack Reacher 2 Never Go Back Online
Watch A United Kingdom Online
Watch Despicable Me 3 Online
Watch Fifty Shades Darker Online
Watch Tulip Fever Online
Watch Smurfs The Lost Village Online
Watch Gold Online
Watch John Wick Chapter 2 Online
Watch Life Online
Watch XXX Return of Xander Cage Online
Watch Inferno Online
Watch Pirates of the Caribbean 5 Dead Men Tell No Tales Online
Watch The Founder Online
Watch Wonder Woman Online
Watch A Cure for Wellness Online
Watch Baywatch Online
Watch Gifted Online
Watch Rough Night Online
Watch XXX Return of Xander Cage Online
Watch Guardians of the Galaxy 2 Online
Watch The Accountant Online
Watch Manchester by the Sea Online
Watch Arrival Online
Watch Tulip Fever Online
Watch It Comes at Night Online
Watch The Space Between Us Online
Watch Life Online
Watch The Shack Online
Watch Collateral Beauty Online
Watch Collateral Beauty Online
Watch Billy Lynns Long Halftime Walk Online
Watch The Circle Online
Watch Ghost in the Shell Online
Watch Transformers 5 The Last Knight Online
Watch The Lego Batman Movie Online
Watch Captain Underpants Online
Watch The Edge of Seventeen Online
Watch Kong Skull Island Online
Watch Power Rangers Online
Watch Transformers 5 The Last Knight Online
Watch All Eyez on Me Online
Watch A Dogs Purpose Online
Watch Alien Covenant Online
Watch The Fate of the Furious Online
Watch Trolls Online
Watch Assassins Creed Online
Watch Beauty and the Beast Online
Watch Gifted Online
Watch Rough Night Online
Watch The Lego Batman Movie Online
Watch Trolls Online
Watch Rogue One Online
Watch Power Rangers Online
Watch The Great Wall Online
Watch The Mummy Online
Watch The Accountant Online
Watch Captain Underpants Online
Watch Arrival Online
Watch The Boss Baby Online
Watch A Cure for Wellness Online
Watch Fantastic Beasts and Where to Find Them Online
Watch Manchester by the Sea Online
Watch Baywatch Online
Watch The Space Between Us Online
Watch Inferno Online
Watch Logan Online
Watch A Monster Calls Online
Watch Cars 3 Online
Watch Fantastic Beasts and Where to Find Them Online
Watch Gold Online
Watch The Founder Online
Watch A Dogs Purpose Online
Watch Free Fire Online
Watch Jack Reacher 2 Never Go Back Online
Watch Guardians of the Galaxy 2 Online
Watch Fifty Shades Darker Online
Watch Sing Online
Watch Rogue One Online
Watch Wonder Woman Online
Watch Moana Online
Watch Passengers Online
Watch Ghost in the Shell Online
Watch John Wick Chapter 2 Online
Watch Billy Lynns Long Halftime Walk Online
Watch Assassins Creed Online
Watch Smurfs The Lost Village Online
Watch Despicable Me 3 Online
Watch The Boss Baby Online
Watch Resident Evil The Final Chapter Online
Watch Pirates of the Caribbean 5 Dead Men Tell No Tales Online
Watch The Great Wall Online
Watch La La Land Online
Watch A Monster Calls Online
Watch King Arthur Legend of the Sword Online
Watch Underworld Blood Wars Online
Watch Doctor Strange Online
Watch Split Online
Watch Passengers Online
Watch La La Land Online
Watch The Edge of Seventeen Online
Watch Free Fire Online
Watch Moana Online
This is a wonderful article,

Robinjack

Much appreciated you all that much to share these connections. Will look at this.. analytics for instagram

Robinjack

Howdy. I discovered your web journal utilizing msn. This is an extremely elegantly composed article. I'll make certain to bookmark it and return to peruse a greater amount of your helpful information. A debt of gratitude is in order for the post. I'll certainly return. Custom medals

Anaheim Teeth Cleaning

We all know that good oral hygiene, including teeth cleaning, keeps your teeth looking clean and shiny, but did you know that it’s also essential to your overall health? It’s true, poor oral hygiene can lead to a variety of dental and medical problems such as gum disease, infection, heart disease, stroke, and even diabetes.Anaheim Teeth Cleaning

Robinjack

Your site is truly cool and this is an awesome motivating article. Much thanks to you to such an extent. more Info

Robinjack

I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up.. goth apparel

Robinjack

This is such a great resource that you are providing and you give it away for free. full article

Robinjack

I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up.. best personal trainer west la

Robinjack

Fascinating and stunning how your post is! It Is Useful and accommodating for me That I like it all that much, and I am anticipating Hearing from your next.. http://sephorabeverlyhills.com/

This is a decent post. This

This is a decent post. This post gives genuinely quality data. I'm unquestionably going to investigate it. Truly extremely valuable tips are given here. Much obliged to you to such an extent. Keep up the benevolent acts. http://rolexsubmariner.shop/

Robinjack

You are very brave focuses in this article. I would have never thought to be any of these on the off chance that I didn't go over this. Much obliged!. majestic theatre tickets

You have done a great job. I

You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site. one solar

threading with websockets

Hi,
Thanks for the article. From a quick look at a thread dump a websocket servlet on a NIO connector with an open connection seems to occupy an executor thread for the life of the connection?
Seems like an obvious scalability problem?
Also the connections seem to be closed automatically on some sort of timeout, do you know what the configuration value for that is?

The stack of the occupied thread is from a thread dump it (note its an exec thread):

"http-nio-8443-exec-14" daemon prio=5 tid=7f9b571ec000 nid=0x119dab000 waiting on condition [119daa000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <7b94fd1b0> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:196)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(AbstractQueuedSynchronizer.java:1011)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(AbstractQueuedSynchronizer.java:1303)
at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:253)
at org.apache.tomcat.util.net.NioEndpoint$KeyAttachment.awaitLatch(NioEndpoint.java:1519)
at org.apache.tomcat.util.net.NioEndpoint$KeyAttachment.awaitReadLatch(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioBlockingSelector.read(NioBlockingSelector.java:174)
at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:246)
at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:227)
at org.apache.coyote.http11.upgrade.UpgradeNioProcessor.readSocket(UpgradeNioProcessor.java:137)
at org.apache.coyote.http11.upgrade.UpgradeNioProcessor.read(UpgradeNioProcessor.java:110)
at org.apache.catalina.websocket.WsFrame.nextFrame(WsFrame.java:213)
at org.apache.catalina.websocket.WsInputStream.nextFrame(WsInputStream.java:68)
at org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:112)
at org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:563)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1620)
- locked <7b8111018> (a org.apache.tomcat.util.net.SecureNioChannel)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

Robinjack

Much obliged for each other useful site. The spot else might just I understand that sort of data written in such a perfect means? I have an endeavor that I"m a little while ago working on, and I have been vigilant for such data. read this

blocked threads

I see a similar stack with websockets holding threads open on 7.0.30 and 7.0.39. This could be a real problem

Name: http-bio-4415-exec-5
State: RUNNABLE
Total blocked: 50 Total waited: 112

Stack trace:
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:161)
java.net.SocketInputStream.read(SocketInputStream.java:132)
org.apache.coyote.http11.upgrade.UpgradeBioProcessor.read(UpgradeBioProcessor.java:81)
org.apache.catalina.websocket.WsFrame.nextFrame(WsFrame.java:214)
org.apache.catalina.websocket.WsInputStream.nextFrame(WsInputStream.java:68)
org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:149)
org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:587)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
- locked org.apache.tomcat.util.net.SocketWrapper@ddb0d168
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1156)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:626)
java.lang.Thread.run(Thread.java:780)

GAME HACK

8 ball pool Acknowledges for penmanship such a worthy column, I stumbled beside your blog besides predict a handful advise. I want your tone of manuscript... coc hack

Just keep writing what you are excited about

Just keep writing what you are excited about, and the visitors will come to your post. Situs Kesehatan Veherba
obat kanker payudara herbal
obat penyakit stroke

After checking out a number

After checking out a number of the articles on your site, I honestly appreciate your technique of writing a blog. I saved as a favorite it to my bookmark webpage list and will be checking back in the near future. Please visit my website as well and let me know your opinion.
Mortgage Broker Calgary

To take the full time to talk

To take the full time to talk about around see post, exemplary info on your site, thanks. Incredible understanding you've with this, it is good to locate a site that details information that is so much about designers that are various.

I use basically superior

I use basically superior fabrics : you will discover these products by: seguridad privada

Actually I read it yesterday

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written. Love Traction Lines Review

Digital AdvantEdge

Howdy. I discovered your web journal utilizing msn. This is an extremely elegantly composed article. I'll make certain to bookmark it and return to peruse a greater amount of your helpful information. A debt of gratitude is in order for the post. I'll certainly return. Lapel pins custom

Fat Diminisher System | Testerone XL Review

testosterone xl I propose merely very good along with reputable data, consequently visualize it: http://finance.yahoo.com/news/fat-diminisher-system-wesley-virgin-124000642.html

Till they've been waste

Till they've been waste collected nevertheless, when the software is reloaded film making, the present available contacts within the previous swimming wouldn't shut.

Robinjack

I truly like your thoughts on the issue. I now have an unmistakable thought on what this matter is about.. Custom Fidget Spinner

Prima Doula

Find the best essays on Postpartum Doula Support Calgary is my friend's profile page.

Robinjack

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Daniel

Pak Readers

I encourage you to read this text it is fun Books described ...

Top Cool Websites

On this subject internet page, you'll see my best information, be sure to look over this level of detail.
top websites

Budget Box Guy Ltd

Very interesting information, worth recommending. However, I recommend this:
bedding stores calgary

Deskgram

It is especially decent, though look into the tips during this home address.
instagram for pc

I actually enjoy your manner

I actually enjoy your manner of composing a website after looking at numerous the posts in your website such a good point. I you will check in the long run and saved like a preferred it to my bookmark website checklist. Please visit with my site aswell and I would like to understand your view.

BlackMen

gives frameworks an easier way to build on top of it.life coach atlanta

Robinjack

If you don't mind share more like that. stolen identity

Thomasqq

You will find there's means anyone produce your current websites that creates people preserve ever coming back. It’s how we employ your current words and phrases for you to communicate the details.
Pro DJ

Robinjack

You finished a couple fine focuses there. I did an inquiry on the subject and discovered about all persons will oblige with your online journal. http://buysonyvaio.com/

It is and so wonderful in

It is and so wonderful in addition to resourceful. I just now appreciate this colorings in addition to the person who obtains the item from the send will likely be happy.
Jual PC HP

Robinjack

Much obliged to you again for all the information you distribute,Good post. I was extremely keen on the article, it's entirely motivating I ought to concede. I like going to you site since I generally go over fascinating articles like this one.Great Job, I enormously acknowledge that.Do Keep sharing! Respects, http://homedepotlosangeles.com/

This is a great inspiring

This is a great inspiring article.I am pretty much pleased with your good work. pvc vloeren vriezenveen

After reading your article I

After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. the respect principle reviews

nice

Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know.
geometry dash| sniper games |happy wheels | happy wheels 2 |agario| my little pony games mickey mouse games

Very interesting website what

Very interesting website what Google did to me. Iam most definately thinking about that one, although a lot of sites I observe nowadays do not actually supply something that Iam thinking about. Simply thought that I let and would publish you know.

Looking at your content I was

Looking at your content I was surprised find this. I know that you describe it very well. And Hopefully other visitors will also experience how I experience after studying your content. I really experience thankful that I just look at this. It is very useful and very useful and I really many userful factors here from it.

Health Insurance in

Health Insurance in Singapore I am cheerful to discover this post Very valuable for me, as it contains parcel of data. I Always want to peruse The Quality and happy I discovered this thing in you post. Much obliged bupa international singapore

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.