Java Velocity引擎初始化问题

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

Mah*_*eTo 9

速度使用有两种模型:

  1. 单身模型即Velocity.init(..),您的应用程序中只有一个速度配置.在应用程序启动时,您应该通过侦听器或任何类型的初始化bean仅调用一次init.
  2. 多模型启动版本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");
Run Code Online (Sandbox Code Playgroud)

所以只需选择您想要使用的型号并遵循它.但是以线程方式调用Velocity.init()肯定会产生不良行为.