J2EE - Creating Custom Cache Tool To Store User Sessions OutSide the Application Server.

Caching is useful for improving application performance and to prevent from OutOfMemory exceptions in Application Servers when huge data has to be stored in session. Some times you may have to store huge data in session. If there are many such users storing data into session you may lead to outofmemory and ApplicationServer goes down. Instead if you store session data in a separate JVM than the Application Server, you won't stuck into memory issues.

There are many commercial and open-source caching tools available for Java.One such tool is Coherence.

Here I am going to discuss how the caching tool internally works by creating custom cash which runs separate from the application server and you can store and retrieve session data from the caching tool but not from Application Server cache.

I created a sample cache tool with the help of Java RMI.

The CacheEngine.java is main java class which does the Registy Binding and the below Code does that part

 LocateRegistry.createRegistry(port);  
 ISession engine = new CacheEngine();  
 ISession stub = (ISession) UnicastRemoteObject.exportObject(engine, 0);  
 Registry registry = LocateRegistry.getRegistry();  
 registry.bind(name, stub);  

There is an another class Session.java which does the part of storing and retrieving. The methods
setAttribute() and getAttibute() from Session.java does the part.

 public String getAttribute(String key) {  
     try {  
       Registry registry = LocateRegistry.getRegistry(hostName,port);  
       ISession comp = (ISession) registry.lookup(cacheName);  
       String cacheValues = comp.getValue(key);  
       return cacheValues;  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
     return null;  
   }  
   public String setAttribute(String key, Object value) {  
     try {  
       Registry registry = LocateRegistry.getRegistry(hostName,port);  
       ISession comp = (ISession) registry.lookup(cacheName);  
       String cacheValues = comp.setValue(key, value);  
       return cacheValues;  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
     return null;  
   }    


Now in our Servlet/JSP we just have to call as shown below:

com.custom.Session.setAttribute("key","value"));

Rest all part will be taken care by caching tool.

Please find the steps below which helps in installing the caching tool.

 Installing the Cache Tool:  
 ==========================  
 1) Please download cache_1.0.zip from this blog.  
 2) Unzip you will find lib and bin directories.  
 3) In bin you will find cache-server.cmd  
 Provide the details  
 set cache_home=<Directory>\cache_1.0  
 set java_home=<Directory>\jdk160_29  
 Cache_home is where cache_1.0 exists. You require JDK 1.6 for this.  
 4) Also there are 3 system properties  
 -Dcustom.port  
 -Dcustom.cacheName  
 -Dcustom.hostName  
 custom.port - On which the cache tool runs  
 custom.cacheName - cache name  
 custom.hostName - On which host the cache tool is running.  
 5) Here in cache-server.cmd you should mention custom.port and custom.cacheName. By default the values are  
 custom.port = 1099  
 custom.cacheName = customcache.  
 6) Now run cache-server.cmd  
 You should see "Cache Engine bounded..." message. Now cache engine is started.  
The below steps helps us how to set the session in the cache and use the cache tool API in our WebApplication.
 Using in JSP:  
 =============  
 1) In cache_1.0/lib you will find customcacheclient.jar.  
 2) In your webapplication put customcacheclient.jar in classpath.  
 3) Now you need to set the 3 system properties  
 -Dcustom.port - This value should be same as you mentioned earlier.  
 -Dcustom.cacheName - This value should be same as you mentioned earlier.  
 -Dcustom.hostName - Host Name where the caching tool is running.  
 In weblogicserver these three system properties is configured in setDomainEnv.cmd.  
 In setDomainEnv.cmd search for "set JAVA_OPTIONS=%JAVA_OPTIONS% %JAVA_PROPERTIES% -Dwlw.iterativeDev=%iterativeDevFlag% -Dwlw.testConsole=%testConsoleFlag% -Dwlw.logErrorsToConsole=%logErrorsToConsoleFlag%".  
 Replace with  
 set JAVA_OPTIONS=%JAVA_OPTIONS% %JAVA_PROPERTIES% -Dcustom.port=1099 -Dcustom.cacheName=customcache -Dcustom.hostName=localhost -Dwlw.iterativeDev=%iterativeDevFlag% -Dwlw.testConsole=%testConsoleFlag% -Dwlw.logErrorsToConsole=%logErrorsToConsoleFlag%  
 Also you can set the system properties in JSP also as shown below  
 <%  
 System.setProperty("custom.port", "1099");  
 System.setProperty("custom.cacheName", "test");  
 System.setProperty("custom.hostName", "localhost");  

 com.custom.Session.setAttribute("1","customcache");  
 %>  
 4) In JSP/Servlets, for setting into cache you need to call  
 com.custom.Session.setAttribute("key","value");  
 Example: com.custom.Session.setAttribute("1","customcache");  
 5) In JSP/Servlets, for retrieving from cache you need to call  
 com.custom.Session.getAttribute("key");  
 Example: com.custom.Session.getAttribute("1");  

Download cache_1.0.zip

Download cache1.0_samedomain.zip for sample setDomainEnv.cmd script for this cache tool.

Download Source Code.


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today