使用velocity来配置内容,用观察者模式自动load模板文件
业务中要用notify发短信。应用端向notify发,有一个短信订阅该消息,并组织内容发出去。
?
我们决定采用velocity来组织内容,并且当修改vm模板内容时能自动识别并加载,不用重新启动程序。
?
?
package com.test.velocity;import java.io.File;import java.io.StringWriter;import java.util.Map;import java.util.Observable;import java.util.Observer;import java.util.Properties;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.apache.velocity.app.VelocityEngine;/** * * 描述 * * @author 锅巴 * @version 1.0 2010-7-12 */public class ParseNotify implements Observer { private VelocityEngine ve; private Template template; private final String velocityFileName = "hellovelocity.vm"; private final String velocityFilePath = "D:\\workspace_mvn\\my-nom\"; public ParseNotify() throws Exception { init(); Object velocityFileChangeObservable = new VelocityFileChangeObservable( velocityFilePath + velocityFileName); ((Observable) velocityFileChangeObservable).addObserver(this); new Thread((Runnable) velocityFileChangeObservable).start(); } private void init() throws Exception { ve = new VelocityEngine(); Properties prop = new Properties(); prop.setProperty(Velocity.ENCODING_DEFAULT, "GBK"); prop.setProperty(Velocity.INPUT_ENCODING, "GBK"); prop.setProperty(Velocity.OUTPUT_ENCODING, "GBK"); ve.init(prop); template = ve.getTemplate(velocityFileName); } private Template getVelocityTemplate() throws Exception { return template; } public SmsBean parseNotifyMsg(Map<String, String> notifyMsg) throws Exception { VelocityContext context = new VelocityContext(); for (Map.Entry<String, String> entry : notifyMsg.entrySet()) { context.put(entry.getKey(), entry.getValue()); } StringWriter writer = new StringWriter(); getVelocityTemplate().merge(context, writer); return new SmsBean(writer.toString(), notifyMsg.get("mobile")); } public void update(Observable o, Object arg) { // TODO Auto-generated method stub try { init(); System.out.println("ParseNotify Observable update "); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } static class VelocityFileChangeObservable extends Observable implements Runnable { private long lastModifiedTime; private File file; public VelocityFileChangeObservable(String filePath) { file = new File(filePath); if (!file.exists()) { throw new IllegalArgumentException("file not exists"); } lastModifiedTime = file.lastModified(); } public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(5000); if (file.lastModified() > lastModifiedTime) { this.setChanged(); this.notifyObservers(); this.lastModifiedTime = file.lastModified(); System.out.println("hellovelocity.vm is changed"); } System.out.println("VelocityFileChangeObservable run"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
?ParseNotify 类负责解晰notify信息(以Map传入),有一个内部类VelocityFileChangeObservable 观察vm文件是否被更改,如果更改则通知ParseNotify 重新load vm
?
? 阿里巴巴招聘:java/C/C++/PHP ,Hadoop,搜索工程师, ????????????????????????? 前端,视觉,交互工程师,系统/数据库工程师, ????????????????????????? 产品经理,运营,市场等 ?? ? 有意请将简历发邮件:iloveditan@sina.cn