I will try to answer your questions in the order in which you have them.
Yes it is possible. This is actually a general way to work with Quartz.Net. In fact, you can also write an ASP.Net MVC application that manages the Quartz.Net schedulers.
Architecture. Ideally and at a high level, your MVC application will use the Quartz.Net API to communicate with the Quartz.Net server, which is installed somewhere as a Windows service. Quartz.Net uses remote access for remote communication, so any restrictions on the use of remote access apply (for example, it is not supported in Silverlight, etc.). Quartz.Net provides the ability to install it as a Windows service out of the box, so there is not much work here, except to configure the service itself to use (in your case) AdoJobStore, as well as enabling Remoting. Care must be taken to properly install the service, so if you have not already done so, look in this message .
Inside your MVC application, you will want to get a link to the scheduler and save it as a singleton. Then, in your code, you will schedule tasks and receive information about the scheduler through this unique instance. You can use something like this:
public class QuartzScheduler { public QuartzScheduler(string server, int port, string scheduler) { Address = string.Format("tcp://{0}:{1}/{2}", server, port, scheduler); _schedulerFactory = new StdSchedulerFactory(getProperties(Address)); try { _scheduler = _schedulerFactory.GetScheduler(); } catch (SchedulerException) { MessageBox.Show("Unable to connect to the specified server", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } public string Address { get; private set; } private NameValueCollection getProperties(string address) { NameValueCollection properties = new NameValueCollection(); properties["quartz.scheduler.instanceName"] = "RemoteClient"; properties["quartz.scheduler.proxy"] = "true"; properties["quartz.threadPool.threadCount"] = "0"; properties["quartz.scheduler.proxy.address"] = address; return properties; } public IScheduler GetScheduler() { return _scheduler; } }
This code configures the Quart.Net client. Then, to access the remote scheduler, just call
GetScheduler()
Requests Here is an example code for receiving all tasks from the scheduler:
public DataTable GetJobs() { DataTable table = new DataTable(); table.Columns.Add("GroupName"); table.Columns.Add("JobName"); table.Columns.Add("JobDescription"); table.Columns.Add("TriggerName"); table.Columns.Add("TriggerGroupName"); table.Columns.Add("TriggerType"); table.Columns.Add("TriggerState"); table.Columns.Add("NextFireTime"); table.Columns.Add("PreviousFireTime"); var jobGroups = GetScheduler().GetJobGroupNames(); foreach (string group in jobGroups) { var groupMatcher = GroupMatcher<JobKey>.GroupContains(group); var jobKeys = GetScheduler().GetJobKeys(groupMatcher); foreach (var jobKey in jobKeys) { var detail = GetScheduler().GetJobDetail(jobKey); var triggers = GetScheduler().GetTriggersOfJob(jobKey); foreach (ITrigger trigger in triggers) { DataRow row = table.NewRow(); row["GroupName"] = group; row["JobName"] = jobKey.Name; row["JobDescription"] = detail.Description; row["TriggerName"] = trigger.Key.Name; row["TriggerGroupName"] = trigger.Key.Group; row["TriggerType"] = trigger.GetType().Name; row["TriggerState"] = GetScheduler().GetTriggerState(trigger.Key); DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc(); if (nextFireTime.HasValue) { row["NextFireTime"] = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTime.Value.DateTime); } DateTimeOffset? previousFireTime = trigger.GetPreviousFireTimeUtc(); if (previousFireTime.HasValue) { row["PreviousFireTime"] = TimeZone.CurrentTimeZone.ToLocalTime(previousFireTime.Value.DateTime); } table.Rows.Add(row); } } } return table; }
You can view this code on Github
jvilalta
source share