Skip Headers
Oracle® Coherence Tutorial for Oracle Coherence
Release 3.7.1

E22622-03
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

11 Caching Sessions with Coherence and WebLogic Server

This chapter describes how to cache session information for Web application instances that are deployed across WebLogic Server instances.

This chapter has the following sections:

11.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 ActiveCache.

ActiveCache is 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 ActiveCache with WebLogic Server instances enables 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 ActiveCache enables you to provide Coherence-based HTTP session state persistence to applications running on WebLogic Server. 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 11g Release 1 (10.3.4). If you do not already have WebLogic Server 11g Release 1 (10.3.4) on your system, you can get it at the following URL:

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

For more information on the integration of Oracle WebLogic Server, Coherence, and Coherence*Web, see Using ActiveCache and User's Guide for Oracle Coherence*Web.

11.2 Caching Session Information for Web Application Instances

The following example demonstrates how to use ActiveCache to cache 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 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. A browser is used to access each application instance to demonstrate that the same session attribute is used among the instances.

  1. Ensure That You are Using Coherence 3.7 with WebLogic Server 10.3.4

  2. Start a Cache Server

  3. Configure and Start the WebLogic Server

  4. Create a Machine

  5. Create the WebLogic Servers

  6. Create a Coherence Cluster

  7. Deploy the Shared Library Files

  8. Create the Counter Web Application

  9. Deploy the Application

  10. Start the Node Manager and the WebLogic Servers

  11. Verify the Example

11.2.1 Ensure That You are Using Coherence 3.7 with WebLogic Server 10.3.4

By default, the installation of WebLogic Server 11g release 1 (10.3.4) installs Coherence 3.6 in the coherence_3.6 folder. To complete this example, ensure that your server start-up files and class paths continue to point to the Coherence 3.7 files you have already installed. Also, ensure that when you are deploying files (such as coherence.jar and coherence-web-spi.war) later in this example, that you are deploying release 3.7 versions.

11.2.2 Start a Cache Server

Start a Coherence cache server. Example 11-1 illustrates a sample script to start the cache server. In this example, tangosol.coherence.clusterport=7777 is the default multicast listen port of a Coherence cluster and tangosol.coherence.clusteraddress=231.1.1.1 is the default multicast listener address.

New for 3.7.1:

The session-cache-config.xml file has been moved from the coherence-web-spi.war file to the coherence-web.jar file. The coherence-web.jar file can be found in the coherence\lib directory.

Example 11-1 Script to Start the Cache Server

setlocal
 
set COHERENCE_HOME=c:\oracle\product\coherence
 
set COH_OPTS=%COH_OPTS% -server -cp %COHERENCE_HOME%\lib\coherence.jar;%COHERENCE_HOME%\lib\coherence-web.jar;
set COH_OPTS=%COH_OPTS% -Dtangosol.coherence.management.remote=true -Dtangosol.coherence.cacheconfig=session-cache-config.xml -Dtangosol.coherence.distributed.localstorage=true -Dtangosol.coherence.clusterport=7777 -Dtangosol.coherence.clusteraddress=231.1.1.1 -Dtangosol.coherence.session.localstorage=true 
 
java %COH_OPTS% -Xms512m -Xmx512m -XX:MaxPermSize=256m com.tangosol.net.DefaultCacheServer
 
:exit

Note:

If you define and start the Coherence cache server using the WebLogic Administration Console, you must indicate the location of the cache configuration file, and set distributed local storage and session local storage to true in the Arguments field of the Server Start tab. For example:

-Dtangosol.coherence.cacheconfig=session-cache-config.xml -Dtangosol.coherence.distributed.localstorage=true -Dtangosol.coherence.session.localstorage=true

11.2.3 Configure and Start the WebLogic Server

To configure and start a Coherence cluster:

  1. Run the Oracle WebLogic Configuration Wizard (Start then All Programs then Oracle WebLogic then WebLogic Server 11gR1 then Tools then Configuration Wizard) to create a new WebLogic Server 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 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.

11.2.4 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 11-1 illustrates the Create a New Machine page.

Figure 11-1 Creating a New Machine

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

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

Figure 11-2 Summary of Machines

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

11.2.5 Create the WebLogic Servers

Create two server instances associated with the Machine. 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. Enter the appropriate value for the Server Listen Address. This is illustrated in Figure 11-3. Click Finish.

    Figure 11-3 Adding a Server to a Machine

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

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

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

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

    The Summary of Servers page displays and is similar to Figure 11-4.

    Figure 11-4 Summary of Servers Page

    Summary of Servers Page

11.2.6 Create a Coherence Cluster

A Coherence cluster is a group of Coherence nodes that share a group address which allows them to communicate. Coherence clusters consist of nodes formed by applications, modules, or application servers (WebLogic Server instances or cache servers).

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 11-5 illustrates the Create Coherence Cluster Configuration page.

    Figure 11-5 Creating a Coherence Cluster

    Creating a Coherence Cluster
    Description of "Figure 11-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 11-6 Specifying a Unicast Listen Port for a Coherence Cluster

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

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

    Figure 11-7 Choosing Coherence Cluster Targets

    Choosing Coherence Cluster Targets
    Description of "Figure 11-7 Choosing Coherence Cluster Targets"

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

    Figure 11-8 Summary of Coherence Clusters

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

11.2.7 Deploy the Shared Library Files

In addition to the coherence.jar file, Coherence provides a deployable shared library, coherence-web-spi.war, that contains a native plug-in to WebLogic Server's HTTP Session Management interface. Coherence also provides the active-cache-1.0.jar file that contains the classes that enable WebLogic Server to interact with Coherence.

You do not have to deploy coherence.jar for this example. It will be bundled with the application in a later step.

To deploy the coherence-web-spi.war and active-cache-1.0.jar files:

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

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

  3. Use the Install Application Assistant to deploy coherence-web-spi.war as a library to ServerA and ServerB.

    1. Locate and select the coherence-web-spi.war file as illustrated in Figure 11-9. The WAR file can be found in the coherence\lib folder of the Coherence installation. Click Next.

      Figure 11-9 Selecting the coherence-web-spi.war File for Deployment

      Selecting the coherence-web-spi.jar File for Deployment
      Description of "Figure 11-9 Selecting the coherence-web-spi.war File for Deployment"

    2. In the Choose targeting style page, ensure that Install this deployment as a library is selected, as illustrated in Figure 11-10. Click Next.

      Figure 11-10 Installing the Deployment as a Library

      Installing the Deployment as a Library
      Description of "Figure 11-10 Installing the Deployment as a Library"

    3. Select ServerA and ServerB as the deployment targets (do not deploy coherence-web-spi.war to AdminServer) as illustrated in Figure 11-11. Click Next.

      Figure 11-11 Selecting Deployment Targets

      Selecting Deployment Targets
      Description of "Figure 11-11 Selecting Deployment Targets"

    4. In the Optional Settings page, select the Copy this application onto every target for me option in the Source accessibility section.

      Figure 11-12 Copying Files to Targets

      Copying files to Targets
      Description of "Figure 11-12 Copying Files to Targets"

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

  4. Repeat Steps 1 through 3 to deploy active-cache-1.0.jar to ServerA and ServerB (do not deploy active-cache-1.0.jar to the AdminServer).

    When you reach the Optional Settings page, select the I will make the deployment accessible from the following location option. This will mimic the nostage WLST option.

    Enter the path to the active-cache-1.0.jar file. The file is included in the WebLogic Server installation. Assuming that you installed the WebLogic Server at C:\oracle\product, you will find the file in the C:\oracle\product\wls1033\wlserver_10.3\common\deployable-libraries folder.

    Figure 11-13 Summary of Deployments Page

    The summary of deployments
    Description of "Figure 11-13 Summary of Deployments Page"

11.2.8 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: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. Create a weblogic.xml file in the /WEB-INF folder.

    • Add a library reference for the coherence-web-spi.war file.

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

    Example 11-2 illustrates a sample weblogic.xml file.

    Example 11-2 Sample weblogic.xml File

    <weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://www.oracle.com/technology/weblogic/weblogic-web-app/1.1/weblogic-web-app.xsd">
          <library-ref>
               <library-name>coherence-web-spi</library-name>
          </library-ref>
         <coherence-cluster-ref>
            <coherence-cluster-name>CoherenceCluster</coherence-cluster-name>
         </coherence-cluster-ref> 
    </weblogic-web-app>
    
  4. Bundle the coherence.jar file with the application: copy coherence.jar from the coherence\lib folder of the Coherence 3.7 installation to the WEB-INF/lib folder.

  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. Add references to the active-cache JAR file. Example 11-3 illustrates a sample manifest.mf file.

    Example 11-3 Sample manifest.mf File

    Extension-List: active-cache
    active-cache-Extension-Name: active-cache
    active-cache-Specification-Version: 1.0
    active-cache-Implementation-Version: 1.0
    
  7. The structure of the Web application folder should appears as follows:

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

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

  3. Use the Install Application Assistant to deploy the counter.war file to ServerA and ServerB. In the Optional Settings page, select the Copy this application onto every target for me option in the Source accessibility section.

    The Summary of Deployments page displays after the application is deployed. Figure 11-14 illustrates the page with the deployed active-cache.jar, coherence-web-spi.war, and counter.war files.

    Figure 11-14 Summary of Deployments Page with Deployed Files

    Summary of Deployments Page with Deployed Files
    Description of "Figure 11-14 Summary of Deployments Page with Deployed Files"

11.2.10 Start the Node Manager and the WebLogic Servers

Start the Node Manager then start the WebLogic Server instances from the WebLogic Server Administration Console. The Node Manager is a Java utility that runs as a separate process from Oracle WebLogic Server, and enables you to perform common operations for a Managed Server, regardless of its location with respect to its Administration Server.

  1. To start the Node Manager, go to Start, then All Programs, then Oracle WebLogic, then WebLogic Server 11gR1, then Tools, and then Node Manager.

  2. Click Environment then Servers in the domain Structure Window. From the Summary of Servers page in the WebLogic Server Administration Console, click the Control tab and start both server instances.

    Figure 11-15 illustrates the deployments table after the servers have been started.

    Figure 11-15 Deployments Window Showing the Deployed Application and Libraries

    Deployments Window with the Deployed Application
    Description of "Figure 11-15 Deployments Window Showing the Deployed Application and Libraries"

11.2.11 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 11-16.

    Figure 11-16 Counter Page with Counter Set to 1

    Counter Page with Counter Set to 1
    Description of "Figure 11-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 11-17.

    Figure 11-17 Counter Page with Counter Set to 2

    Counter Page with Counter Set to 4
    Description of "Figure 11-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.