13 Caching Sessions with Managed Coherence Servers

This exercise describes how to cache session information for Web application instances that are deployed across WebLogic Server instances. It highlights the use of managed servers, which allows you to use Coherence data caches and seamlessly incorporate Coherence*Web for session management.

If you do not already have the current WebLogic Server release, you can get it at the following URL:

http://www.oracle.com/technology/software/products/middleware/index.html

This chapter has the following sections:

13.1 Introduction

WebLogic Server includes features that enable deployed applications to use Coherence data caches and seamlessly incorporate Coherence*Web for session management and TopLink Grid as an object-to-relational persistence framework. Collectively, these features are referred to as Managed Coherence Servers.

About Managed Coherence Servers

Managed Coherence Servers are employed by applications running on WebLogic Server and provides replicated and distributed caching services that make an application's data available to all servers in a Coherence data cluster. Applications can obtain direct access to data caches either through resource injection or component-based JNDI lookup. You can display, monitor, create, and configure Coherence clusters using the WebLogic Server Administration Console and WLST.

Using Managed Coherence Servers with WebLogic Server instances enable you to create a data tier dedicated to caching application data and storing replicated session state. This is separate from the application tier, where the WebLogic Server instances are dedicated to running the application.

Using Coherence*Web with Managed Coherence Servers enables you to provide Coherence-based HTTP session state persistence to applications. Coherence*Web enables HTTP session sharing and management across different Web applications, domains, and heterogeneous application servers. Session data can be stored in data caches outside of the application server, thus freeing application server heap space and enabling server restarts without losing session data.

Coherence and Coherence*Web are included in the default installation of WebLogic Server. The Coherence and Coherence*Web libraries (coherence.jar and coherence-web.jar) are included in the system classpath of WebLogic Server. These libraries will load application classes with the appropriate classloader in WebLogic Server. This means that you do not have to include the coherence.jar or coherence-web.jar files in the web application's classpath.

For more information on the integration of Oracle WebLogic Server, Coherence, and Coherence*Web, see Oracle Fusion Middleware Administering HTTP Session Management with Oracle Coherence*Web. For more information on Managed Coherence Servers, see Oracle Fusion Middleware Developing Oracle Coherence Applications for Oracle WebLogic Server.

About Coherence Clusters

Coherence clusters are different than WebLogic Server clusters. They use different clustering protocols and are configured separately. Multiple WebLogic Server clusters can be associated with a Coherence cluster and a WebLogic Server domain can contain only a single Coherence cluster. Managed servers that are configured as Coherence cluster members are referred to as managed Coherence servers.

Note:

Using multiple Coherence clusters in a single WebLogic Server domain is not recommended.

Managed Coherence servers can be explicitly associated with a Coherence cluster or they can be associated with a WebLogic Server cluster that is associated with a Coherence cluster. Managed Coherence servers are typically setup in tiers based on their type: a data tier for storing data, an application tier for hosting applications, and a proxy tier for allowing access to external clients.

For more information on Coherence clusters in a WebLogic server environment, see "Configuring and Managing Coherence Clusters" in Administering Clusters for Oracle WebLogic Server.

13.2 Caching Session Information for Web Application Instances

The following example demonstrates how Managed Coherence Servers and Coherence*Web caches session information for Web application instances that are deployed across WebLogic Server instances. To do this, you will create a Web application and deploy it to two WebLogic Server instances that belong to a Coherence cluster. 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. A browser is used to access each application instance to demonstrate that the same session attribute is used among the instances.

  1. Configure and Start WebLogic Server

  2. Create the WebLogic Servers

  3. Create a Coherence Cluster

  4. Enable a Server for Coherence*Web Local Storage

  5. Create the Counter Web Application

  6. Start the WebLogic Servers

  7. Deploy the Application

  8. Verify the Example

13.2.1 Configure and Start WebLogic Server

The following instructions assume that you have installed WebLogic Server in the default location: C:\Oracle\Middleware\Oracle_Home.

To configure and start WebLogic Server:

  1. Run the Oracle WebLogic Configuration Wizard to create a new WebLogic Server domain.

  2. In the Create Domain page, select the Create a new domain option. In the Domain location field, enter C:\Oracle\Middleware\Oracle_Home\user_projects\domains\test_domain as the domain name and location.

  3. In the Templates page, ensure that the default Basic WebLogic Server Domain is selected.

  4. In the Administrator Account page, enter a password for the domain.

  5. In the Domain Mode and JDK and the Advanced Configuration pages, accept the defaults.

  6. In the Configuration Summary page click Create.

  7. In the final Configuration Success page, select the Start Admin Server check box and click Done. The Configuration Wizard automatically starts the Administration Server.

  8. Start the WebLogic 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.

13.2.2 Create a Machine

To create a Machine on which to host WebLogic Server instances:

From the Domain Structure window, select Environment and then Machines. Click New. The Create a New Machine page displays. Enter a name for the Machine (in this case, Test) and click Next. Click Finish on the following page. Figure 13-1 illustrates the Create a New Machine page.

Figure 13-1 Creating a New Machine

Creating a New Machine
Description of "Figure 13-1 Creating a New Machine"

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

Figure 13-2 Summary of Machines

Summary of Machines
Description of "Figure 13-2 Summary of Machines"

13.2.3 Create the WebLogic Servers

Create two WebLogic server instances associated with the machine. One will be Coherence*Web storage-enabled, the other will be Coherence*Web storage-disabled. The application will be deployed to these servers in a later step.

To create server instances:

  1. Click the name of the Machine in the Summary of Machines page to open the Settings for machine page. Click the Servers tab and then click Add to create a server.

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

  3. Provide details about the server in the Create a New Server page.

    Enter ServerA as the Server Name and 8081 as the Server Listen Port. Ensure that No, this is a stand-alone server is selected, as illustrated in Figure 13-3. Click Finish.

    Figure 13-3 Adding a Server to a Machine

    Adding a Server to a Machine
    Description of "Figure 13-3 Adding a Server to a Machine"

  4. When you are returned to the Settings for machine page, click Add, and repeat the previous step to create a second server.

    Enter ServerB as the Server Name and 8082 as the Server Listen Port. Ensure that No, this is a stand-alone server is selected. Click Finish.

  5. Click Servers in the Domain Structure. The Summary of Servers page displays and is similar to Figure 13-4.

Figure 13-4 Summary of Servers Page

Summary of Servers Page
Description of "Figure 13-4 Summary of Servers Page"

13.2.4 Create a Coherence Cluster

A Coherence cluster is a group of Coherence members that share a group address which allows them to communicate. Coherence clusters consist of members formed by applications, modules, or application servers (WebLogic Server instances or cache servers). In this step you create a Coherence cluster and assign the two WebLogic Server instances to it.

To create a Coherence cluster:

  1. Click Environment in the domain Structure Window and then click Coherence Clusters. In the Summary of Coherence Clusters page, click New. In the Coherence Cluster Properties page of the Create Coherence Cluster Configuration wizard, enter CoherenceCluster in the Name field, then click Next.

    Figure 13-5 illustrates the Create Coherence Cluster Configuration page.

    Figure 13-5 Creating a Coherence Cluster

    Creating a Coherence Cluster
    Description of "Figure 13-5 Creating a Coherence Cluster"

  2. Enter a value such as 8085, in the Unicast Listen Port field. Do not change any of the other values and click Next.

    Figure 13-6 Specifying a Unicast Listen Port for a Coherence Cluster

    Specifying a Unicast Listen Port for a Coherence Cluster
    Description of "Figure 13-6 Specifying a Unicast Listen Port for a Coherence Cluster"

  3. In the Coherence Cluster Members page of the Create Coherence Cluster Configuration wizard, select ServerA and ServerB as the targets. Do not select AdminServer. Click Finish.

    Figure 13-7 Choosing Coherence Cluster Members

    Choosing Coherence Cluster Targets
    Description of "Figure 13-7 Choosing Coherence Cluster Members"

    The Summary of Coherence Clusters page looks similar to Figure 13-8.

    Figure 13-8 Summary of Coherence Clusters

    Summary of Coherence Clusters
    Description of "Figure 13-8 Summary of Coherence Clusters"

13.2.5 Enable a Server for Coherence*Web Local Storage

One of the WebLogic servers must be enabled for Coherence*Web local storage. In this step, Coherence*Web local storage will be enabled for ServerB.

  1. Click Servers in the Domain Structure tree to open the Summary of Servers.

  2. Click ServerB.

  3. In Settings for ServerB, click the Configuration tab, then the Coherence tab.

  4. Select the Coherence*Web Local Storage Enabled checkbox, as illustrated in Figure 13-9.

  5. Click Save.

    Figure 13-9 Enabling a Server for Coherence*Web Local Storage

    Enabling a Server for Coherence*Web Local Storge
    Description of "Figure 13-9 Enabling a Server for Coherence*Web Local Storage"

13.2.6 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 folder as follows:

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

    <?xml version = '1.0' encoding = 'windows-1252'?>
     
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5">
       <description>Empty web.xml file for Web Application</description>
    </web-app>
    
  3. Create a weblogic.xml file in the /WEB-INF folder.

    • Reference coherence-web as the persistent store type for the session descriptor.

    • Reference the Coherence cluster in a coherence-cluster-ref stanza.

    Example 13-1 illustrates a sample weblogic.xml file.

    Example 13-1 Sample weblogic.xml File

    <weblogic-web-app>
      <session-descriptor> 
          <persistent-store-type>coherence-web</persistent-store-type>
      </session-descriptor> 
         <coherence-cluster-ref>
            <coherence-cluster-name>CoherenceCluster</coherence-cluster-name>
         </coherence-cluster-ref>
    </weblogic-web-app>
    
  4. Create a lib subfolder in the WEB-INF folder. Leave it empty.

  5. 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 folder.

    <html>
        <body>
          <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>
        </body>
    </html>
    
  6. Create a MANIFEST.MF file in the META-INF folder. Example 13-2 illustrates a sample MANIFEST.MF file.

    Example 13-2 Sample MANIFEST.MF File

    Manifest-Version: 1.0
    Created-By: 1.7.0_03 (Oracle Corporation) 
    
  7. The structure of the web application folder should appears as follows:

    /
    /counter.jsp
    /META-INF/MANIFEST.MF
    /WEB-INF/lib/
    /WEB-INF/web.xml
    /WEB-INF/weblogic.xml
    
  8. ZIP or JAR the Web application folder and save the file as counter.war.

13.2.7 Start the WebLogic Servers

Start the managed WebLogic Servers ServerA and ServerB. Because of the tight integration between WebLogic and Coherence, the cache server will start with the managed servers. The managed servers can be started from the command line or from the WebLogic Server Administration Console.

13.2.7.1 To Start the WebLogic Servers from the Command Line

The following instructions are for starting the servers from the command line.

  1. Open a command prompt from the domain's bin folder. This example assumes that the bin folder is located in c:\Oracle\user_projects\domains\base_domain\bin.

  2. Enter the following command to start ServerA:

    startManagedWebLogic.cmd ServerA
    

    Enter the server's user name and password when prompted.

  3. Open a second command prompt from the domain's bin folder.

  4. Enter the following command to start ServerB:

    startManagedWebLogic.cmd ServerB
    

    Enter the server's user name and password when prompted.

When the servers are running, you should see the response similar to Figure 13-10 in the Summary of Servers page in the WebLogic Server Administration Console.

Figure 13-10 Starting the Managed WebLogic Servers

Starting the Managed WebLogic Servers
Description of "Figure 13-10 Starting the Managed WebLogic Servers"

13.2.7.2 To Start the WebLogic Servers from the WebLogic Server Administration Console

To start WebLogic Servers from WebLogic Server Administration Console, see "Start Managed Servers from the Administration Console" in the Oracle WebLogic Server Administration Console Online Help.

When the servers are running, you should see the response similar to Figure 13-10 in the Summary of Servers page in the WebLogic Server Administration Console.

13.2.8 Deploy the Application

Deploy the counter.war application to the running managed servers.

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 wizard opens.

  3. Navigate to the location of the counter.war file, as illustrated in Figure 13-11. Click Next.

    Figure 13-11 Locating the Deployable Application

    Locating the Deployable Application
    Description of "Figure 13-11 Locating the Deployable Application"

  4. Select Install this deployment as an application as illustrated in Figure 13-12. Click Next.

    Figure 13-12 Installing the Deployment as an Application

    Installing the Deployment as an Application
    Description of "Figure 13-12 Installing the Deployment as an Application"

  5. Target the deployment to ServerA and ServerB, as illustrated in Figure 13-13.

    Figure 13-13 Targeting the Deployment

    Targeting the Deployment
    Description of "Figure 13-13 Targeting the Deployment"

  6. In the Optional Settings page, accept the defaults, as illustrated in Figure 13-14. Click Finish.

    Figure 13-14 Optional Settings Page of the Installation Assistant

    Optional Settings Page of the Installation Assistant
    Description of "Figure 13-14 Optional Settings Page of the Installation Assistant "

The counter.war file appears in the table in the Summary of Deployments page. The table indicates that the application is active and running (since the servers are running) on ServerA and ServerB, as illustrated in Figure 13-15.

Figure 13-15 Deployments Window Showing the Deployed Application

Deployments Window with the Deployed Application
Description of "Figure 13-15 Deployments Window Showing the Deployed Application"

13.2.9 Verify the Example

To verify the example:

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

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

    The counter page displays and the counter is set to 1 as illustrated in Figure 13-16.

    Figure 13-16 Counter Page with Counter Set to 1

    Counter Page with Counter Set to 1
    Description of "Figure 13-16 Counter Page with Counter Set to 1"

  2. In a new browser (or new browser tab), access the ServerB counter instance using the following URL:

    http://host:8082/counter/counter.jsp

    The counter page displays and the counter increments to 2 based on the session data as illustrated in Figure 13-17.

    Figure 13-17 Counter Page with Counter Set to 2

    Counter Page with Counter Set to 4
    Description of "Figure 13-17 Counter Page with Counter Set to 2"

  3. If you refresh the page, the counter increments to 3. Return to the original browser (or browser tab), refresh the instance, and the counter displays 4.

13.3 Working with Custom Session Cache Configuration Files

If you want to use a custom session cache configuration file, you must package it with your application. It must also be stored in a Grid Archive (GAR) file and deployed to the WebLogic Server cluster that is to act as the storage-enabled Coherence cluster members. A detailed discussion of working with custom session cache configuration files is beyond the scope of this document. For more information on creating and deploying a custom session cache configuration file, see "Using a Custom Session Cache Configuration File" in Oracle Fusion Middleware Administering HTTP Session Management with Oracle Coherence*Web.