Steps to implement dynacache :-
Dynacache is a place to store data . We can store objects and use them when ever required for future use. WebSphere will be responsible for maintaining and managing these objects. We can change the configurations from was admin console.
Step 1 :- Create or check existing cache instance in websphere portal . If it doesn't exist create one. Navigate to below location in was console
Step 3 :- Click save and then check for newly created cache instance then restart the portal server
Step 4 :- Now create a portlet and write code to lookup for service object to access dynacache object
package com.ibm.dynacacheportlet;
import java.io.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.*;
import com.ibm.websphere.cache.DistributedMap;
/**
* A sample portlet
*/
public class DynacachePortlet extends javax.portlet.GenericPortlet {
/**
* @see javax.portlet.Portlet#init()
*/
//Distributed map which consists of all stored objects
private DistributedMap appCache = null;
public void init() throws PortletException{
super.init();
InitialContext ctx;
try {
ctx = new InitialContext();
appCache = (DistributedMap) ctx.lookup("tutorialCache");
} catch (NamingException e) {
// TODO need to change the exception handling...
e.printStackTrace();
}
}
/**
* 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
System.out.println("enterring");
response.setContentType(request.getResponseContentType());
appCache.put("Name", "renault");
String mapValue = (String) appCache.get("Name");
System.out.println("String value :- "+ mapValue);
response.getWriter().println("DynacachePortlet#doView()"+ mapValue);
}
/**
* 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
}
}
No comments:
Post a Comment