📄 listaction.java
字号:
/*
* Created on 2005-1-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.struts.action;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
/**
* @author jmwang
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ListAction extends DispatchAction {
// 实例话一个log4j实例
static Logger log = Logger.getLogger(ListAction.class);
ServletCacheAdministrator admin = null;
Cache oscache = null;
/**
*
* @param mapping
* @param form
* @param req
* @param res
* @return
*/
public ActionForward test(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) {
String testOsEntryKey=null;
String oscachekey="testkey";
String oscachegroup="testgroup";
String cachecontent="";
try {
if (admin == null) {
admin = ServletCacheAdministrator.getInstance(this.getServlet()
.getServletContext());
}
if (oscache == null) {
oscache = admin.getCache(req, PageContext.APPLICATION_SCOPE);
}
//得到对应得oscache得entrykey
testOsEntryKey = admin.generateEntryKey(oscachekey, req,PageContext.APPLICATION_SCOPE);
log.debug("testOsEntryKey:"+testOsEntryKey);
//定义刷新周期为10秒
int RefreshPeriod=10;
//如果oscache过期,将抛出异常,那么再异常部分取得新得内容更新到oscache中
Object osobj = oscache.getFromCache(testOsEntryKey,RefreshPeriod);
} catch (NeedsRefreshException nrfe) {
log.debug("Cache已经过期!");
String oldCache = (String) nrfe.getCacheContent();
try {
//生成新数据
Date date=new Date();
cachecontent=date.toGMTString();
//避免死锁因为NeedsRefreshException产生时,如果不条用update或者put得相关操作,oscache将锁该cache
oscache.putInCache(testOsEntryKey, oldCache,
new String[] { oscachegroup });
//刷新group
oscache.flushEntry(testOsEntryKey);
} catch (Exception e1) {
log.error("刷新OsCache时异常,使用oldCache..." + e1.toString());
e1.printStackTrace();
oscache.putInCache(testOsEntryKey, nrfe
.getCacheContent(),
new String[] { oscachegroup });
}
}
req.setAttribute("cacheContent", cachecontent);
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -