8 Caching HTTP Sessions with Coherence*Web

The following example demonstrates how to use Coherence*Web to cache session information for Web application instances that are deployed across WebLogic application servers. To do this, you will create a Web application and deploy it to two application server instances. The application is a simple counter that stores the current count as a session attribute. Coherence*Web automatically serializes and replicates the attribute across both server instances. Lastly, a browser is used to access each application instance to demonstrate that the same session attribute is used among the instances.

To complete this example, the following software must be installed:

8.1 Install Coherence*Web on WebLogic 10.X

The Coherence*Web module includes a plug-in installer that supports WebLogic 9.2 MP1 and 10.3. For more information on Coherence*Web, including installation and configuration instructions, see the User's Guide for Oracle Coherence*Web.

8.2 Start a Cache Server

Start a Coherence Cache Server. Example 8-1 illustrates a sample script to start the server.

Example 8-1 Script to Start the Cache Server

setlocal
 
if (%COHERENCE_HOME%)==() (
    set COHERENCE_HOME=c:\oracle\product\coherence
) 
 
set COH_OPTS=%COH_OPTS% -server -cp c:\oracle\product\coherence\lib\coherence.jar;c:\oracle\product\coherence\lib\coherence-web-spi.war;
set COH_OPTS=%COH_OPTS% -Dtangosol.coherence.management.remote=true -Dtangosol.coherence.cacheconfig=/WEB-INF/classes/session-cache-config.xml -Dtangosol.coherence.session.localstorage=true
 
java %COH_OPTS% -Xms512m -Xmx512m com.tangosol.net.DefaultCacheServer
 
:exit

8.3 Configure the WebLogic Server

This example requires two application server instances:

  1. Run the Oracle WebLogic Configuration Wizard (Start>All Programs> Oracle Fusion Middleware>WebLogic Server 10.3>Tools>Configuration Wizard) to create a WebLogic domain called test_domain.

    Before exiting the wizard, select the Start Admin Server check box, and click Done. The configuration wizard automatically starts the administration server.

  2. Start the Server Administration Console.

    From the browser, log in to the Oracle WebLogic Server Administration Console using the following URL:http://hostname:7001/console. The console starts, and the domain home page displays.

  3. Create a machine on which to run the servers.

    From the Domain Structures window, click Environment>Machines. Click New. The Create a New Machine page displays. Enter a name for the machine (in this case, Test) and click OK.

    Figure 8-1 Creating a New Machine

    Creating a New Machine

    The Summary of Machines page should look similar to Figure 8-2.

    Figure 8-2 Summary of Machines

    Summary of Machines
  4. Create servers associated with the machine.

    Click the name of the machine in the Summary of Machines page to open the Settings for <machine> page. Click the Servers tab then Add to create a server.

  5. Select Create a new server and associate it with this machine in the Add a Server to Machine page, and click Next.

  6. Provide details about the server in the Add a Server to Machine page.

    Enter ServerA as the Server Name and 8080 as the Server Listen Port. Enter the appropriate value for the Server Listen Address. Click Finish.

    Figure 8-3 Adding a Server to a Machine

    Adding a Server to a Machine
  7. When you are returned to the Settings for machine page, repeat the previous three steps to create a second server.

    Enter ServerB as the Server Name and 8081 as the Server Listen Port. Enter the appropriate value for the Server Listen Address. Click Finish.

  8. Expand Environment in the Domain Structure menu and click Servers.

    The Summary of Servers page displays and should be similar to Figure 8-4:

    Figure 8-4 Summary of Servers Page

    Summary of Servers Page
  9. Start the Node Manager from Start>All Programs>Oracle Fusion Middleware>WebLogic Server10.3>Tools>Node Manager.

  10. From the Summary of Servers screen, click the Control tab and start both server instances.

8.4 Deploy the coherence-web-spi JAR File

The current release of Coherence provides a deployable shared library, coherence-web-spi.jar, that contains a native plug-in to WebLogic Server's HTTP Session Management interface.

To deploy the coherence-web-spi.jar file:

  1. From the Domain Structure menu, click Deployments. The Summary of Deployments page displays.

  2. Click Install. The Install Application Assistant screen displays.

  3. Use the Install Application Assistant to deploy coherence-web-spi.jar to both ServerA and ServerB.

    1. Locate the coherence-web-spi.jar file.

      Figure 8-5 Selecting the coherence-web-spi.jar File for Deployment

      Selecting the coherence-web-spi.jar File for Deployment
    2. Select ServerA and ServerB as the deployment targets.

      Figure 8-6 Choosing the Deployment Servers

      Choosing the Deployment Servers
  4. You can click Finish to skip the rest of the steps in the Install Application Assistant. The Summary of Deployments page displays after the application is deployed.

8.5 Create the Counter Web Application

The Counter Web application is a simple counter implemented as a JSP. The counter is stored as an HTTP session attribute and increments each time the page is accessed.

To create the Counter Web application:

  1. Create a standard Web application directory as follows:

    /
    /WEB-INF
    
  2. Copy the following code to a text file and save it as web.xml to the /WEB-INF directory.:

    <?xml version = '1.0' encoding = 'windows-1252'?>
    <web-app 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" 
     xmlns="http://java.sun.com/xml/ns/j2ee" version="2.5">
       <description>Empty web.xml file for Web Application</description>       
    </web-app>
    
  3. Copy the following code for the counter JSP to a text file and save the file as counter.jsp in the root of the Web application directory.

    <h3>
          Counter :
          <%
             Integer counter = new Integer(1);
             HttpSession httpsession = request.getSession(true);
             if (httpsession.isNew()) {
                    httpsession.setAttribute("count", counter);
                    out.println(counter);
             } else {
                    int count = ((Integer) httpsession.getAttribute("count")).intValue();
                    httpsession.setAttribute("count", new Integer(++count));
                    out.println(count);
             }
          %>
          </h3>
    
  4. Add a library reference for the coherence-web-spi jar file to the weblogic.xml file. Save the file in the root of the Web application directory.

    <weblogic-web-app>
         ...
          <library-ref>
               <library-name>coherence-web-spi</library-name>
              <specification-version>1.0.0.0</specification-version>
              <implementation-version>1.0.0.0</implementation-version>
              <exact-match>false</exact-match>
         </library-ref>
         ...
    </weblogic-web-app>
    
  5. The Web application directory should appear as follows:

    /
    /counter.jsp
    /weblogic.xml
    /WEB-INF/web.xml
    
  6. ZIP or JAR the Web application directory and save the file as counter.war.

8.6 Deploy the Application

To deploy the counter.war application:

  1. Open the Summary of Deployments page by clicking Deployments in the Domain Structure menu in the Oracle WebLogic Server Administration Console.

  2. Click Install. The Install Application Assistant screen displays.

  3. Use the Install Application Assistant to deploy counter.war to both ServerA and ServerB. The Summary of Deployments page displays after the application is deployed. Figure 8-7 illustrates the deployments table with the counter Web application.

Figure 8-7 Deployments Window Showing the Deployed Application

Deployments Window with the Deployed Application

8.7 Verify the Example

To verify the example:

  1. Open a browser and access the ServerA Counter instance using the following URL:

    http://host:8080/counter/counter.jsp

    The counter page displays and the counter is set to 1 as follows:

    Figure 8-8 Counter Page with Counter Set to 1

    Counter Page with Counter Set to 1
  2. From the same browser (or in a new browser tab), access the ServerB Counter instance using the following URL:

    http://host:8081/counter/counter.jsp

    The counter page displays and the counter is incremented to 2 based on the session data: If you refresh the page, the counter is incremented to 3. Refresh the instance on ServerA and the counter is at 4.

    Figure 8-9 Counter Page with Counter Set to 4

    Counter Page with Counter Set to 4