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

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

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)

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

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.

Prima Doula

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

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

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

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

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.

Websites Moving Problem

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.

roblox robux generator | Hack Agario | Pixel Gun 3D Cheats

You could be allowed to put

You could be allowed to put up manufacturers, except for shortcuts, in the event that they've been recognized not to mention concerning content.
Cheap ClubFlyers

API remains to be seen. In

API remains to be seen. In the meantime major servlet container are publishing non- standard APIs, Tomcat being no different.cccam server

Looking at your material I

Looking at your material I was amazed discover this. I know that you explain it very well agree with. And Hopefully other guests will also encounter how I encounter after learning your material. I really encounter grateful that I just look at this. It is very useful and very useful and I really many userful aspects here from it.

I wanted to thank you

I wanted to thank you for this excellent read. I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. http://www.moniquehaarenmakeup.nl/

MXQ PRO

It's difficult to get learning RUSSIAN in blogging world, but your tips are so helpful. Looks like I've got some work ahead of me! :) Thanks for the great read!firefly tv box wintel box

MXQ PRO

Admiring the time and effort you put into your blog,thanks.mxq plus firmware
wintel w8 pro

such an amazing information

such an amazing information
كلاش اوف كلانس

An intriguing discussion is

An intriguing discussion is worth comment. There's no doubt that that you ought to publish more about this subject matter, it might not be a taboo subject but usually people do not speak about such subjects. To the next! Many thanks!!
Edmonton Mortgage Broker
Life Insurance Vancouver
Life Insurance Calgary

great

Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
five nights at freddy's 3 five nights at freddy's 4 five nights at freddy's 2 five nights at freddy's

An interesting dialogue may

An interesting dialogue may be worth remark. Often although there is no question that that you simply must submit more about that subject material buy kids furniture, it could not be considered a taboo topic individuals don't talk about such topics. To another! Thank you!!

Good to become visiting with

Good to become visiting with your site again, it's been weeks for me personally. Well this informative article that I have been waited for such a long time Visit This Link. I want my task to be completed by this article within the university, and contains same subject together with your post. Excellent share, cheers.

Awesome to be going to your

Awesome to be going to your weblog site again, it has been several weeks for me. Well this post that i've been patiently waited for so lengthy blog here . I need this post to finish my task in the higher education, and it has same subject with your content. Thanks, excellent discuss.

best stock trading system

One of the many blog sites using the net, I like perusing joining your the foremost; most people make available you and me such a lot of material within smallish section. I'm sure on the lookout for not to mention as there are a spirit for the purpose of helpful penning, I just determined you would purpose everybody in your best suited place. How can you guidance? best stock trading system

Among the several sites that

Among the several sites that are blog using the web, I love reading joining your the foremost people offer me and you such a great deal of substance within area that is smallish. I am positive looking for and of course as certainly a nature are with the objective of useful writing link, I simply decided everyone would be purposed by you inside your ideal location. How will you assistance?

OC Housing News

Such sites are important because they provide a large dose of useful information ...
Housing Market News

Among the several websites

Among the several websites that are weblog using the web, I really like studying becoming a member of your the major individuals provide me and you such a good cope of material within place that is small read this. I am beneficial looking for and of course as certainly a characteristics are with the reason for useful composing.

Pretty good post

agario game - agario is a massively multiplayer action game developed by Matheus Valadares. agario a multiplayer browser game in which players have to eat competitors or be eaten while striving to dominate the world of colorful cells. agario game can be played in a deathmatch or between teams. To control the game agar io you just need to use your mouse/track pad, the "w" key and the SPACEBAR. The mouse navigates your blob around and the SPACEBAR allows you to split your cell(s) in half and the "w" key allows you to eject mass for feeding other players, viruses or help you drop some weight to get out of a tough spot. Enjoy the world of agar io skins!
Visit to play: Agario jeu est un jeu affronte des milliers de concurrents. Play SuperMechs online for free - The best Super Mechs - super mechs 2 games series, defeat the enemy to win and level up. Play Atari breakout is a very easy game to play. Atari breakout - atari breakout game began with eight rows of blocks, each two rows had different colors.
Play the free online game Chaos Faction - Click to play Chaos Faction 2 full games. The best place to play Chaos Faction series. Atari Breakout: Using a single ball and must knock down as many bricks as possible by Atari Breakout Game using the paddle to ricochet the ball against the bricks and eliminate them. Play Atari Breakout online.
[url=http://mahjongfreegamesonline.com]mahjong games[/url] Play now, have fun! Play [url=http://run3play.com]Run3[/url] and [url=http://run3play.com/run-2.html]Run2[/url] games free, play this popular game and get fun with us. [url=http://headsocceronline.com]football heads[/url] is a famous football game that you can play on all browsers

Thanks

Thanks for this.
SMART TOOL FOR ADVERTISE
IM Conversion download
IM Conversion REVIEW | IM Conversion bonus | IM Conversion features
Click here | See more

<a href="http://salesenvyreviewz.net/" rel="dofollow">SALESENVY REVIEW</a>

<a href="http://salesenvyreviewz.net/" rel="dofollow">SALESENVY REVIEW</a>

ladwasur

get free gems and gold in clash royale

great

Clash royale cheats | score hero hack

Good article

My name is Marcelino (34 years old) and my hobbies are Boxing and Baseball. Review my page ... Triche Drift Zone 2 Disney Magic Kingdoms Triche

March of Empires Triche

juegosfriv2017.net, juegosfriv2016.org

Ma tänan teid, hr president, Euroopa Parlamendi nimel, meile öeldud väljendusrikaste sõnade eest ja soovin Portugalile igakülgset edu meie ühise Euroopa huvides. Juegos Friv 4 Juegos Friv 100 Juegos Friv 5 Juegos De Friv 3 Juegos Friv 3 Korralise arutlusvõimaluse võimaldamine ühiste lähenemisviiside üle ohutuse ja keskkonnakaitse valdkondades käesoleva lepingu kohaldamisala piires ning lennundusohutuse probleemide alase teabevahetuse üle,
Juegos De Friv 4 Friv 10 Gry Friv 10 Gry Friv 1000 Y1000 sealhulgas konsulteerimine kavandatavate uute ohutusmeetmete ning juba olemasolevate ohutusmeetmete muutmise teemadel.

Post new comment

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