Oracle® Coherence Tutorial for Oracle Coherence Release 3.5 Part Number E14527-01 |
|
|
View PDF |
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:
Oracle Coherence 3.5
WebLogic 10.X (This example uses 10.3. WebLogic 8.X and 9.X are also supported.)
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.
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
This example requires two application server instances:
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.
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.
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.
The Summary of Machines page should look similar to Figure 8-2.
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.
Select Create a new server and associate it with this machine in the Add a Server to Machine page, and click Next.
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.
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.
Expand Environment in the Domain Structure menu and click Servers.
The Summary of Servers page displays and should be similar to Figure 8-4:
Start the Node Manager from Start>All Programs>Oracle Fusion Middleware>WebLogic Server10.3>Tools>Node Manager.
From the Summary of Servers screen, click the Control tab and start both server instances.
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:
From the Domain Structure menu, click Deployments. The Summary of Deployments page displays.
Click Install. The Install Application Assistant screen displays.
Use the Install Application Assistant to deploy coherence-web-spi.jar
to both ServerA
and ServerB
.
Locate the coherence-web-spi.jar
file.
Select ServerA
and ServerB
as the deployment targets.
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.
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:
Create a standard Web application directory as follows:
/ /WEB-INF
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>
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>
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>
The Web application directory should appear as follows:
/ /counter.jsp /weblogic.xml /WEB-INF/web.xml
ZIP or JAR the Web application directory and save the file as counter.war
.
To deploy the counter.war
application:
Open the Summary of Deployments page by clicking Deployments in the Domain Structure menu in the Oracle WebLogic Server Administration Console.
Click Install. The Install Application Assistant screen displays.
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.
To verify the example:
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:
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.