I have my own 开发者_开发百科ITrigger
. Basically, it looks like the below:
public interface ITrigger : IDisposable
{
/// <summary>
/// Occurs when an input has been trigger.
/// </summary>
event InputTriggedEventHandler InputTrigged;
/// <summary>
/// Starts the trigger.
/// </summary>
/// <param name="trigger">The data about the trigger to start.</param>
void Init(Trigger trigger);
}
One implementation of this interface is a FileCreatedTrigger
which fires the event when a file is created.
I want another implementation where I can set the Trigger
to fire at a certain interval (much like the Windows Task Scheduler). So, I looked at the Quartz.Net and it's pretty much what I want.
The question is How do I get the InputTrigged event to fire from an IJob
? which is what Quartz uses. The IJob only implements execute which cannot call the parent (which is in this case the ITrigger
as it does not know which instance this is.
Hope I made myself clear. I want to be able to keep my interface ITrigger
while using Quartz.Net
which has another implementation of how to trigger.
What I ended up doing was keeping a static reference to the ScheduleTriggers with guid as key. The guid is then passed to the jobdetail which uses it to find the ScheduleTrigger and raises the event. Not pretty, but does the job:
public class ScheduleTrigger : BaseTrigger
{
Guid name = Guid.NewGuid();
static Dictionary<Guid, ScheduleTrigger> triggers = new Dictionary<Guid, ScheduleTrigger>();
public static Dictionary<Guid, ScheduleTrigger> Triggers
{
get
{
return triggers;
}
}
public void Init(Trigger triggerParam)
{
....
JobDetail jobDetail = new JobDetail(name.ToString(), Type.GetType(schedTrig.JobType.JobClassName));
Triggers.Add(name, this);
}
public void Dispose()
{
if (Triggers.ContainsKey(name))
{
triggers.Remove(name);
}
base.Dispose();
}
internal void RaiseEvent()
{
base.OnInputTrigged(string.Empty);
}
}
And the very simple job to raise the event
EventRaiserJob : IJob
{
public void Execute(JobExecutionContext context)
{
Guid name = new Guid(context.JobDetail.Name);
ScheduleTrigger.Triggers[name].RaiseEvent();
}
}
精彩评论