随笔 - 24  文章 - 1 评论 - 115 trackbacks - 0

///LOG记录

public static class ChildllyLog
{
static ILog log = LogManager.GetLogger(typeof(ChildllyLog));
public static void Log(string logTitle)
{
log.Info(logTitle);

}
public static void Error(string logTitle, Exception ex)
{
log.Error(logTitle, ex);

}
}

 

///构建一个JOB模板

public sealed class EtsJobInfo 
{
/// <summary>
/// 初始化构造函数,本job模板主要实现根据不同的IP 端口 实现 服务器PING 和telnet、已经实现模拟登陆的的功能。
/// </summary>
/// <param name="jobName"></param>
/// <param name="jobDescription"></param>
/// <param name="jobTrigger"></param>
/// <param name="jobIp"></param>
/// <param name="jobTrackingUrl"></param>
/// <param name="jobUserName"></param>
/// <param name="jobPSD"></param>
/// <param name="jobPort"></param>
/// <param name="jobType"></param>
public EtsJobInfo(string jobId, string jobName, string jobDescription, string jobTrigger, string jobIp, string jobTrackingUrl,
string jobUserName, string jobPSD, string jobPort, string jobType)
{
this._JobID = jobId;
this._JobName = jobName;
this._JobDescription = jobDescription;
this._Jobtrigger = jobTrigger;
this._JobIP = jobIp;
this._JobTrackingURL = jobTrackingUrl;
this._JobLoginUserName = jobUserName;
this._JobLoginPSD = jobPSD;
this._JobPort = jobPort;
this._JobType = jobType;
}
/// <summary>
/// 
/// </summary>
public EtsJobInfo()
{


}
private string _JobID;
/// <summary>
/// JOB ID
/// </summary>
public string JobID
{
get { return _JobID; }
set { _JobID = value; }
}

private string _JobName;
/// <summary>
/// JOB名称
/// </summary>
public string JobName
{
get { return _JobName; }
set { _JobName = value; }
}


private string _JobDescription;
/// <summary>
/// 
/// job描述
/// </summary>
public string JobDescription
{
get { return _JobDescription; }
set { _JobDescription = value; }
}
private string _Jobtrigger;
/// <summary>
/// JOB的触发器 这里使用 cron表达式
/// </summary>
public string Jobtrigger
{
get { return _Jobtrigger; }
set { _Jobtrigger = value; }
}
private string _JobIP;
/// <summary>
/// JOB的IP
/// </summary>
public string JobIP
{
get { return _JobIP; }
set { _JobIP = value; }
}

private string _JobTrackingURL;
/// <summary>
/// 如果是登陆的话,保存登陆的路径
/// </summary>
public string JobTrackingURL
{
get { return _JobTrackingURL; }
set { _JobTrackingURL = value; }
}

private string _JobLoginUserName;
/// <summary>
/// 登陆的用户名
/// </summary>
public string JobLoginUserName
{
get { return _JobLoginUserName; }
set { _JobLoginUserName = value; }
}
private string _JobLoginPSD;
/// <summary>
/// 登陆的密码
/// </summary>
public string JobLoginPSD
{
get { return _JobLoginPSD; }
set { _JobLoginPSD = value; }
}
private string _JobPort;
/// <summary>
/// 如果是模拟端口TELNET的话保存端口号
/// 
/// </summary>
public string JobPort
{
get { return _JobPort; }
set { _JobPort = value; }
}

private string _JobType;
/// <summary>
/// JOB类型 分为PING TELNET LOGIN三类 分别模拟PING,TELNET和登陆的操作
/// </summary>
public string JobType
{
get { return _JobType; }
set { _JobType = value; }
}
}

 

//定义JOB

public sealed class ChildllyQuartzJob : IJob
{

#region IJob 成员
public static int i = 0;
public void Execute(IJobExecutionContext context)
{
try
{
i++;

string jobName = context.JobDetail.Key + "---" + context.JobDetail.Description;
JobDataMap map = context.JobDetail.JobDataMap;//根据JOB传过来的值
EtsJobInfo sts = map["KEY"] as EtsJobInfo;
DateTime dt = DateTime.Now;
ChildllyLog.Log(sts.JobName + "->时间:" + dt.ToString("G"));
if (sts.JobType == "LOGIN")
{
//doing something
}
else if (sts.JobType == "TELNET")
{
//doing something
}
else if (sts.JobType == "PING")
{
//doing something
}
}
catch (Exception ex)
{
ChildllyLog.Error(ex.Message, ex);
}
}
#endregion
}

 

 

protected void Page_Load(object sender, EventArgs e)
{
List<EtsJobInfo> jobList = new List<EtsJobInfo>();
EtsJobInfo eji = new EtsJobInfo("1", "woody_test_01", "测试Jobs", "/15 * * * * ? ", "127.0.0.1", "http://127.0.0.1/1", "woody_01", "woody_01", "80", "PING");
jobList.Add(eji);
eji = new EtsJobInfo("2", "woody_test_02", "测试Jobs", "/15 * * * * ? ", "127.0.0.1", "http://127.0.0.1/2", "woody_02", "woody_02", "80", "PORT");
jobList.Add(eji);
eji = new EtsJobInfo("3", "woody_test_03", "测试Jobs", "/15 * * * * ? ", "127.0.0.1", "http://127.0.0.1/3", "woody_03", "woody_03", "80", "TELNET");
jobList.Add(eji);
eji = new EtsJobInfo("4", "woody_test_04", "测试Jobs", "/15 * * * * ? ", "127.0.0.1", "http://127.0.0.1/4", "woody_04", "woody_04", "80", "TELNET");
jobList.Add(eji);

//调度器
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler();
foreach (EtsJobInfo sj in jobList)
{
Dictionary<string, EtsJobInfo> etsJ = new Dictionary<string, EtsJobInfo>();
etsJ.Add("KEY", sj);
//设置JOB传递的参数,采取的是KEY-VALUE模式
JobDataMap map = new JobDataMap(etsJ);
IJobDetail job = JobBuilder
.Create<ChildllyQuartzJob>()
.WithIdentity(sj.JobName,"etsGroup01")
.UsingJobData(map) //--传递一个类对象过去
//.UsingJobData("NAME", sj.JobTrackingURL)//传递单个值
//.UsingJobData("LOGTYPE", sj.JobType)//传递单个值
.RequestRecovery() // ask scheduler to re-execute this job if it was in progress when the scheduler went down...
.Build();
//设置触发器
ITrigger trigger = TriggerBuilder.Create()

.WithIdentity("trigger" + sj.JobName, "etsGroup01")//触发器的模式选择cron表达式

.WithCronSchedule(sj.Jobtrigger)

.Build();


sched.ScheduleJob(job, trigger);
}
 

sched.Start();
}

posted on 2012-12-11 10:34 woody.wu 阅读(...) 评论(...) 编辑 收藏