blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

Command caching in Websphere (cluster)


The command cache can cache the results of the invocation of server-side commands, which implement the WebSphere Command Pattern interfaces. This is useful for caching intensive operations, such as complex SQL joins or host requests. The results here are typically an object or container of objects, rather than the HTML results that are held in the servlet/JSP cache.

The command pattern is widely used and relatively simple to retrofit to an existing application, should you feel it could benefit from this type of caching. Retrofitting involves writing thin command wrappers around the existing code. Similar to how the caching engine filters on a servlet or JSP's service() method, the command cache filters on a command's execute() method to determine if the command is cacheable and if there are already results in the cache.
The following steps are same irrespective of whether it's a standalone server or cluster.

Enable Bean caching



Install cacheMonitor.ear.

Now write a Bean class,

package beans;

import com.ibm.websphere.command.CacheableCommandImpl;

public class MyBean extends CacheableCommandImpl{

 private static final long serialVersionUID = 1L;
 
 private String beanId;
 private String response;
 
 public MyBean(String beanId){
  this.beanId = beanId;
 }
 
 @Override
 public boolean isReadyToCallExecute() {
  System.out.println("isReadyToCallExecute");
  return true;
 }

 @Override
 public void performExecute() throws Exception {
  System.out.println("performExecute");
  //do business logic here
  if(Integer.parseInt(beanId)>100){
   setResponse("hello...");
  }else {
   setResponse("...bye");
  }
   
 }

 public void setBeanId(String beanId) {
  this.beanId = beanId;
 }

 public String getBeanId() {
  return beanId;
 }
 public void setResponse(String response) {
  this.response = response;
 }

 public String getResponse() {
  return response;
 }

 @Override
 public String toString() {
  return "MyBean [beanId=" + beanId + ", response=" + response + "]";
 }
}


Now write the servlet class,

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.MyBean;

import com.ibm.websphere.command.CommandException;

/**
 * Servlet implementation class CmdCacheServlet
 */
public class CmdCacheServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public CmdCacheServlet() {
  super();
 }

 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter writer = response.getWriter();
  String id = request.getParameter("id");
  MyBean bean = new MyBean(id);
  try {
   System.out.println("before execute");
   bean.execute();
   System.out.println("after execute");
   writer.println("MyBean says: " + bean.getResponse());
   writer.println("Local port:" + request.getLocalPort());
   String memberName = (String) getServletContext().getAttribute(
     "com.ibm.websphere.servlet.application.host");
   writer.println("Cluster member:" + memberName);

   writer.flush();
   writer.close();
  } catch (CommandException e) {
   e.printStackTrace();
  }
 }

 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}

WEB-INF/cachespec.xml


<?xml version="1.0" ?>
<!DOCTYPE cache SYSTEM "cachespec.dtd">
<cache>
 <cache-entry>
  <class> command</class>
  <sharing-policy>not-shared</sharing-policy>
  <name>beans.MyBean.class</name>
  <cache-id>
   <component type="method" id="getBeanId">
    <required>true</required>
   </component>
   <priority>1</priority>
   <timeout>60</timeout>
  </cache-id>
 </cache-entry>
</cache>

Check the URL,
http://localhost/TestSR/CmdCacheServlet?id=150



[4/2/14 15:44:02:851 IST] 0000003b SystemOut O before execute
[4/2/14 15:44:02:852 IST] 0000003b SystemOut O isReadyToCallExecute
[4/2/14 15:44:02:853 IST] 0000003b SystemOut O performExecute
[4/2/14 15:44:02:853 IST] 0000003b SystemOut O after execute
[4/2/14 15:44:30:276 IST] 0000003b SystemOut O before execute
[4/2/14 15:44:30:277 IST] 0000003b SystemOut O isReadyToCallExecute
[4/2/14 15:44:30:278 IST] 0000003b SystemOut O after execute



Restart cluster.


<?xml version="1.0" ?>
<!DOCTYPE cache SYSTEM "cachespec.dtd">
<cache>
 <cache-instance name="services/cache/sercache">
  <cache-entry>
   <class> command</class>
   <sharing-policy>not-shared</sharing-policy>
   <name>beans.MyBean.class</name>
   <cache-id>
    <component type="method" id="getBeanId">
     <required>true</required>
    </component>
    <priority>1</priority>
    <timeout>60</timeout>
   </cache-id>
  </cache-entry>
 </cache-instance>
</cache>


Check URL,
http://localhost/TestSR/CmdCacheServlet?id=150




No comments:

Post a Comment