




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
我作了一个测试,把Liferay中cms部分增加了生成html的功能,提供给用户访问,这样在一定程度上可以减少10%数据库的负载,因为在互联网中 匿名者访问占50%,其余的50%主要是做那些互动的操作,比如blog,bbs,电子商务.我们把一些page让用户在可选择的情况下进行 generating static HTML,当匿名者访问这个页面的时候,我们就把html的url response给browser.我的这个测试这样做的首先把工具类做成一个监听服务ServletContextListener,在perties设置一个values来确定how long again run the ServletContextListener.然后admin根据需要选择guest group中的页面在page setting的功能中选择 generating static HTML,如图.最后,就是看我们的生成的页面了. 这样,用户在使用liferay的情况下,当访问量很大的情况下,就能有效降低服务器的负载.首先增加一个自动生成代码的调度。12package com.blueauk.util;3import javax.servlet.*;45/*6 * 自动监听器7 * author stephen8 *9 */10public class GeneratingListener implements ServletContextListener 11 12 /*13 * 自动监听时钟14 */15 private GeneratingTimer rt = null;16 17 public void contextInitialized(ServletContextEvent event) 18 String status = Properties listener start .;19 event.getServletContext().log(status);20 System.out.println(status);21 22 / 激活自动监听时钟23 rt = new GeneratingTimer(15, event.getServletContext();24 rt.start();25 26 27 public void contextDestroyed(ServletContextEvent event) 28 String status = Properties listener stop .;29 event.getServletContext().log(status);30 System.out.println(status);31 32 / 停止自动监听时钟33 if (rt != null) 34 rt.stop();35 36 3712package com.blueauk.util;34import java.util.*;5import javax.servlet.*;67/*8 * 自动监听时钟9 * author stephen10 *11 */1213public class GeneratingTimer 14 private final Timer timer = new Timer();15 private final int sec;16 private ServletContext context = null;1718 public GeneratingTimer(int seconds, ServletContext context) 19 sec = seconds;20 this.context = context;21 2223 /*24 * 启动自动监听任务25 */26 public void start() 2728 / 取得当前日期时间29 Date date = new Date();3031 / 执行自动监听计划32 timer.schedule(new GeneratingTask(this.context), date, 60*60* 1000);33 3435 /*36 * 停止自动监听任务37 */38 public void stop() 39 timer.cancel();4041 42432package com.blueauk.util;3import java.io.IOException;4import java.io.*;5import java.util.*;67import javax.servlet.*;8import com.blueauk.util.Tools;9/*10 * 自动监听任务11 * 12 * author stephen13 * 14 */1516public class GeneratingTask extends TimerTask 17 18 private ServletContext context = null;19 /*20 * 配置文件的最后更新时间21 */22 private long lastModified = 0;23 24 private long count=0;25 /*26 * 构造一个自动更新任务27 * 28 * param context29 */30 public GeneratingTask(ServletContext context)31 this.context = context;32 System.out.println(A task instance is created now.); / 任务在整个33 / application34 / 周期内只创建一次。35 36 37 /*38 * 每次执行任务时显示一个随机数。39 */40 public void todoTestRandom()41 System.out.println(Task running);42 context.setAttribute(random, String.valueOf(Math.random();43 System.out.println(String)context.getAttribute(random);44 45 46 /*47 * 监听配置文件是否被更新。48 */49 public void todoTestFileStatus()50 System.out.println(Getting file status);51 System.out.println(this.isFileUpdated(WEB-INF/platforms/perties);52 53 54 /*55 * 监听配置文件是否被更新,自动更新文件中的配置项存储到 application 变量中。56 */57 public void todo()58 String filename = WEB-INF/platforms/perties;59 if(this.isFileUpdated(filename)60 61 System.out.println(Getting properties);62 try63 this.loadProperties(test, filename);64 catch(IOException ioe)65 System.err.println(ioe.getMessage();66 67 68 System.out.println(Test value is: + this.getTestProperty(name);69 70 71 public void run() 72 System.out.println(run-:+count);73 long newCount=count;74 if(newCount0)75 Tools tools=new Tools();76 tools.startGen();77 this.count=newCount+1;78 else79 this.count=newCount+1;80 81 82 83 84 /*85 * 判断物理文件是否已被更新86 * 87 * param filename88 * 物理文件名89 * return 是 true 否 false90 */91 private boolean isFileUpdated(String filename)92 File file = new File(context.getRealPath(filename);93 if(file.isFile()94 long lastUpdateTime = file.lastModified();95 if(lastUpdateTime this.lastModified)96 System.out.println(The properties file was modified.);97 this.lastModified = lastUpdateTime;98 return true;99 else100 System.out.println(The properties file was not modified.);101 return false;102 103 else104 System.out.println(The path does not point to a file.);105 return false;106 107 108 109 /*110 * 获取配置文件111 * 112 * param key113 * param filename114 * return115 */116 public void loadProperties(String key, String filename) throws IOException117 Properties prop = new Properties();118 InputStream stream = context.getResourceAsStream(filename);119 prop.load(stream);120 if(stream!=null)121 stream.close();122 123 context.setAttribute(key, prop);124 125 126 /*127 * 从 application 取配置项的值128 * 129 * param key130 * 配置项的键名131 * return 配置项的值132 */133 public String getTestProperty(String key)134 Properties prop = (Properties)context.getAttribute(test);135 if(prop=null)136 return null;137 else138 return (String)prop.get(key);139 140 1411422package com.blueauk.util;34import java.io.*;5import .*;6import java.util.*;7import javax.servlet.*;8import javax.servlet.http.HttpServletRequest;910import com.liferay.portal.kernel.util.StringMaker;11import com.liferay.portal.model.Layout;12import com.liferay.portal.service.LayoutLocalServiceUtil;13import com.liferay.portal.theme.ThemeDisplay;14import com.liferay.portal.util.PortalUtil;1516public class Tools 17 final static Object lock = new Object();1819 List urlall = new ArrayList();2021 public void startGen() 22 try 2324 String dir = (Tools.class.getClassLoader().getResource()25 .toString();26 String endDir = / + dir.substring(6, dir.length() - 16);27 File file = new File(endDir);28 if (!file.exists() 29 file.mkdir();30 31 htmlAll();32 List genHtmlURL = urlall;3334 for (int i = 0; i genHtmlURL.size(); i+) 35 String Url = (String) genHtmlURL.get(i);36 String filedir = htmlmkdir(endDir + Url);37 makeHtml( + Url, + filedir38 + Url.substring(Url.lastIndexOf(/) + 1) + .html);39 40 catch (Exception e) 4142 4344 4546 private void _buildSiteMapURL(List layouts, int displayDepth, int curDepth,47 ThemeDisplay themeDisplay, StringMaker sm) throws Exception 4849 for (int i = 0; i curDepth) 54 _buildSiteMapURL(layout.getChildren(), displayDepth,55 curDepth + 1, themeDisplay, sm);56 57 5859 6061 private void htmlAll() 62 try 63 List rootLayoutsURL = null;64 StringMaker sm = new StringMaker();65 rootLayoutsURL = LayoutLocalServiceUtil.getLayouts(14, false, 0);66 ThemeDisplay themeDisplayURL = new ThemeDisplay();67 _buildSiteMapURL(rootLayoutsURL, 0, 1, themeDisplayURL, sm);6869 catch (Exception e) 7071 7273 7475 private static void makeHtml(String page, String filePath) 76 makeHtml(page, filePath, UTF-8);77 7879 private static String htmlmkdir(String url) 8081 String bb = url.replace(/web/guest, htmls);8283 String aaa = bb.split(/);8485 String dir = ;86 for (int i = 0; i 0) 120 bw.write(line);121 bw.newLine();122 123 124 catch (Exception e) 125 e.printStackTrace();126 finally 127 try 128 br.close();129 bw.close();130 huc.disconnect();131 catch (Exception e) 132 e.printStackTrace();133 134 135 136 137在FriendlyU
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年赣县区有关部门下属事业单位第二批公开选调工作人员(10人)考试参考题库及答案解析
- 建筑施工劳务合同法律文本
- 2025嘉兴海宁市许村镇中心幼儿园公开招聘劳动合同制教职工28人备考考试题库附答案解析
- 2025河北唐山市人力资源和社会保障局高校毕业生临时公益性岗位招聘442人备考考试题库附答案解析
- 2025年8月广东广州市天河区车陂小学招聘语文、数学、心理健康教育教师4人考试参考题库及答案解析
- 物流运输合同范本与签订注意事项
- 农业领域技术研发合作合同
- 销售业务合同审核标准化工具
- 商务楼宇租赁合同及能源管理协议
- 工业制造设备维护与修理协议
- 拆除工程环境保护方案及措施
- 2025年度保密教育线上培训考试部分试题及参考答案
- 18项医疗核心制度题库(含答案)
- 科技美肤基础知识培训课件
- 《幼儿园开学安全第一课》课件
- 幼儿园红色小故事PPT:抗日小英雄王二小的故事
- 社会学导论(第五版)孙立平课件
- DL-T 5190.1-2022 电力建设施工技术规范 第1部分:土建结构工程(附条文说明)
- 真空断路器介绍ppt课件
- 车辆租赁合同下载_范本范文
- 高速公路收费员面试精选.
评论
0/150
提交评论