Java Velocity引擎初始化问题

发布于 2024-12-23 14:08:04 字数 1342 浏览 4 评论 0原文

我写了一个包含邮件构建部分的库。该邮件构建部分使用了 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

I have a written a library that has a mailbuilding part. This mailbuilding part employs the use of Velocity. The mailbuilder class is as follows --

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();
....
....
}

}

Now when i run and test this library as is from eclipse, it the results are as expected and things seems fine.
I have a web application that takes in a request from the UI, and uses ExecutorService (newSingleThreadExecutor) to service these user requests one by one silently in the background.

I am noticing that my calls to the aforementioned library are getting hung at the mailbuilding part specifically at Velocity.init(props) There is no exception thrown but the thread seems to hang at the VelocityEngine initialization.
I have looked up online and had no luck with what the issue may be.
Any help on how the issue would be immense.

Thanks
p1ng

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-12-30 14:08:04

速度使用有两种模型:

  1. 单例模型,即 Velocity.init(..),在这里,您的应用程序中只有一个速度配置。在这种情况下,您应该在应用程序启动时通过侦听器或任何类型的初始化 bean 仅调用 init 一次。
  2. 从 1.2 版开始,您可以使用多种配置拥有多个速度引擎,模型的使用方式如下:
导入 org.apache.velocity.app.VelocityEngine;
导入 org.apache.velocity.Template;

...


// 创建一个新的引擎实例
 VelocityEngine ve = new VelocityEngine();


// 配置引擎。在这种情况下,我们使用
// 我们自己作为记录器(参见记录示例..)


ve.setProperty(
    VelocityEngine.RUNTIME_LOG_LOGSYSTEM,这个);

// 初始化引擎
ve.init();

...

模板 t = ve.getTemplate("foo.vm");

因此,只需选择您想要使用的模型并遵循它即可。但以线程方式调用 Velocity.init() 肯定会产生不良行为。

There are two models for velocity usage:

  1. Singleton model i.e. Velocity.init(..), here you have only a single velocity configuration in your app. You should call init only once in this case in the application startup via a listener or any sort of initializing bean.
  2. The multimodel starting version 1.2 you can have multiple velocity engines using multiple configurations the model is used as such:
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");

so just choose the model you wish to use and follow it. but invoking Velocity.init() in a threaded way certainly should have an undesirable behavior.

客…行舟 2024-12-30 14:08:04

这个问题是因为我的代码不是线程安全的。就这样解决了问题。我仍然理解在 SingleThreadExecutor 用例中代码需要线程安全。
这就是答案——
使用 ExecutorService 执行异步任务时出现问题

This issue was because my code was not thread safe. Making it so fixed the issue. im still understanding the need for the code to be thread safe in a SingleThreadExecutor use case.
This is what lead to the answer --
Issue when executing asynchronous tasks using ExecutorService

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文