2011-12-29 94 views
4

我寫了一個包含mailbuilding部分的庫。這個mailbuilding部分使用了Velocity。該mailbuilder類是如下 -Java Velocity引擎初始化問題

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

回答

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"); 

所以才選擇你想使用,並按照它的模式。但以線程方式調用Velocity.init()肯定會有不良行爲。

+0

我已經嘗試通過與VelocityEngine obects工作。我得到了相同的結果,線程掛在ve.init()。 – ping 2011-12-29 16:47:11

+0

你可以嘗試在應用程序啓動時進行一次初始化嗎? – MahdeTo 2011-12-29 18:25:59

+0

嘗試了單例方法並面臨同樣的問題,線程掛在Velocity.init(prop)上。我也嘗試使用ClasspathResourceLoader的FileResourceLoader無效。 我開始認爲這個問題可能與傳遞給VelocityEngine的Properties對象相關。 – ping 2011-12-30 11:32:57