Child pages
  • Sessions Tutorial

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Include Page
Sessions Tutorial
Wiki Markup

Wiki Markup


h1. Clustering a Spring Web Application

h2. Purpose
This tutorial shows how to configure a web application for session clustering using Terracotta Sessions. By the end of the tutorial, you should be ready to use Terracotta Sessions on your own Web applications.

h2. What You'll Need

# Terracotta Software
# [The Spring Framework|] \- We'll be using the _Spring JPetStore_ example application.
# [Apache Ant|]

h2. What You'll See

At the end of the tutorial, you will have multiple application servers running a web application with clustered sessions.

h2. Overview

The basic steps of this tutorial are:
# [Download and install _Terracotta_.|#getting_started]
# [Download Spring and build the Spring JPetStore sample.|#build_it]
# [Start the JPetStore database.|#start_db]
# [Start the Terracotta Sessions Configurator|#start_configurator]
# [Import the JPetStore WAR into the Terracotta Sessions Configurator.|#import_war]
# [Start the application servers without Terracotta enabled.|#start_app_without_tc] Verify that the application works without _Terracotta_ and that its sessions are not clustered.
# [Restart the application with Terracotta enabled.  Verify that session clustering works.|#start_app_with_tc]
# [Inspect the clustered sessions object graph.|#inspect_session_graph] _Terracotta_ enables runtime visibility into live clustered objects.


h2. Getting Started

# Download and install {html}<a href="/dl/">Terracotta</a>{html}.
# Download and unzip [The Spring Framework|].  We'll be using the _JPetStore_ sample application that comes with the _Spring Framework_ kit.
# Download and install [_Apache Ant_|] if you don't have it installed already.  Detailed instructions for installing _Ant_ are in the [Apache Ant Manual|]


h2. Build the Spring JPetStore Sample

*Note*: For more detailed instructions on building the _JPetStore_ sample, see the _readme.txt_ file in the jpetstore samples directory.
# Change directories to _<spring_dir>/samples/jpetstore_
# Run _ant warfile_

%> cd <spring_dir>/samples/jpetstore
%> ant warfile
This will leave the _JPetStore_ WAR file in _<spring_dir>/samples/jpetstore/dist/jpetstore.war_&nbsp;


h2. Start the JPetStore Database

# Change directories to _<spring_dir>/samples/jpetstore/db/hsqldb/_
# Run the _server.sh_ script.

%> cd <spring_dir>/samples/jpetstore/db/hsqldb/ %> sh

h2. Start the Terracotta Sessions Configurator

You can [obtain the Sessions Configurator from the Terracotta forge|]. See the [README.txt|] for information on how to run the Sessions Configurator. See the [Sessions Configurator Guide] for an introduction to the Sessions Configurator.

h2. Import the JPetStore WAR

*Note:* If the welcome screen is not visible, you can use the _File -> Import..._ menu.

Import the _JPetStore_ application by clicking the _Import..._ button.


Use the file chooser to select _<spring_dir>/samples/jpetstore/dist/jpetstore.war_



h2. Start the Application Servers Without Terracotta

Ensure that the _Terracotta Sessions enabled_ checkbox is _NOT_ selected.  Click the _Start all_ button to start the application servers.

This will start two instances of the application server in two separate Java Virtual Machines. Each of these application servers has its own instance of the web applications deployed inside them. The first few applications are _Terracotta_ sample applications. You should see the _JPetStore_ application listed below.


h2. View the Application Unclustered

When the application servers have started, the hyper links to the different instances of the web applications will become active.

Click on the _[http://localhost:9081/jpetstore/_]_ link.  This will open your browser to the application server instance listening on port _9081_.


Click on the _Enter the Store_ link.


You should see the _JPetStore_ homepage.


*Note:* If you get an error that says *"Could not open JDBC Connection for transaction"*, the _JPetStore_ database is not running. [Go back to that step|#start_db] and start the database.

Browse to one of the product pages and add an item to your shopping cart. Your cart should look something like this:


Now switch back to the _Terracotta Sessions Configurator_ and click on the [http://localhost:9082/jpetstore/] link. This will open your browser to the application server instance listening on port _9082_.


In the new browser window, click the shopping cart icon.  You should see an empty shopping cart.


Because the two application servers, _9081_ and _9082_, are running in separate JVMs and they aren't clustered, the session data that contained your shopping cart items is lost when you move from one application server to the next. The result is frustrated customers and lost sales.


This is precisely what happens when multiple application servers are load balanced and user sessions move from one application server to another. This may happen, for example, in the event that one or more application servers have crashed or have been taken down for maintenance.

Now let's watch what happens to your shopping cart when _Terracotta_ session clustering is turned on.


h2. Start the Application Servers With Terracotta

Switch back to the _Terracotta Sessions Configurator_ and stop the application servers by clicking on the _Stop all_ button.

When the servers have stopped, click on the _Terracotta Sessions enabled_ checkbox.


Click on the _Start all_ button and, this time, the application servers will start with _Terracotta_ session clustering enabled.


When the servers have started, go through the same process of adding items to you cart in one server and checking the contents of the cart in the other server:
# Click on the [http://localhost:9081/jpetstore/] link.
# Enter the store and add an item to your cart.
# Switch back to the _Terracotta Sessions Configurator_ a click on the [http://localhost:9082/jpetstore/] link.
# In the new browser window, browse to the shopping cart.  Your shopping cart from the server on port _9081_ should be intact on the server on port _9082_.



h2. Inspect the Clustered Session Object Graph

Switch back to the _Terracotta Sessions Configurator_ and click on the _Monitor_ tab at the top of the window.


Expand the _Roots_ tree and select the _tc:tomcat_session_jpetstore_ element.

This is a real-time view of the clustered session object graphs. These are the plain Java objects that are currently in your session.


*Note:* to refresh the object graph view, click on the element or select it and press _F5_ on your keyboard.  You can also right-click on an element to see a context menu.

Explore the object tree to see what's in the session. You should be able to find individual shopping cart items in the graph. Try changing the quantities of certain items in your cart and updating the object graph view in the _Terraotta Sessions Configurator_. You should be able to see the changes to those objects in the object graph view.


h2. Conclusions

This tutorial has taken you through the process of importing a web application into the _Terracotta Sessions Configurator_ and viewing the application with _Terracotta_ session clustering disabled and enabled. _Terracotta Sessions Configurator_ is an easy way to get started with _Terracotta_ and to watch the behavior of your clustered application at runtime.

h2. Resources

* [_The Spring JPetStore_|] sample application was written by Juergen Hoeller
* _The Spring JPetStore_ sample application is based on Clinton Begin's _[_JPetStore_|]__._
* [Manually Configuring Web Applications|docs:Configuration Guide and Reference#web applications]
* [Terracotta Sessions Configurator Guide|docs:Sessions Configurator Guide]