使用velocity作为模板
设置velocity模板
public class VelocityTemplateFactory {
public static void init() {
Velocity.setProperty("resource.loader", "file");
Velocity.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
//Velocity.setProperty("file.resource.loader.path", Task.JOB_PATH + File.separator + "conf");
Velocity.setProperty("file.resource.loader.path", "conf");
Velocity.setProperty("file.resource.loader.cache", "false");
Velocity.setProperty("file.resource.loader.modificationCheckInterval", "2");
Velocity.setProperty("input.encoding", "UTF-8");
Velocity.setProperty("output.encoding", "UTF-8");
Velocity.setProperty("default.contentType", "text/html; charset=UTF-8");
Velocity.setProperty("velocimarco.library.autoreload", "true");
Velocity.setProperty("runtime.log.error.stacktrace", "false");
Velocity.setProperty("runtime.log.warn.stacktrace", "false");
Velocity.setProperty("runtime.log.info.stacktrace", "false");
Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
Velocity.setProperty("runtime.log.logsystem.log4j.category", "velocity_log");
try {
Velocity.init();
} catch (Throwable e) {
e.printStackTrace();
System.err.println("init velocity faild to exit");
// System.exit(0);
}
}
}
public class Test {
private static final Logger logger = Logger.getLogger(Test.class);
// 页面模板
private static final String TEMPLATE = "/template/businessdailyreport.html";
private static ExecutorService threadPool = Executors.newFixedThreadPool(2);
public static CountDownLatch countDownLatch = new CountDownLatch(2);
static {
// 初始化模板引擎
VelocityTemplateFactory.init();
}
public static void main(String[] args) throws Exception {
Map<String, Object> ultimateDateMap = new HashMap<String, Object>();
// 使用线程获得html模板使用的数据放到Map中
WeekAccumulativeThread weekAccumulativeThread = new WeekAccumulativeThread(ultimateDateMap);
TodayAccumulativeThread todayAccumulativeThread = new TodayAccumulativeThread(ultimateDateMap);
threadPool.submit(weekAccumulativeThread);
threadPool.submit(todayAccumulativeThread);
BusinessIncomeDailyTask.countDownLatch.await();
logger.info("ultimateDateMap:" + ultimateDateMap);
// 得到html模板
Template template = Velocity.getTemplate(TEMPLATE);
// 得到的数据填充的html模板中
Context context = new VelocityContext(ultimateDateMap);
StringWriter stringWriter = new StringWriter();
template.merge(context, stringWriter);
stringWriter.flush();
logger.info(stringWriter.toString());
FileInputStream fis = new FileInputStream("conf/businessDailyEmail.properties");
Properties pro = new Properties();
pro.load(fis);
String emails = pro.getProperty("emails");
String[] emailsSplit = emails.split(",");
List<String> mailList = new ArrayList<String>();
for (String email : emailsSplit) {
mailList.add(email);
}
// 发送邮件
EmailNoticeUtil.sendEmail(stringWriter.toString(), "招聘商业日报:" + DateUtil.getDashYesterdayFormat(), mailList);
threadPool.shutdown();
logger.info("========== execute success ==========");
}
}