Command Cache :- This is used as a place holder for data and replace it at a particular interval as desired .
Step 1:- First you need to check whether your web-sphere portal is capable of caching the data
Step 2 :- Now click on Dynamic cache service under container services and check whether below check box is selected
Step 3 :-
Step 1:- First you need to check whether your web-sphere portal is capable of caching the data
Step 2 :- Now click on Dynamic cache service under container services and check whether below check box is selected
Step 3 :-
- Create a Java class that extends
CacheableCommandImpl
class, this class provides logic to cache data by providing life cycle for your class. TheCacheableCommandImpl
is abstract class so in order to make a concrete class you will have to override itsisReadyToCallExecute()
andperformExecute()
method - The
CacheableCommandImpl
class provides life cycle for the cache-able object, it will call theisReadyToCallExecute()
method of your class to check if your class is ready to execute. You can use this method to check if your data access part is ready. If it is not ready you can write custom code to return false - The
performExecute()
method of your class is the place were you will be having your custom code to get data . In my example class name is CachableContent
//Class code
package com.ibm.cachableClass;
import java.util.Random;
import com.ibm.websphere.command.CacheableCommandImpl;
public class CachableContent extends CacheableCommandImpl {
private static final long serialVersionUID = 6243454554964886940L;
private int randomValue = 1;
private int ComputedValue = 0;
public CachableContent(int value) {
this.randomValue = value;
// TODO Auto-generated constructor stub
}
@Override
public boolean isReadyToCallExecute() {
// TODO Auto-generated method stub
return true;
}
@Override
public void performExecute() throws Exception {
System.out.println("enterring into perform execute");
//We will get updated computed value for every 60 seconds
setComputedValue(randomValue+randomValue);
// TODO Auto-generated method stub
}
public int getRandomValue() {
return randomValue;
}
public void setRandomValue(int randomValue) {
this.randomValue = randomValue;
}
public int getComputedValue() {
return ComputedValue;
}
public void setComputedValue(int computedValue) {
ComputedValue = computedValue;
}
}
import java.util.Random;
import com.ibm.websphere.command.CacheableCommandImpl;
public class CachableContent extends CacheableCommandImpl {
private static final long serialVersionUID = 6243454554964886940L;
private int randomValue = 1;
private int ComputedValue = 0;
public CachableContent(int value) {
this.randomValue = value;
// TODO Auto-generated constructor stub
}
@Override
public boolean isReadyToCallExecute() {
// TODO Auto-generated method stub
return true;
}
@Override
public void performExecute() throws Exception {
System.out.println("enterring into perform execute");
//We will get updated computed value for every 60 seconds
setComputedValue(randomValue+randomValue);
// TODO Auto-generated method stub
}
public int getRandomValue() {
return randomValue;
}
public void setRandomValue(int randomValue) {
this.randomValue = randomValue;
}
public int getComputedValue() {
return ComputedValue;
}
public void setComputedValue(int computedValue) {
ComputedValue = computedValue;
}
}
Step 4 :- Now create a portlet to access the CachableContent
package com.ibm.cachableportlet;
import java.io.*;
import javax.portlet.*;
import com.ibm.cachableClass.CachableContent;
/**
* A sample portlet
*/
public class CachablePortlet extends javax.portlet.GenericPortlet {
/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
}
/**
* Serve up the <code>view</code> mode.
*
* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
CachableContent cc = new CachableContent();
//will actually call the perform execute method
cc.execute();
int randValue = cc.getRandomValue();
//Depending on the time you set this will be refreshed
response.getWriter().println("CachablePortlet#doView()"+randValue);
}
/**
* Serve up the <code>edit</code> mode.
*
* @see javax.portlet.GenericPortlet#doEdit(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// TODO: auto-generated method stub
}
/**
* Serve up the <code>help</code> mode.
*
* @see javax.portlet.GenericPortlet#doHelp(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// TODO: auto-generated method stub
}
/**
* Process an action request.
*
* @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
*/
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
// TODO: auto-generated method stub
}
}
Step 5 :- Now create the cachespec.xml to specify the time and place it under web-inf like below
<?xml version="1.0" ?>
<!DOCTYPE cache SYSTEM "cachespec.dtd">
<cache>
<cache-entry>
<class> command</class>
<sharing-policy>not-shared</sharing-policy>
<name> com.ibm.cachableClass.CachableContent.class</name>
<cache-id>
<component type="method" id="getRandomValue">
<required>true</required >
</component>
<priority>1</priority>
<timeout>60</timeout >
</cache-id>
</cache-entry>
</cache>
Mention the method you are calling and the time interval in which it should refresh. Now the random number will be regenerated for every 60 seconds.
Step 6 :- Deploy this in the portal server and observe the change of number for every 60 second. If you do a refresh of the page
No comments:
Post a Comment