Java Velocity引擎初始化问题
我写了一个包含邮件构建部分的库。该邮件构建部分使用了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
速度使用有两种模型:
Velocity.init(..)
,在这里,您的应用程序中只有一个速度配置。在这种情况下,您应该在应用程序启动时通过侦听器或任何类型的初始化 bean 仅调用 init 一次。因此,只需选择您想要使用的模型并遵循它即可。但以线程方式调用 Velocity.init() 肯定会产生不良行为。
There are two models for velocity usage:
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.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.
这个问题是因为我的代码不是线程安全的。就这样解决了问题。我仍然理解在 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