gpt4 book ai didi

coldfusion - 使用 Coldfusion 发送大量电子邮件

转载 作者:行者123 更新时间:2023-12-03 22:27:36 25 4
gpt4 key购买 nike

这个问题可能与做任何大量的事情有关,但在这种情况下,我正在尝试发送电子邮件。

我已经在一个新线程中设置了发送过程,因此用户无需等待,并将请求超时覆盖到一个小时。

问题是,一旦该进程发送了大约 2000 封电子邮件(在下面的代码上循环了大约 2000 次),服务器就会耗尽内存,停止响应,需要重新启动。

阅读有关此的其他主题,CF 应该能够很好地处理如此大量的电子邮件。

我考虑过的一件事是将所有对象调用更改为直接 DB 查询,并使用 cfmail 标记(我猜)删除所有对象,使其不再根据访问请求创建和构建(我猜这就是正在发生的事情),但我不确定这是否会有所作为,如果可能的话,真的想避免这种方法。我考虑过的其他事情是将它分成 3 或 4 个单独的线程,但同样,不确定这是否能解决问题。

有没有人遇到过这个问题,你发现什么工作可以让处理继续进行而不会让内存慢慢填满并杀死服务器?

thread name="sendBroadcastEmail" rc="#rc#" prc="#prc#" filters="#rc.filters#" email="#email#" emailSignature="#emailSignature#"{
createObject( "java", "coldfusion.tagext.lang.SettingTag" ).setRequestTimeout(javaCast( "double", 3600 ));

//get profiles that it will be sent to
var sendToProfiles = profileService.getWithFilters(rc.filters);

var mailService = getPlugin("MailService");
var emailSent = false;
var sentCount = 0;
var failedCount = 0;

//send the email (and log in profile events)
if (listFind(attributes.rc.email.action,'send')){

for ( i=1; i<=arrayLen(sendToProfiles);i++){
var profile = sendToProfiles[i];
try{

if (len(trim(profile.getPrimaryEmail()))){

var emailBody = profile.processDynamicPlaceholders(attributes.rc.email.body);
var emailBody = attributes.emailSignature.getHeader() & emailBody & attributes.emailSignature.getFooter();

var sendEmail = mailService.newMail(
from = attributes.emailSignature.getEmailAddress(),
//to = profile.getPrimaryEmail(),
to = Application.settings.testemail,
subject = attributes.rc.email.subject,
body = emailBody,
type="html");

sendEmail.addMailParam(disposition='attachment', file=attributes.email.getAttachmentWithPath());
mailService.send(sendEmail);

//log profile event
profile.saveEvent(eventType = 3,
title="Broadcast Email: #attributes.rc.email.subject#",
description="Broadcast Email Sent: Subject: <br> #attributes.rc.email.subject#",
sentContent=emailBody,
ref2=1);
sentCount++;
}
}
catch (any exception){
//log profile event
profile.saveEvent(eventType = 3,
title="FAILED Broadcast Email",
description="<br>Subject: #attributes.email.subject#<br>This email should have been sent to this profile, but the attempted send failed. The likely cause is a malformed email address.",
sentContent=emailBody,
ref2=0);
failedCount++;
}

}
emailSent = true;

}


//persist email object
if (listFind(attributes.rc.email.action,'save')){
email.setTstamp(attributes.prc.now);
email.setSent(emailSent);
email.setStatsSent(sentCount);
email.save();
}

}//end thread

最佳答案

一种方法是按定时批次生成电子邮件以均匀分布负载。可以调整批次大小和批次之间的延迟以适合您的环境。

    thread name="sendBroadcastEmail" rc="#rc#" prc="#prc#" filters="#rc.filters#" email="#email#" emailSignature="#emailSignature#"{
createObject( "java", "coldfusion.tagext.lang.SettingTag" ).setRequestTimeout(javaCast( "double", 3600 ));

// set thread to a lowish prority
var currentThread = CreateObject( "java","java.lang.Thread" ).currentThread();
var priority = currentThread.getPriority();
currentThread.setPriority( 3 );

//get profiles that it will be sent to
var sendToProfiles = profileService.getWithFilters(rc.filters);

var mailService = getPlugin("MailService");
var emailSent = false;
var sentCount = 0;
var failedCount = 0;

//send the email (and log in profile events)
if (listFind(attributes.rc.email.action,'send')){

var emailsPerBatch = 1000; // divide into batches, set size here
var batchcount = Ceiling( ArrayLen( sendToProfiles ) / emailsPerBatch ); // number of batches
var batchdelay = 120000; // set delay between batches (ms)
// initialise first batch
var firstitem = 1;
var lastitem = emailsPerBatch;

for( var batch=1; batch<=batchcount; batch++ ) {
if( batch > 1 ){
// delay sending next batch and give way to other threads
currentThread.yield();
currentThread.sleep( batchdelay );
}

for ( var i=firstitem; i<=lastitem;i++ ){
var profile = sendToProfiles[i];

// generate emails ...

}


// initialise next batch
firstitem = lastitem++;
lastitem += emailsPerBatch;
if( lastitem > ArrayLen( sendToProfiles ) ) {
// last batch
lastitem = ArrayLen( sendToProfiles );
}

}
emailSent = true;
}

currentThread.setPriority( priority ); // reset thread priority


}//end thread

关于coldfusion - 使用 Coldfusion 发送大量电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24356678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com