我写了一个包含邮件构建部分的库。该邮件构建部分使用了 Velocity。 mailbuilder 类如下——
public class mailBuilder {
public void initialize() throws Exception
{
Properties props = new Properties();
log.info("About to set the ClassPath for Velocity specific tasks");
props.setProperty(VelocityEngine.RESOURCE_LOADER, "classpath");
props.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER + ".class", ClasspathResourceLoader.class.getName());
try
{
log.info("Just before");
Velocity.init(props);
log.info("Just after");
}
catch ( Exception e )
{
log.error( "Caught Execption on velocityEngine init", e );
throw new Exception( "Caught Execption on velocityEngine init", e );
}
log.info("Completed initializing Velocity Engine");
}
public String returnMailstring() throws Exception {
initialize();
....
....
}
}
现在,当我从 Eclipse 中运行并测试这个库时,结果符合预期,而且看起来很好。
我有一个 Web 应用程序,它接收来自 UI 的请求,并使用 ExecutorService (newSingleThreadExecutor) 在后台默默地一一服务这些用户请求。
我注意到我对上述库的调用在邮件构建部分特别是在Velocity.init(props)
没有抛出异常,但线程似乎在 VelocityEngine 初始化时挂起。
我在网上查了一下,但没有找到问题所在。
关于这个问题的任何帮助都将是巨大的。
谢谢
p1ng
速度使用有两种模型:
- 单例模型即
Velocity.init(..)
,这里您的应用程序中只有一个速度配置。在这种情况下,您应该在应用程序启动时通过侦听器或任何类型的初始化 bean 仅调用 init 一次。
- 从 1.2 版开始,您可以使用多种配置拥有多个速度引擎,模型的使用方式如下:
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
...
// create a new instance of the engine
VelocityEngine ve = new VelocityEngine();
// configure the engine. In this case, we are using
// ourselves as a logger (see logging examples..)
ve.setProperty(
VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
// initialize the engine
ve.init();
...
Template t = ve.getTemplate("foo.vm");
因此,只需选择您想要使用的模型并遵循它即可。但以线程方式调用 Velocity.init() 肯定会产生不良行为。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)