I recently started to check out some of the new features in Spring 4, so being able to attend the 2014 DevNexus conference in Atlanta, GA last week was timely and extremely beneficial. Craig Walls delivered an excellent high-level overview of what’s new in Spring 4. In this blog post, I want to highlight a couple of those features that really stood out to me and caught my attention.
Websockets and SockJS
Websockets is a full duplex communication channel that allows the server to talk to the browser. In typical web applications, one typically envisions the opposite: the web browser talking to the server and the server only speaking when spoken to. Websockets provides a mechanism for keeping an open channel with the browser so that the server can deliver useful data to it in a push approach versus a client pull or polling approach. I can imagine all sorts of helpful and cool uses of this technology, such as, delivering the results of a long running process or a constant stream of updates to the browser, giving the user visibility into a long running job on the server. I encourage you to research this technology in more detail, but to give a brief overview, Websockets uses HTTP for the initial handshake only which is used to negotiate a protocol switch or upgrade. This then establishes an open channel for the TCP socket to remain open and allow the client and the server to talk to each other. Websockets make sense when time sensitive information needs to be delivered to the browser, such as, in finance applications. Another use case for Websockets, is when a high frequency of messages need to be exchanged. When you have low latency tolerance and a high frequency of messages in your application, then it is time to consider Websockets. Spring 4 provides a new spring-websocket module that provides comprehensive support for websocket communication and also provides for fallback support of SockJS when a browser does not yet support the Websocket protocol. Spring continues to provide great abstractions on top of these and other protocols and does so in a consistent fashion making learning easy and painless.
Another cool new feature in Spring is the enabling of simpler REST controllers. The @RestController annotation takes advantage of @ResponseBody now being promoted to type level, meaning it can be applied to classes, interfaces and other annotations. @RestController is based on the @Controller annotation, which now specifies @ResponseBody meaning your request methods do not need to explicitly specify this. This is great because before, your controller request methods could get redundant with having to put @ResponseBody on every request method. This may be not the most earth shattering new feature, but any saved typing is cool in my book!
Another cool feature is Asynchronous REST clients. RestTemplate provides a way to make client calls to RESTful web services. AsyncRestTemplate allows developers to create more reactive event-driven REST services by allowing multiple backend REST services to be called concurrently. This makes for a very scalable application when multiple backend services need to be called concurrently.
These are just a couple of the cool new features in Spring 4 and I encourage you to research these and some of the other new features such as support for Java 8 lambdas, Groovy bean definition DSL and several new testing improvements. Check out http://docs.spring.io/spring/docs/current/spring-framework-reference/html/new-in-4.0.html for more information on Spring 4.