Spring MVC 3.2 Preview: Techniques for Real-time Updates

Engineering | Rossen Stoyanchev | May 08, 2012 | ...

Last updated on November 5th, 2012 (Spring MVC 3.2 RC1)

In my last post I introduced the new Servlet 3 based, async support in Spring MVC 3.2 and talked about long-running requests. A second very important motivation for async processing is the need for browsers to receive real-time updates. Examples include chatting in a browser, stock quotes, status updates, live sports results, and others. To be sure not all examples are equally delay-sensitive but all of them share a similar need.

In standard HTTP request-response semantics a browser initiates a request and the server sends a response, which means the server can't send new information until it has a request from the browser. Several approaches have evolved including traditional polling, long polling, and HTTP streaming and most recently we have the WebSocket protocol.

Traditional Polling

The browser keeps sending requests to check for new information and the server responds immediately each time. This fits scenarios where polling can be done at reasonably sparse intervals. For example a mail client can check for new messages every 10 minutes. It's simple and it works. However, the approach becomes inefficient when new information must be shown as soon as possible in which case polling must be very frequent.

Long Polling

The browser keeps sending requests but the server doesn't respond until it has new information to send. From a client perspective this is identical to traditional polling. From a server perspective this is very similar to a long-running request and can be scaled using the technique discussed in Part 1.

How long can the response remain open? Browsers are set to time out after 5 minutes and network intermediaries such as proxies can time out even sooner. So even if no new information arrives, a long polling request should complete regularly to allow the browser to send a new request. This IETF document recommends using a timeout value between 30 and 120 seconds but the actual value to use will likely depend on how much control you have over network intermediaries that separate the browser from server.

Long polling can dramatically reduce the number of requests required to receive information updates with low latency, especially where new information becomes available at irregular intervals. However, the more frequent the updates are the closer it gets to traditional polling.

HTTP Streaming

The browser sends a request to the server and the server responds when it has information to send. However, unlike long polling, the server keeps the response open and continues to send more updates as they arrive. The approach removes the need for polling but is also a more significant departure from typical HTTP request-response semantics. For example the client and server need to agree how to interpret the response stream so that the client will know where one update ends and another begins. Furthermore, network intermediaries can cache the response stream which thwarts the intent of the approach. This is why long polling is more commonly used today.

WebSocket Protocol

The browser sends an HTTP request to the server to switch to the WebSocket protocol and the server responds by confirming the upgrade. Thereafter browser and server can send data frames in both directions over a TCP socket.

The WebSocket protocol was designed to replace the need for polling and is specifically suited for scenarios where messages need to be exchanged between browser and server at a high frequency. The initial handshake over HTTP ensures WebSocket requests can go through firewalls. However, there are also significant challenges since a majority of deployed browsers do not support WebSockets and there are further issues with getting through network intermediaries.

WebSockets revolves around the two way exchange of text or binary messages. It leads to a significantly different approach from a RESTful, HTTP-based architecture. In fact there is a need for some another protocol on top of WebSockets, e.g. XMPP, AMQP, STOMP, or other and which one(s) will become predominant remains to be seen.

The WebSocket protocol is already standardized by the IETF while the WebSocket API is in the final stages of being standardized by W3C. A number of Java implementations have become available including servlet containers like Jetty and Tomcat. The Servlet 3.1 spec will likely support the initial WebSocket upgrade request while a separate JSR-356 will define a Java-based WebSocket API.

Coming back to Spring MVC 3.2, the Servlet 3 async feature can be used for long-running requests and also for HTTP streaming, techniques Filip Hanik referred to as "the server version of client AJAX calls". As for WebSockets, there is no support yet in Spring 3.2 but it will most likely be included in Spring 3.3. You can watch SPR-9356 for progress updates.

The next post turns to sample code and explains in more detail the new Spring MVC 3.2 feature.

Spring MVC 3.2 Preview: Introducing Servlet 3, Async Support

Engineering | Rossen Stoyanchev | May 07, 2012 | ...

Last updated on November 5th, 2012 (Spring MVC 3.2 RC1)

Overview

Spring MVC 3.2 introduces Servlet 3 based asynchronous request processing. This is the first of several blog posts covering this new capability and providing context in which to understand how and why you would use it.

The main purpose of early releases is to seek feedback. We've received plenty of it both here and in JIRA since this was first posted after the 3.2 M1 release. Thanks to everyone who gave it a try and commented! There have been numerous changes and there is still time for more feedback!

At a Glance

From a programming model perspective the new capabilities appear deceptively simple. A controller method can now return a java.util.concurrent.Callable to complete processing asynchronously. Spring MVC will then invoke the Callable in a separate thread with the help of a TaskExecutor. Here is a code snippet before:


// Before
@RequestMapping(method=RequestMethod.POST)
public String processUpload(final MultipartFile file) {
    // ...
    return "someView";
}

// After
@RequestMapping(method=RequestMethod.POST)
public Callable<String> processUpload(final MultipartFile file) {

  return new Callable<String>() {
    public Object call() throws Exception {
      // ...
      return "someView";
    }
  };
}

A controller method can also return a DeferredResult (new type in Spring MVC 3.2) to complete processing in a thread not known to Spring MVC. For example reacting to a JMS or an AMQP message, a Redis notification, and so on. Here is another code snippet:


@RequestMapping("/quotes")
@ResponseBody
public DeferredResult<String> quotes() {
  DeferredResult<String> deferredResult…

This Week in Spring, May 1, 2012

Engineering | Josh Long | May 01, 2012 | ...

Welcome to another installment of This Week in Spring! I'm writing the back of the room during Adrian Colyer's amazing keynote at SpringOne On The Road - London event.

  1. Did you guys miss Oleg Zhurakousky's webinar, Practical Tips and Tricks with Spring Integration? Have no fear, the video is available online.

    Also, be sure to check out part 2, this Wednesday, May 3rd for both Europe and North America!

    	</LI>
    	<LI> <a href = "http://blog.springsource.org/author/rclarkson/">Roy Clarkson</A> has announced the <a href = "http://www.springsource.org/spring-mobile/news/1.0.0.rc2-released">latest release of Spring Mobile</A>.  
    		 The release has several enhancements including more refined resolution, and improved site switching behavior. 
    		
    		</LI> 
    		<LI>  <a href = "http://blog.springsource.org/author/jbrisbin/">Jonathan Brisbin</A> just announced <a href="http://blog.springsource.org/author…

This Week in Spring - April 24th, 2012

Engineering | Josh Long | April 24, 2012 | ...

Welcome back to another installment of This Week in Spring! As I compile this, I'm eagerly waiting for Costin Leau to begin his talk on NOSQL with Spring here in sunny, and beautiful Kiev, Ukraine, the first stop in the European leg of the Cloud Foundry Open Tour. The turnout for this event's been staggering! If you're reading this, then you've already missed out on the chance to attend the Kiev event, but be sure to register for the upcoming Moscow and London events.

  1. In this SpringOne 2GX 2011 session, Mark Fisher and Thomas Risberg transform a monolithic enterprise application by changing its relational DB with a NoSQL one, introducing modularity, adding polyglot support and incorporating message queuing and event driven request processing using common enterprise integration patterns.
  2. Did you guys notice that the final edition of the excellent Spring Roo in Action has just been published?
        This book is, as Ben Alex (Spring Roo project founder) put it, "an insightful and comprehensive treatment." I (personally) can't recommend it enough. Ken Rimple and Srini Penchikala, as long time readers of this roundup will know, are frequent Spring community bloggers and 
    

    routinely provide amazing content on all things Spring.

  3. 			 <LI>  
    		Blogger Billy Sj&ouml;berg on DZone has a great post on how <a href = "http://www.dzone.com/links/r/bridging_between_jms_and_rabbitmq_amqp_using_spri.html">to bridge JMS and RabbitMQ</A>. 
    		 This example uses <a href = "http://www.springsource…

This Week in Spring: April 17th, 2012

Engineering | Adam Fitzgerald | April 17, 2012 | ...

Welcome back to another installment of This Week in Spring. This week is the last chance to sign up for the SpringOne on the Road events in London, Kiev and Moscow so be sure to register. Let's dive into it!

  1. Chris Richardson's webinar recording on NoSQL options for the Java developer is online in the SpringSourceDev YouTube Channel.
  2. Shekhar Gulati's excellent introduction to Spring Roo continues over on IBM's developerWorks portal. The latest installment introduces writing advanced (and wrapper) Spring Roo addons.
  3. <LI>  This article, which introduces how to use <a href = "http://java.dzone.com/articles/use-javafx2-spring">Spring to assemble  JavaFX 2 components</a> is short and to the point.  I'd probably use Spring's Java configuration option to fully exploit all the custom components, however. The nice thing about the approach outlined (over using FXML, directly, is that beans configured this way benefit from all the services that Spring provides, including dependency injection and AOP). Nice post, Andy!  </LI>
    
    <LI>Blogger <EM>Rob Gordon</EM> has a nice post introducing <a href ="http://rgordon.co…

This Week in Spring, April 10th, 2012

Engineering | Josh Long | April 10, 2012 | ...

What a great week! The Cloud Foundry Open Tour's well under way, having just finished the Asian and US legs of the tour. Now, onward to Europe! (If you're in Europe, now's the time to secure your spot!)

Before we continue on to the bevy of the latest and greatest content, I wanted to remind you guys to check out Spring Integration ninja Oleg Zhurakousky's upcoming webinar, Practical Tips for Spring Integration. There is, as usual, one event for North America, and one for Europe

  1. Gunnar Hillert's put together a blog introducing a feature that's received a lot of attention in SpringSource Tool Suite: easy-to-use templates for creating Spring Integration projects. Nice job, Gunnar! Also, check out Gunnar's accompanying video Create Spring Integration Projects with STS on the SpringSource YouTube channel.
  2. Michael Isvy has put together a great blog explaining a few of the things you should be aware of when upgrading to Spring 3.1. Handy!
  3.  <LI> Spring Integration 2.1.1 has been released! This is the first maintenance release of 2.1.x branch and contains the usual things like bug fixes and improvements related to AMQP, Gemfire, Mongo and Redis modules which were first introduced in Spring Integration 2.1.0. All together 56 issues were resolved with this release. 
    	 For more, consult <a href = "http://www.springsource.org/node/3520">the release announcement</A>.</LI>
    		
    <LI>  Gabriel Axel talks about the <a href = "http://www.gabiaxel.com/2012/04/spring-social-google-first-milestone-is.html">first milestone of Spring Social…

Create Spring Integration Projects using STS

Engineering | Gunnar Hillert | April 09, 2012 | ...

Just days ago, SpringSource Tool Suite™ (STS) 2.9.1 was released. Besides many new features, it provides several features that are especially exciting for Spring Integration users. First of all, the latest STS release adds support for Spring Integration 2.1 and improves the visualization capabilities for Spring Integration flows. STS supports now all recently added Spring Integration adapters such as:

Also, all existing Spring Integration adapters have been updated to support new visualization elements. Another wonderful addition to Spring Integration users is that SpringSource Tool Suite 2.9.x now ships with templating support for Spring Integration. Thus, when creating a new project using the Spring Template Project Wizard, you can now select between the following 3 new Spring Integration targeted templates:
  • Spring Integration Project (Standalone) - Simple
  • Spring Integration Project (Standalone) - File
  • Spring Integration Project (War)
The Simple template creates a basic Spring Integration project, which runs as a standalone Java application, using only core Spring Integration components. In order to illustrate File polling capabilities, the File template uses additional components to poll file directories as well as to route those files. Lastly, the War template allows users to easily create basic Spring Integration projects that are targeted to run within servlet containers (e.g. Tomcat) as part of a WAR deployment. For illustration purposes the War template uses the Spring Integration Twitter adapter.

Creating a new Spring Integration Project

In order to start a new project using STS Spring Templates, go in the main menu to File, then NEW and then Spring Template Project. The Template Selection Screen will show up next.

 

[caption id="attachment_10681" align="aligncenter" width="342" caption="The Template Selection Screen"][/caption]

This screen provides a list of all available Spring Templates, including the 3 previously mentioned new templates for Spring Integration. If you see that little arrow in front of the template name - that means that the actual template has not been downloaded, yet. Once downloaded, the Templates will be cached on your machine and you won’t need to download the template files again, unless you press the Refresh

This Week in Spring - April 3rd, 2012

Engineering | Josh Long | April 03, 2012 | ...

Welcome to another Installment of This Week in Spring The Cloud Foundry Open Tour is well underway, and have been thus far very good events to attract community.

There are still some (well, there were yesterday!) early bird spots in the upcoming shows in <a href = "http://opentour.cloudfoundry.com/2012/austin">Austin</A>, <a href = "http://opentour.cloudfoundry.com/2012/washington">Washington D.C.</a>, <a href = "http://opentour.cloudfoundry.com/2012/kiev">Kiev</A>, <a href = "http://opentour.cloudfoundry.com/2012/moscow">Moscow</A>, and <a href  = "http://opentour.cloudfoundry.com/2012/london">London</A>, so book now. 

  1. Some of the wonderful content from the Spring I/O conference is now available online! The conference, held in Spain in February of this year, is conducted in both Spanish and English, so there's a lot to like no matter which language you speak. Adrian Colyer's keynote session is super, once you get past the audio problems at the beginning. I couldn't find a SpringIO-specific hash tag, but you can pick them out of the other videos pretty easily by scrolling down. Stay tuned, there should be even more content posted, soon.
  2.  <LI> Tobias Fiohre (who  seemingly lives  <EM>only</Em> to please us, the lucky developers in the  Spring community!)  has put up…

Secure Data Binding With Grails

Engineering | Jeff Scott Brown | March 28, 2012 | ...

Introduction

The Grails Framework provides a lot of tools and techniques to web application developers to simplify solving common application development challenges.

Among those are a number of things which simplify the complicated and tedious problems often associated with data binding. In general, data binding is made very simple by Grails as it offers several techniques for binding maps of data to graphs of objects.

It is important that application developers understand the implications of each of those techniques in order to decide which is most appropriate and most secure for any given use case.

Web Application Data Binding Overview

A really common task for many web applications is for the application to accept a set of http request parameters and bind those parameters to an object. The object then might be stored in the database, used to perform some kind of calculation or used carry out some kind of application logic. In a Grails application some of that is often carried out in a controller action and the data is often being bound to a domain object.

Consider a domain class which looks something like this:

Code Listing 1

class Employee {
    String firstName
    String lastName
    BigDecimal salary
}

There might be…

This Week in Spring - March 27th, 2012

Engineering | Josh Long | March 28, 2012 | ...

Welcome to another installation of This Week in Spring. As usual, we have a lot to cover. As this post goes up, the Cloud Foundry Open Tour is underway in Beijing, and coming to a city near you, soon. This show's a very unique opportunity to learn more about Cloud Foundry and Spring from the experts - don't miss out, register today.

  1. Spring web dude Rossen Stoyanchev announced that Spring Web Flow 2.3.1 has been released. This is a maintenance release featuring an upgrade to Spring 3.1.1, and JavaServer Faces 2.1.7 along with a number of bug fixes.
  2. SpringSource Tool Suite lead Martin Lippert announced the release of the Cloud Foundry Integration for Eclipse 1.0. This release brings a complete, cohesive Cloud Foundry integration for all Eclipse distributions, including the SpringSource Tool Suite.
  3. Martin also announced the release of SpringSource Tool Suite release, 2.9.1, featuring new features and bug fixes.
  4. <LI>    <a href = "http://www.springone2gx.com/conference/speaker/mark_fisher">Mark Fisher</A> and <a href = "http://www.springone2gx.com/conference/speaker/thomas_risberg">Thomas Risberg</A>'s epic talk from <a href = "http://www.springone2gx.com">SpringOne 2GX 2011</A>  - <EM><A href = "http://www.infoq.com/presentations/Architecture-Choices-for-Scalable-Cloud-Apps">Architecture Choices for Scalable Cloud Apps</A></EM> -  that introduces how to build scalable architectures in the cloud using technologies like Spring Integration and Cloud Foundry is now up on InfoQ. 
    	  </LI>
    	
    
  5. Maciej Walkowiak has a blog that introduces how to use Spring 3.1 profiles in conjunction with some custom Tomcat configuration to activate Spring profiles without changing the deployed binary.
  6. Michal Jastak has put together a wonderful post introducing how to use Spring MVC 3.1's support for flash attributes.
  7. Tobias Flohre is at it again, this time with two posts on Spring Batch. The first post introduces the basics of transactions in Spring Batch, and the second post introduces some of the finer points of restarting cursor-based readers and writers.
     </LI> 
    
  8. Artur Mkrtchyan has a great post introducing both how to install Redis (a fast, highly optimized data-structure server) and how to use Spring Data Redis (part of the Spring Data umbrella project that facilitates access to the wide varieties of so-called NoSQL and big-data stores) to build Spring applications that talk to Redis.

Get the Spring newsletter

Stay connected with the Spring newsletter

Subscribe

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all