pin*_*ing 4 java velocity initialization
我有一个写有一个邮件构建部分的库.该邮件构建部分使用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();
....
....
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我按照eclipse运行和测试这个库时,结果是预期的,事情似乎很好.我有一个Web应用程序从UI接收请求,并使用ExecutorService(newSingleThreadExecutor)在后台静默地为这些用户请求提供服务.
我注意到我对前面提到的库的调用在特定的邮件构建部分被挂起了Velocity.init(props)没有异常抛出但是线程似乎在VelocityEngine初始化时挂起.我在网上查了一下,但对于这个问题可能没有运气.任何有关问题如何巨大的帮助.
谢谢p1ng
速度使用有两种模型:
Velocity.init(..),您的应用程序中只有一个速度配置.在应用程序启动时,您应该通过侦听器或任何类型的初始化bean仅调用一次init.Run Code Online (Sandbox Code Playgroud)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()肯定会产生不良行为.