我有一个用ASP.NET MVC 2编写的应用程序,它将用户预定事件的日期和时间存储在MSSQL数据库中。我需要应用程序在存储事件发生前72小时向用户发送电子邮件警报,而无需任何手动干预。
实现此并行流程的最佳方式是什么(我已经有电子邮件代码)?
答案 0 :(得分:0)
假设您已将事件存储在数据库中,我将创建一个独立于asp.net应用程序运行的Windows服务,该应用程序定期轮询数据库以查找要完成的工作。
答案 1 :(得分:0)
如果您可以控制服务器,那么我可能会使用Windows服务(如The Evil Greebo said)。
但假设你没有,我会创建一个类,可能类似于下面的类,你可以使用/继承它。在global.asax中的“Application_start”事件期间实例化此类,并将其副本保存在缓存中。将您的逻辑放在“ExecuteProcess”方法中。你的逻辑可能看起来像(伪代码):
while(true)
check for events that need to have notifications sent
send any needed notifications
wait x seconds and repeate
loop
确保MSSQL数据库表中有一个字段,用于跟踪是否已发送通知。
基础基类:
Imports System.Threading
Public MustInherit Class LongRunningProcess
Public ReadOnly Property Running() As Boolean
Get
Return _Running
End Get
End Property
Public ReadOnly Property Success() As Boolean
Get
Return _Success
End Get
End Property
Public ReadOnly Property Exception() As Exception
Get
Return _Exception
End Get
End Property
Public ReadOnly Property StartTime() As DateTime
Get
Return _StartTime
End Get
End Property
Public ReadOnly Property EndTime() As DateTime
Get
Return _EndTime
End Get
End Property
Public ReadOnly Property Args() As Object
Get
Return _Args
End Get
End Property
Protected _Running As Boolean = False
Protected _Success As Boolean = False
Protected _Exception As Exception = Nothing
Protected _StartTime As DateTime = DateTime.MinValue
Protected _EndTime As DateTime = DateTime.MinValue
Protected _Args() As Object = Nothing
Protected WithEvents _Thread As Thread
Private _locker As New Object()
Public Sub Execute(ByVal Arguments As Object)
SyncLock (_locker)
'if the process is not running, then...'
If Not _Running Then
'Set running to true'
_Running = True
'Set start time to now'
_StartTime = DateTime.Now
'set arguments'
_Args = Arguments
'Prepare to process in a new thread'
_Thread = New Thread(New ThreadStart(AddressOf ExecuteProcess))
'Start the thread'
_Thread.Start()
End If
End SyncLock
End Sub
Protected MustOverride Sub ExecuteProcess()
End Class
Args属性允许您从“ExecuteProcess”中访问您可能需要的任何参数/变量。