4

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

ping
  • 1,229
  • 3
  • 21
  • 41

2 Answers2

10

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.

MahdeTo
  • 11,034
  • 2
  • 27
  • 28
  • I have already tried by working with obects of VelocityEngine. I got the same result, the thread hung at ve.init(). – ping Dec 29 '11 at 16:47
  • could you try having a single initialization on app startup ? – MahdeTo Dec 29 '11 at 18:25
  • Tried the singleton approach and faced the same issue, thread hung at Velocity.init(prop). I also tried using the FileResourceLoader instaed of the ClasspathResourceLoader to no avail. Im beginning to think that the issue may be with the Properties object im passing to VelocityEngine. – ping Dec 30 '11 at 11:32
  • I would analyze by taking a stack-trace of the process to see where exactly the code hangs. Use "jstack " or some tool like VisualVM to get a stacktrace. – centic Dec 30 '11 at 17:13
1

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

Community
  • 1
  • 1
ping
  • 1,229
  • 3
  • 21
  • 41