使用velocity来配置内容,用观察者模式自动load模板文件

本文介绍了一个使用Apache Velocity实现的短信内容动态生成系统。系统能够自动检测模板文件的变化,并在文件更新后重新加载模板,确保短信内容的准确性和时效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

业务中要用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值