我有一个用MSSQLMVC2编写的应用程序,它将用户预定事件的日期和时间存储在ASP.NET数据库中。我需要应用程序发送一个电子邮件警报到用户72小时前存储的事件发生,没有任何人工干预。
实现这种并行过程的最佳方式是什么(我已经有了电子邮件代码)?
发布于 2011-06-22 02:33:00
假设您已将事件存储在数据库中,我将创建一个Windows Service,它独立于asp.net应用程序运行,该应用程序定期轮询数据库以查找要完成的工作。
发布于 2011-06-22 02:40:58
如果你可以控制服务器,那么我可能会选择Windows Service (如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 ClassArgs属性允许您从"ExecuteProcess“中访问您可能需要的任何参数/变量。
https://stackoverflow.com/questions/6430203
复制相似问题