Java Velocity Engine Initialization Problem

I have a written library in which there is a postal part. This postal part uses 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 it comes from eclipse, the results look as expected, and everything seems to be fine. I have a web application that accepts a request from the user interface and uses ExecutorService (newSingleThreadExecutor) to serve these user requests one by one in the background.

I notice that my calls to the aforementioned library were hung up on the mailbuilding part specifically in Velocity.init(props) An exception was thrown, but the thread seems to freeze when initializing VelocityEngine. I looked online and had no luck with what could be the problem. Any help on how the problem would be huge.

Thanks P1nG

+4
source share
2 answers

This problem was due to the fact that my code was not thread safe. Fix this problem. im still understanding the need for code security when using the SingleThreadExecutor. Here's what led to the answer - Problem while performing asynchronous tasks using ExecutorService

+1
source

There are two models for using speed:

  • Singleton model, i.e. Velocity.init(..) , here you have only one speed configuration in your application. You should call init only once in this case when starting the application through a listener or any bean initialization.
  • In the multi-mode start version 1.2, you can have several high-speed engines using several configurations that are used as a model:
 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"); 

just select the model you want to use and follow it. but when calling Velocity.init () in streaming mode, it must have unwanted behavior.

+9
source

Source: https://habr.com/ru/post/1388460/


All Articles