2008-12-16 44 views
1

我使用Grails發送大量HTML電子郵件。我用的是SimpleTemplateEngine以這種方式創建我的電子郵件正文:Groovy/Grails SimpleTemplateEngine凍結

def ccIdToEmailMap = [:] 
def emailTemplateFile = Utilities.retrieveFile("email${File.separator}emailTemplate.gtpl") 
def engine = new SimpleTemplateEngine() 
def clientContacts = ClientContact.list() 
for(ClientContact cc in clientContactList) { 
    def binding = [clientContact : cc] 

    //STOPS (FREEZES) EITHER HERE OR.... 
    def template = template = engine.createTemplate(emailTemplateFile).make(binding) 

    //OR STOPS (FREEZES) HERE 
    def body = template.toString() 

    def email = [text: body, to: cc.emailAddress] 
    ccIdToEmailMap.put(cc.id, email) 
    println "added to map" 
} 
return ccIdToEmailMap 

這裏是我試圖使每個電子郵件正文中的模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 


<html> 
<head> 
<title>Happy Holidays from google Partners</title> 
</head> 

<body> 
    <table width="492" cellpadding="0" cellspacing="0" style="border:2px solid #acacac;margin:8px auto;" align="center"> 
     <tr> 
      <td colspan="5" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/cardbg.gif" width="492" height="10" border="0"></td> 
     </tr> 

     <tr> 
      <td width="6" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/sidebgl.gif" width="6" height="453" border="0"></td> 
      <td style="background:#fff;border:1px solid #acacac;padding:2px;" width="228"> 
       <div style="width:208px;margin:4px 8px 0px 8px; color:#515151;"> 
       <font face="Times New Roman" size="2"> 
       <span style="font:14px 'Times New Roman',times,serif;">Static text that is the same for each email 
       <br>&nbsp;<br> 
       More text 
       <br>&nbsp;<br> 
       We wish you health and happiness during the holidays and a year of growth in 2009. 
       </span> 
       </font> 
       </div> 
      </td> 
      <td style="background:#c9f4fe;border-top:1px solid #acacac;border-bottom:1px solid #acacac;" width="5"><img src="http://www.google.com/holiday2008/vertbg.gif" border="0" height="453" width="5"></td> 
      <td width="247" style="background:#fff;border:1px solid #acacac;"><img src="http://www.google.com/holiday2008/snowing.gif" width="247" height="453" border="0"></td> 
      <td width="6" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/sidebgr.gif" width="6" height="453" border="0"></td> 
     </tr> 
     <tr> 
      <td width="6" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/sidebgr.gif" width="6" height="38" border="0"></td> 
      <td colspan="3" style="border:1px solid #acacac;" align="center"><img src="http://www.google.com/holiday2008/happyholidays.gif" width="480" height="38" alt="Happy Holidays" border="0"></td> 
      <td width="6" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/sidebgr.gif" width="6" height="38" border="0"></td> 
     </tr> 
     <tr> 
      <td width="6" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/sidebgr.gif" width="6" height="120" border="0"></td> 
      <td colspan="3" style="background-color#fff;border:1px solid #acacac;padding:2px;" valign="top"> 
       <img src="http://www.google.com/holiday2008/gogl_logo_card.gif" width="140" height="40" alt="google partners" border="0" align="right" hspace="4" vspace="4" /> 
       <font face="Times New Roman" size="2"> 
       <div style="padding:4px;font:12pt 'Times New Roman',serif;color:#515151;"> 
       <span style="font-size:10pt"><i>from:</i></span> 

        <div style="padding:2px 4px;"> 
         <% clientContact.owners.eachWithIndex { it, i -> %> 
          <% if(i < (clientContact.owners.size() - 1)) { %> 
           ${it.toString()}, 
          <% }else { %> 
           ${it.toString()} 
          <% } %> 
         <% } %> 
        </div> 
       </div> 
       </font> 
      </td> 
      <td width="6" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/sidebgr.gif" width="6" height="120" border="0"></td> 
     </tr> 
     <tr> 
      <td colspan="5" bgcolor="#c1e0f3"><img src="http://www.google.com/holiday2008/cardbg.gif" width="492" height="10" border="0"></td> 

     </tr> 
    </table> 
</body> 
</html> 

一旦這個方法返回ccIdToEmail地圖,我發送了所有的電子郵件。出於某種原因,準備clientContactIds和電子郵件正文的這張地圖會導致我的應用程序凍結在上面列出的兩條線中的任何一條。我可以在凍結之前成功地準備/發送140封電子郵件。這種情況非常一致。

有沒有人知道爲什麼這會工作,但之後停止工作,從模板創建〜140個電子郵件正文?我一直無法在網上找到任何有關其他人的問題。

安德魯

回答

0

看來,有一個與模板延遲加載我的客戶接觸業主的問題。而不是期望所有者被加載(效率低下),而SimpleTemplateEngine正在製作電子郵件正文,所以我在綁定/創建正文前熱切地獲取所有者。

我上面的代碼現在看起來是這樣的:

def emailTemplateFile = null 
    def ccIdToEmailMap = [:] 

    emailTemplateFile = Utilities.retrieveFile("email${File.separator}emailTemplate.gtpl") 
    def engine = new SimpleTemplateEngine() 
    def template = engine.createTemplate(emailTemplateFile) 
    for(ClientContact cc in clientContactList) 
    { 
     //there was a locking problem when we tried to create the template for too many client contacts 
     //i believe it was caused by lazy-fetching of the person/owners. So, I fetch them before we bind 
     //and make the email body. 
     def criteria = ClientContact.createCriteria() 
     cc = criteria.get { 
      eq("id", cc.id) 
      fetchMode('relationship', FM.EAGER) 
      fetchMode('relationship.person', FM.EAGER) 
     } 
     def binding = [clientContact : cc] 
     def body = template.make(binding).toString() 
     def email = [text: body, to: cc.emailAddress] 
     ccIdToEmailMap.put(cc.id, email) 
    } 

    return ccIdToEmailMap 

它仍然有種效率低下,使許多查詢每個客戶接觸的,但它的作品。我無法解釋爲什麼在模板製作過程中加載它們會導致grails/groovy凍結,但它確實如此。如果任何人都可以解釋,我將不勝感激。

謝謝你的回答。齊格弗裏德......你讓我從正確的方向開始。

安德魯

1

聽起來像一個同步問題。作爲第一步,您應該在循環之外創建模板。由於不需要每次重新創建模板。

def ccIdToEmailMap = [:] 
    def emailTemplateFile = Utilities.retrieveFile("email${File.separator}emailTemplate.gtpl") 
    def engine = new SimpleTemplateEngine() 
    def template = engine.createTemplate(emailTemplateFile) 
    def clientContacts = ClientContact.list() 
    for(ClientContact cc in clientContactList) 
    { 
      def binding = [clientContact : cc] 
      def body = template.make(binding).toString() 
      def email = [text: body, to: cc.emailAddress] 
      ccIdToEmailMap.put(cc.id, email) 
      println "added to map" 
    } 
    return ccIdToEmailMap 

如果它沒有幫助,它可能會幫助您發佈模板內容和/或ClientContact的來源。

心連心,全球皆姐妹研究所

+0

我改變了我的代碼,你有什麼,但它還是引起了在141凍結我嘗試了創建所有電子郵件機構,但不發送,並與160多個作品。發送這些電子郵件會導致問題。但是,從我的println聲明看來,它在發送之前似乎凍結了。 – anschoewe 2008-12-16 18:07:38