Keeping Http-Sessions in Coherence cache

Overview:

This article talks about keeping your Http Sessions in Coherence cache , instead of keeping it as part of app server’s in memory or any other external device ..file/DB. We will integrate Weblogic to use Coherence cache and will play around the Http session a little bit.

Why we need:

1) Considering the Coherence cache storage running on a separate JVM even if the app server JVM was down for some reason, if the app server is back within the session timeout period, Customer can access the same session. In fact if the Coherence cache server is running in a separate  box, you even can replace a hardware which belongs to that App server JVM.

2) Having a separate HTTPSession layer, we can store huge data in the session without worrying about OutOfMemory from the application server layer.

3) Not quite sure if this is possible practically, but theoretically different app servers  can share the same Http Session if needed( at least in the future).

What we need:

1) Weblogic 10.3.3 server

2) Coherence 3.6.1

Setup: I am referring Oracle Coherence documentation for the same

http://download.oracle.com/docs/cd/E14526_01/coh.350/e14536/wslinstall.htm#BABDFAEF

i) Copy coherence.jar in the domain lib directory, in my machine I copied the same into the following directory

E:\oracle\bea1033\user_projects\domains\coherence_web_domain\lib

ii) Start the Weblogic Admin Server and create a Cluster of two servers

iii) Deploy coherence-web-spi.war as a shared library –> One of the reasons behind deploying this war is , I believe this carries the coherence cache config for the session.
iv) create a war, below are the file contents.

File Contents:

E:\coherence\CoherenceWeb\machanmama –>test.jsp
|
WEB-INF–
|
–weblogic.xml
–web.xml
|
lib –> empty folder
classes –> empty folder

File contents:

weblogic.xml:
————
Basically this file is telling

1) Replicate my session if the servers are Clustered
2) Reference to the coherence web library we deployed a little while ago.

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

<weblogic-web-app xmlns=”http://www.bea.com/ns/weblogic/90″&gt;
<session-descriptor>
<persistent-store-type>replicated_if_clustered</persistent-store-type>
</session-descriptor>
<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>

Web.xml:
——
since I am just playing with jsp alone, there is no content in there

<web-app xmlns=”http://java.sun.com/xml/ns/j2ee&#8221; version=”2.4″>
</web-app>

Test.jsp:
——–

<%

out.println(“– Start —“);

try {

if(session.getAttribute(“obj1″) != null){
out.println(” Inside the getAttribute “);
session.getAttribute(“obj1″);
}else{
out.println(” Inside the setAttribute “);
session.setAttribute(“obj1″,”obj1”);
}

}
catch(java.lang.Exception ex){
out.println(“– Exception —“);
ex.printStackTrace();
}
out.println(“– End —“);

%>

Coherence Cluster setup:

1) I do little customization from Coherence cluster perspective, I would like to specify an override file with some unique values to make sure my Coherence Servers are forming an unique Cluster, I am still trying to Multicast as the Cluster messaging mode. To do that, create a file name test-override. xml with the following content in it, the values which are more important are  Cluster name and multicast address.

<?xml version=’1.0′?>

<!DOCTYPE coherence SYSTEM “coherence.dtd”>

<coherence>
<logging-config>
<severity-level system-property=”tangosol.coherence.log.level”>9</severity-level>
</logging-config>
<cluster-config>
<member-identity>
<cluster-name>Machan</cluster-name>
</member-identity>
<multicast-listener>
<address>224.3.6.0</address>
<!– <time-to-live system-property=”tangosol.coherence.ttl”>0</time-to-live> –>
<port>8901</port>
</multicast-listener>
</cluster-config>

</coherence>

2) Add the below -D option as part of the server start up

-Dtangosol.coherence.override=E:\temp\eclipse-workspace\hello-grid\src\test-override.xml

I put the same in E:\oracle\bea1033\user_projects\domains\coherence_web_domain\bin\setDomainEnv.cmd

and added as part of the JAVA_OPTIONS

set JAVA_OPTIONS=%JAVA_OPTIONS% %JAVA_PROPERTIES% -Dwlw.iterativeDev=%iterativeDevFlag% -Dwlw.testConsole=%testConsoleFlag% -Dwlw.logErrorsToConsole=%logErrorsToConsoleFlag% -Dtangosol.coherence.override=E:\temp\eclipse-workspace\hello-grid\src\test-override.xml

Testing:

1) Start the Coherence Cache Server using the below command,  and you can see the output from my machine as well, keep it running.

E:\coherence\coherence-java-3.6.1.0b19636\coherence\lib>java -server -cp coherence.jar;coherence-web-spi.war  -Dtangosol.coherence.management.remote=true -Dtangosol.coherence.management=all -Dtangosol.coherence.management.remote=true -Dtangosol.coherence.cacheconfig=WEB-INF/classes/session-cache-config.xml -Dtangosol.coherence.session.localstorage=true -Dtangosol.coherence.override=E:\temp\eclipse-workspace\hello-grid\src\test-override.xml com.tangosol.net.DefaultCacheServer

2) Start the Weblogic Admin and the two ManagedServers: After this you pretty much see four command prompts running, i) Weblogic Admin ii) Weblogic Managed Server 1 iii) Weblogic Managed Server 2 iv) And the Coherence Cache Server

3) Now go to the Console and deploy the test.war and target to the Weblogic Cluster ( server1 and server2).

4) To verify the setup , please open a jConsole and see the details, you see a Coherence-> Cluster –> Attributes–> Cluster Size = 3 and you also see Coherence –> Cache –> DistributedSessions, like below and this tells you, the Cluster is formed correctly and the Session Cache got initialized.

5) Now open a browser and invoke the test.jsp

http://localhost:7004/machanmama/test.jsp

output: –> meaning first time , it is creating a new session keeping it in Coherence cache.

— Start — Inside the setAttribute — End —

6) Now restart both the Weblogic Servers and hit the same URL again–> This time , though the servers are rebooted, still the session is active

— Start — Inside the getAttribute — End —

7) Now change the URL to hit the second server, you will get the same output as

— Start — Inside the getAttribute — End —

Summary:

I believe this explains the basic  feature of using Conherence cache as Weblogic’s HttpSession holder. Please feel free to ping me about any questions with regard to this test case.

About sasikumarchellappan

Part of Sustaining Weblogic Development Team of Oracle Weblogic Server.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment