Reputation: 3649
I'm using QUARTZ.Net as my job scheduler in my project, my goal is that i want the user to be scheduled emails and trigger them (monthly/weekly/daily). lets say my host is on azure .. and i want to create an application and deploy it. where should i place my scheduled, what happens if i place it under
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
//here
}
}
i want it to always run no matter if there is a request or not.
Upvotes: 0
Views: 4666
Reputation: 1888
You can define three different jobs ( or you can trigger the same job more than once ) per day, weekly and monthly and assign the appropriate cron values to the trigger.
Use http://www.cronmaker.com/ for planning.
You can use a structure like the following.
In Global asax Application_Start method it will suffice to call QuartzServer.Start () method.
This code was written according to the latest version of quatz. This is important because there is no asynchronous support in previous versions of 3.0.
EMail Job :
With [DisallowConcurrentExecution] attribute, it is expected to finish the current job if it comes time to trigger again when the job is running
[DisallowConcurrentExecution]
public class MailJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
// Do Work
await Console.Out.WriteLineAsync("Mail Job is executing.");
}
}
Quartz Server Class
public class QuartzServer
{
private static IScheduler _scheduler;
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Start()
{
_scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult();
Load();
_scheduler.Start();
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Stop()
{
_scheduler.Shutdown(true);
}
[MethodImpl(MethodImplOptions.Synchronized)]
private static void Load()
{
JobCreator.CreateJob<MailJob>(new JobInfo
{
JobName = "MailJob",
TriggerName = "MainJobTrg",
GroupName = "MainJobGroup",
DataParamters = null,
CronExpression = "paste here cronmaker time planning string"
}, ref _scheduler);
// Define job Daily , weekly and mounthly
}
}
JobCreator Class
public sealed class JobCreator
{
public static void CreateJob<T>(JobInfo jInfo, ref IScheduler scheduler) where T : IJob
{
JobBuilder jbuilder = JobBuilder.Create<T>();
jbuilder.WithIdentity(jInfo.JobName, jInfo.GroupName);
if (jInfo.DataParamters != null && jInfo.DataParamters.Any())
{
foreach (var item in jInfo.DataParamters)
{
jbuilder.UsingJobData(GetDataMap(item));
}
}
IJobDetail jobDetail = jbuilder.Build();
TriggerBuilder tBuilder = TriggerBuilder.Create();
tBuilder.WithIdentity(jInfo.TriggerName, jInfo.GroupName)
.StartNow()
.WithCronSchedule(jInfo.CronExpression);
//.WithSimpleSchedule(x => x
// .WithIntervalInSeconds(10)
// .RepeatForever());
ITrigger trigger = tBuilder.Build();
scheduler.ScheduleJob(jobDetail, trigger);
}
private static JobDataMap GetDataMap(DataParameter dataParameter)
{
JobDataMap jDataMap = new JobDataMap();
switch (dataParameter.Value.GetType().Name)
{
case "Int32":
jDataMap.Add(dataParameter.Key, (int)dataParameter.Value);
break;
case "String":
jDataMap.Add(dataParameter.Key, dataParameter.Value);
break;
}
return jDataMap;
}
}
Upvotes: 2
Reputation: 226
we're using Quartz.NET but not send email , we're using get data other services.We have 2 applications for this Quartz Service and Quartz Client.
Quartz service is a Console Application and its create job.
Quartz Client is a MVC application and we monitoring , jobs manually trigger them.
This is the official tutorial Quartz Tutorial
Upvotes: 0