Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Open source Java projects: Terracotta

A unique approach to clustering conquers scalability and fail-over

  • Print
  • Feedback

Page 3 of 4

Finally, Listing 4 shows the Web deployment descriptor for this application.

Listing 4. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>
   TeracottaSessionClusteringExample</display-name>
   <servlet>
      <description>
      </description>
      <display-name>SessionServlet</display-name>
      <servlet-name>SessionServlet</servlet-name>
      <servlet-class>com.geekcap.terracottaexample.SessionServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>SessionServlet</servlet-name>
      <url-pattern>/SessionServlet</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
   </welcome-file-list>
</web-app>

The web.xml file defines the SessionServlet and maps requests for /SessionServlet to the SessionServlet.

Package these files into a WAR file named TerracottaExample.war with the following structure:

enterYourName.jsp
hello.jsp
WEB-INF/web.xml
WEB-INF/classes/com/geekcap/terracottaexample/SessionServlet.class

Using the Terracotta Sessions Configurator

To test the sample application and build a Terracotta configuration file, you'll use the Sessions Configurator. Launch the welcome application (by executing either welcome.bat or welcome.sh), select the Sessions tab (shown in Figure 1), and choose Terracotta Sessions Configurator.

Launching the Terracotta Sessions Configurator

Figure 1. Launching the Terracotta Sessions Configurator

When the Sessions Configurator starts, click the Import button. Or, if you close the initial dialog box, you can choose File ->Import webapp. Navigate to your WAR file and click OK, as shown in Figure 2.

 Opening a WAR application

Figure 2. Opening a WAR application

The Control tab, shown in Figure 3, shows your application configured to run in two Tomcat instances.

 Two instances of the Terracotta example application

Figure 3. Two instances of the Terracotta example application

The Terracotta Sessions enabled checkbox configures Tomcat to cluster Tomcat sessions. A simple way to see the effects of the clustering is to launch the application with this checkbox disabled and hit both servers, then launch the application with the checkbox enabled and hit both servers. Give it a try:

  1. Disable the checkbox.
  2. Click Start all.
  3. Hit both servers, which are at these URLs:
    • http://localhost:9081/TerracottaExample/SessionServlet
    • http://localhost:9082/TerracottaExample/SessionServlet
  4. Click Stop all.
  5. Enable the checkbox.
  6. Repeat steps 2 through 4.

What you should notice is that when you hit the individual servers with Terracotta Sessions disabled, the servers know nothing about each other. As a matter of fact they clobber each other's session cookie, and the results are rather random. When you enable Terracotta Sessions and then enter your name into one server, the other server is immediately aware of your name. And when you change your name on one server, the change is immediately reflected on the other server.

Figure 4 shows screen shots from both of my Tomcat servers showing the same data.

  • Print
  • Feedback

Resources

More from JavaWorld