在吉日嘎拉DotNet.WebForm中使用FluentScheduler调度任务
有些用户一直说系统发送的邮件一直收不到,投诉系统不正常,这时候怎么洗刷冤屈呢?将发送的每一封Email都保存到数据库中,并记录发送的日志,让用户无话可说。
自己创建3个表:
- MessageFailed - 失败记录(超过5次发送失败就保存到这里)
- MessageQueue - 信息队列 (成功了就放MessageSucceed,失败5次就保存到MessageFailed)
- MessageSucceed - 成功记录
使用FluentScheduler,直接在Web端调度,省去Windows服务程序。

FluentScheduler
Automated job scheduler with fluent interface.
相关代码,我是用了IRegisteredObject,避免Application Pool和进程重启造成的Job中断等异常:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace DotNet.Business
{
using DotNet.Model;
using DotNet.Business;
using DotNet.Utilities; using FluentScheduler;
using System.Web.Hosting; #region MessageRegistry
public partial class MessageRegistry : Registry
{
public MessageRegistry()
{
//不允许重复进入
NonReentrantAsDefault(); // Schedule an IJob to run at an interval
Schedule<MessageJob>().NonReentrant().ToRunNow().AndEvery().Seconds(); //// Schedule an IJob to run once, delayed by a specific time interval
//Schedule<MessageJob>().ToRunOnceIn(5).Seconds(); //// Schedule a simple job to run at a specific time
//Schedule(() => Console.WriteLine("It's 9:15 PM now.")).ToRunEvery(1).Days().At(21, 15); //// Schedule a more complex action to run immediately and on an monthly interval
//Schedule<MessageJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0); //// Schedule multiple jobs to be run in a single schedule
//Schedule<MessageJob>().AndThen<MessageJob>().ToRunNow().AndEvery(5).Minutes();
}
}
#endregion #region MessageJob
public partial class MessageJob : BaseManager, IBaseManager, IJob, IRegisteredObject
{
private readonly object _lock = new object(); private bool _shuttingDown; public MessageJob()
{
//Register this job with the hosting environment.
//Allows for a more graceful stop of the job, in the case of IIS shutting down.
HostingEnvironment.RegisterObject(this);
} public void Execute()
{
lock (_lock)
{
if (_shuttingDown)
return; //Do work, son!
new MessageQueueManager(this.UserInfo).Resend();
}
} public void Stop(bool immediate)
{
//Locking here will wait for the lock in Execute to be released until this code can continue.
lock (_lock)
{
_shuttingDown = true;
} HostingEnvironment.UnregisterObject(this);
} }
#endregion
}
调度的时候,在Global.asax中调度如下:
//FluentScheduler任务调度
FluentScheduler.JobManager.Initialize(new DotNet.Business.MessageRegistry());
Message相关的代码,MessageFailed中的邮件发送Error的记录暂未实现:
#region 重新发送消息
/// <summary>
/// 重新发送消息
/// </summary>
/// <param name="id">主键</param>
/// <returns>是否成功</returns>
public bool Resend(MessageQueueEntity entity, int maxFailCount = )
{
bool result = false;
if (entity.MessageType.ToLower().Contains("mail"))
{
if (MailUtil.Send(entity.Recipient, entity.Subject, entity.Body))
{
//发送成功,移动数据到MessageSucceed表
MessageSucceedEntity entitySuccesed = new MessageSucceedEntity();
entitySuccesed.MessageType = entity.MessageType;
entitySuccesed.Recipient = entity.Recipient;
entitySuccesed.Subject = entity.Subject;
entitySuccesed.Body = entity.Body;
entitySuccesed.CreateOn = entity.CreateOn;
new MessageSucceedManager(this.UserInfo).Add(entitySuccesed);
//删除MessageQueue表中的数据
//this.Delete(entity.Id);
this.DeleteObject(entity.Id);
result = true;
}
else
{
//更新MessageQueue表中的失败次数
entity.FailCount = entity.FailCount + ;
this.UpdateObject(entity);
if (entity.FailCount >= maxFailCount)
{
//发送失败超过5次,移动数据到MessageFailed表
MessageFailedEntity entityFailed = new MessageFailedEntity();
entityFailed.MessageType = entity.MessageType;
entityFailed.Recipient = entity.Recipient;
entityFailed.Subject = entity.Subject;
entityFailed.Body = entity.Body;
entityFailed.FailCount = entity.FailCount;
entityFailed.CreateOn = entity.CreateOn;
//entityFailed.Error = "";
new MessageFailedManager(this.UserInfo).Add(entityFailed);
//删除MessageQueue表中的数据
//this.Delete(entity.Id);
this.DeleteObject(entity.Id);
result = false;
}
result = false;
} }
return result;
}
#endregion #region 重新发送所有队列
/// <summary>
/// 重新发送所有队列
/// </summary>
/// <returns>发送成功数量</returns>
public int Resend(int maxFailCount = )
{
int result = ;
//每次发一封,避免超时,任务不停启动而listEntity并未重新获取
List<MessageQueueEntity> listEntity = this.GetList<MessageQueueEntity>(, MessageQueueEntity.FieldId);
foreach (var entity in listEntity)
{
if (this.Resend(entity, maxFailCount))
{
result++;
}
} return result;
}
#endregion
因为这个后台Job的调度是邮件发送,其实MVC的项目也可以直接用上述代码。
在吉日嘎拉DotNet.WebForm中使用FluentScheduler调度任务的更多相关文章
- 吉日嘎拉DotNet.BusinessV4.2中的一处bug,及我的修复和扩展
bug所在位置:DotNet.Business\Utilities\BaseManager.GetDataTableByPage.cs的函数 public virtual DataTable GetD ...
- C#开发中Windows域认证登录2(扩展吉日嘎拉GPM系统)
原文地址:http://www.cuiwenyuan.com/shanghai/post/Windows-AD-Logon-Intergrated-into-Jirigala-GPM-DotNet-B ...
- C#开发中Windows域认证登录2016(扩展吉日嘎拉GPM系统V4.2)
2013年搞公司的OA时,为了统一用户登录,将Windows AD的用户和OA的账号对接,OA用户名的规则就是使用Windows AD的用户名,格式举例:Troy.Cui,原理就是先进行域服务器的认证 ...
- 基于吉日嘎拉的通用权限管理Webform版老界面bug修复
虽然弄了新界面<基于吉日嘎底层架构的通用权限管理Web端UI更新:参考DTcms后台界面>,但老界面的一点菜单显示的问题还是让我这种强迫症揪心,终于今晚可以美美的睡觉了. 老代码用了Ses ...
- 吉日嘎拉C#快速开发平台V4.0到V4.2升级记
目前我用的版本是4.0的,也有近2年没更新了,狠了狠心升级一下,没想到真的行动起来,也没那么难! 用了3天时间,将吉日嘎拉的代码升级到了4.2版本,并让原来的DotNet.WebApplication ...
- webform 中使用ajax
常用的方式有 js –> WebService , js->*.ashx, js->WebAPI, js->MVC Controller->Action. 前两种就不说 ...
- 基于吉日嘎拉的OA协同办公模块重写
这一个月的业余时间主要是在忙这个重构的事情,将吉日嘎拉自带的文档管理.公司公告.留言板.通讯录.周任务.考勤,全部重新建表,重构代码和UI. 目前根据中小企业常用的日常办公需要,搞定了公告栏.任务中心 ...
- webform中使用webapi,并且使用autofac
private void AutofacIoCRegister() { HttpConfiguration config = GlobalConfiguration.Configuration; if ...
- 【Ext.Net学习笔记】01:在ASP.NET WebForm中使用Ext.Net
Ext.NET是基于跨浏览器的ExtJS库和.NET Framework的一套支持ASP.NET AJAX的开源Web控件,包含有丰富的Ajax运用,其前身是Coolite. 下载地址:http:// ...
随机推荐
- DNS 正向查找与反向查找
原创地址:http://www.cnblogs.com/jfzhu/p/3996323.html 转载请注明出处 所谓正向查找,就是说在这个区域里的记录可以依据名称来查找对应的IP地址.反向查找就是在 ...
- web系统架构设计中需要知道的点(前端篇)
上周没写东西,这周写点互联网系统开发中需要了解的技术点,每个点都可以发散出去,连接更多的知识点,打算做个逐步细化的记录. 一个应用的整个生命周期中(生,老,病,死)都需要有一个整体规划. 前期 评估需 ...
- ci框架里rewrite示例
ci里新建应用app,入口文件app.php. Nginx 这里附上vhost配置 app.52fhy.com.conf server { listen 80; server_name app.52f ...
- 实用的开放源码的Excel导入导出类库 CarlosAg ExcelXmlWriter
做企业管理软件经常会遇到要把数据导出成EXCEL格式,目前市面上有很多工具类库可以实现此功能.CarlosAg ExcelXmlWriter是其中之一,它绿色小巧,免安装,又源码开放,我在项目中一直以 ...
- GridView里做页面的链接
采用Gridview的OnRowDataBound属性 后台 protected void gvTranslateInfo_RowDataBound(object sender, GridViewRo ...
- Hard Drive Inspector Pro 4.26.208(硬盘检测工具)简体中文特别版
Hard Drive Inspector监视硬盘错误并且接收警报,检查变化并实施诊断.例如:驱动器旋转时间增加以及数次重试才能运转驱动器通常意味着发动机和/或者轴存在可以导致数据丢失的错误.HardD ...
- [转载]TFS发送邮件提醒功能
第一次使用TFS 2010,发现有Project Alerts功能,就是项目组工程中若有任何改动时,TFS Server会自动发邮件提醒.Microsoft提供的配置方法(http://msdn.mi ...
- js每天进步一点点3
JS之样式的改变
- 【转】图文详解YUV420数据格式
YUV格式有两大类:planar和packed. 对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V.对于packed的YUV格式,每个像素点的Y, ...
- 新人学习Android开发遇到的小问题总结
1. IDE搭建: 搭建android的IDE时,先注意是什么版本的系统,64/32位系统. 通常使用的是Eclipse for android,Android Studio由于还需要FQ,网速慢,所 ...