6 An ActiveCache Example

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.

The Coherence*Web module is included in the default installation of WebLogic Server 11gR1 (10.3.3). For more information, see the User's Guide for Oracle Coherence*Web.

Start a Cache Server

Start a Coherence cache server. Example 6-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 listen address.

Example 6-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-spi.war;
set COH_OPTS=%COH_OPTS% -Dtangosol.coherence.management.remote=true -Dtangosol.coherence.cacheconfig=/WEB-INF/classes/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 com.tangosol.net.DefaultCacheServer
 
:exit

Configure and Start the WebLogic Server

This example requires a Coherence Cluster.

  1. Run the Oracle WebLogic Configuration Wizard (Start > All Programs > Oracle WebLogic > WebLogic Server 11gR1 > 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 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.

Create a Machine

Create a Machine on which to host WebLogic Server instances.

From the Domain Structure window, select 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 6-1 Creating a New Machine

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

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

Figure 6-2 Summary of Machines

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

Create the WebLogic Servers

Create two server instances associated with the Machine. The application will be deployed to these servers in a later step.

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

  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 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 6-3 Adding a Server to a Machine

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

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

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

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

    Figure 6-4 Summary of Servers Page

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

Create a Coherence Cluster

Create a Coherence Cluster.

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

    Figure 6-5 Creating a Coherence Cluster

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

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

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

    Figure 6-7 Choosing Coherence Cluster Targets

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

    The Summary of Coherence Clusters page should look similar to Figure 6-8.

    Figure 6-8 Summary of Coherence Clusters

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

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 which contains the classes that allow 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 screen displays.

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

    1. Locate and select the coherence-web-spi.war file. Click Next.

      Figure 6-9 Selecting the coherence-web-spi.jar File for Deployment

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

    2. Select ServerA and ServerB as the deployment targets (do not deploy coherence-web-spi.war to the AdminServer). Click Next.

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

      Figure 6-10 Copying Files to Targets

      Copying files to Targets
      Description of "Figure 6-10 Copying Files to Targets"

    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.

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

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

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

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

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

    Example 6-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://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
          <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>
          </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 to the WEB-INF/lib directory.

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

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

    Example 6-3 Sample manifest.mf File

    Extension-List: active-cache
    active-cache-Extension-Name: active-cache
    active-cache-Specification-Version: 1.0
    active-cache-Implmenentation-Version: 1.0
    
  7. The Web application directory should appear 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 directory and save the file as counter.war.

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 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 6-11 illustrates the deployments table with the counter Web application.

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

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

Start the Node Manager and the WebLogic Servers

Start the Node Manager then start the WebLogic Servers 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. Start the Node Manager from Start > All Programs > Oracle Fusion Middleware > WebLogic Server10.3 > Tools > Node Manager.

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

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 6-12 Counter Page with Counter Set to 1

    Counter Page with Counter Set to 1
    Description of "Figure 6-12 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:8081/counter/counter.jsp

    The counter page displays and the counter increments to 2 based on the session data.

    Figure 6-13 Counter Page with Counter Set to 2

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