我正在开发一个使用Admin
数据库的应用程序,该数据库包含所有客户端及其数据库的详细信息(以及服务器的名称),它们都用于连接此应用程序。所有客户端数据库都包含一个名为NotifyQueue
. 该表保存了所有在预设时间发送的排队邮件(预设是指每个客户端设置的时间间隔)。
我们的前端应用程序编写了一项服务,该服务触发从所有这些NotifyQueue
表中为所有客户端发送邮件。问题是我们目前有超过 600 个数据库分布在不同的服务器上,我们需要提取这 1000NotifyQueue
封邮件并发送它们。这需要相当多的时间,有时大约需要 15 到 20 分钟。我们需要的是保持每 1 分钟发送这么多邮件。目前使用的逻辑是遍历每个数据库并检索排队的邮件,然后再次遍历每个邮件以发送它。代码如下
private void SendEMail(List<EmailNotifyQueue> listNotifyQueue, SqlConnection objConn, string Module, EmailSettings emailSettings)
{
try
{
for (int i = 0; i < listNotifyQueue.Count; i++)
{
ClientConfig cfg = ClientConfig.CurrentSetings;
objemailinfo = new EmailInformation();
objemailinfo.DatabaseName = emailSettings.DatabaseName;
objemailinfo.DatabaseServer = emailSettings.DatabaseServer;
objemailinfo.SmtpServer = cfg.SMTPServer;
objemailinfo.RequireAuthentication = emailSettings.EmailRequiresAuthentication;
objemailinfo.SmtpUserName = emailSettings.EmailUserName;
objemailinfo.SmtpPassWord = emailSettings.EmailPassword;
objemailinfo.EnableNotifications = emailSettings.EnableNotifications;
if (lstNotifyQueue[i].ToAddress != string.Empty)
{
objemailinfo.To = ExcludeMail(lstNotifyQueue[i].ToAddress.Replace(';', ','), emailSettings.EmailExclusions, emailSettings.DatabaseName);
objemailinfo.Body = lstNotifyQueue[i].Body;
if (Module == "IM")
objemailinfo.From = lstNotifyQueue[i].FromAddress;
else
objemailinfo.From = emailSettings.EmailAddress;
if (objemailinfo.From != string.Empty)
{
objemailinfo.Subject = lstNotifyQueue[i].Subject;
if (objemailinfo.To != string.Empty)
if (objemailinfo.sendEmail())
{
ChangeEmailNotifyQueue(Convert.ToInt32(lstNotifyQueue[i].Key), objConn, emailSettings.DatabaseName, Module);
if (!String.Equals(Module, "IM"))
{
try
{
LogEmailNotificationInModules(lstNotifyQueue[i], objConn);
}
catch (Exception ex)
{
//Let the exception not break the email sending logic so simply log the exception
SchedulerHelper.LogEmailNotification(SchedulerHelper.FormatException(emailSettings.DatabaseName, "Scheduler", "SendEMail",
"Exception occured while Logging EmailNotification Status" + ex.Message));
}
}
}
}
}
}
}
catch (Exception ex)
{
SchedulerHelper.LogEmailNotification(SchedulerHelper.FormatException(emailSettings.DatabaseName, "Scheduler", "SendEMail", ex.Message));
}
}
为每个数据库调用此方法,这意味着当前将调用 600 次
谁能帮我找到更好的解决方案来快速检索所有数据并在 1 分钟内发送?
更新 我们计划使用线程同时运行但逻辑混乱