.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using System.Threading;
namespace Hizzah.JobListeners
{
public class BeforeJobExecutedEventArgs : EventArgs
{
JobExecutionContext _context;
public BeforeJobExecutedEventArgs(JobExecutionContext context)
{
_context = context;
}
public JobExecutionContext Context
{
get { return _context; }
set { _context = value; }
}
}
public class AfterJobExecutedEventArgs : EventArgs
{
JobExecutionContext _context;
public JobExecutionContext Context
{
get { return _context; }
set { _context = value; }
}
JobExecutionException _exception;
public JobExecutionException Exception
{
get { return _exception; }
set { _exception = value; }
}
public AfterJobExecutedEventArgs(JobExecutionContext context, JobExecutionException exception)
{
_context = context;
_exception = exception;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using System.Threading;
namespace Hizzah.JobListeners
{
public class HizzahJobListener : IJobListener
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public event EventHandler<BeforeJobExecutedEventArgs> BeforeJobExecution;
public event EventHandler<AfterJobExecutedEventArgs> AfterJobExecution;
public HizzahJobListener(string name)
{
_name = name;
}
#region IJobListener Members
private string _name;
public string Name
{
get { return _name; }
}
public void JobToBeExecuted(JobExecutionContext context)
{
_name = context.JobDetail.Name;
OnBeforeJobExecution(new BeforeJobExecutedEventArgs(context));
}
protected void OnBeforeJobExecution(BeforeJobExecutedEventArgs args)
{
// Copy a reference to the delegate field now into a temporary field for thread safety
EventHandler<BeforeJobExecutedEventArgs> temp = Interlocked.CompareExchange(ref BeforeJobExecution, null, null);
if (temp != null)
{
temp(this, args);
}
}
protected void OnAfterJobExecution(AfterJobExecutedEventArgs args)
{
// Copy a reference to the delegate field now into a temporary field for thread safety
EventHandler<AfterJobExecutedEventArgs> temp = Interlocked.CompareExchange(ref AfterJobExecution, null, null);
if (temp != null)
{
temp(this, args);
}
}
public void JobExecutionVetoed(JobExecutionContext context)
{
throw new NotImplementedException();
}
public void JobWasExecuted(JobExecutionContext context, JobExecutionException jobException)
{
_name = context.JobDetail.Name;
OnAfterJobExecution(new AfterJobExecutedEventArgs(context, jobException));
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using System.Threading;
namespace Hizzah.TriggerListeners
{
public class TriggerFiredEventArgs : EventArgs
{
JobExecutionContext _context;
Trigger _trigger;
public TriggerFiredEventArgs()
:base()
{}
public TriggerFiredEventArgs(Trigger trigger, JobExecutionContext context)
:base()
{
_context = context;
_trigger = trigger;
}
public JobExecutionContext Context
{
get { return _context; }
set { _context = value; }
}
public Trigger Trigger
{
get
{
return _trigger;
}
set
{
_trigger = value;
}
}
}
public class TriggerCompletedEventArgs : EventArgs
{
JobExecutionContext _context;
Trigger _trigger;
SchedulerInstruction _instruction;
public JobExecutionContext Context
{
get { return _context; }
set { _context = value; }
}
public Trigger Trigger
{
get
{
return _trigger;
}
set
{
_trigger = value;
}
}
public SchedulerInstruction Instruction
{
get
{
return _instruction;
}
set
{
_instruction = value;
}
}
public TriggerCompletedEventArgs()
{ }
public TriggerCompletedEventArgs(Trigger trigger, JobExecutionContext context, SchedulerInstruction instruction)
:base()
{
_context = context;
_trigger = trigger;
_instruction = instruction;
}
}
public class HizzahTriggerListener : ITriggerListener
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private string _name;
public event EventHandler<TriggerFiredEventArgs> TriggerFiredEvent;
public event EventHandler<TriggerCompletedEventArgs> TriggerCompletedEvent;
public HizzahTriggerListener(string name)
{
_name = name;
}
#region ITriggerListener Members
public string Name
{
get
{
return _name;
}
}
public void TriggerFired(Trigger trigger, JobExecutionContext context)
{
log.Debug("TriggerFired Fired");
OnTriggerFired(new TriggerFiredEventArgs(trigger, context));
}
public bool VetoJobExecution(Trigger trigger, JobExecutionContext context)
{
log.Debug("VetoJobExecution Fired");
return false;
//throw new NotImplementedException();
}
public void TriggerMisfired(Trigger trigger)
{
log.Debug("TriggerMisfired Fired");
throw new NotImplementedException();
}
public void TriggerComplete(Trigger trigger, JobExecutionContext context, SchedulerInstruction triggerInstructionCode)
{
log.Debug("TriggerComplete Fired");
OnTriggerCompleted(new TriggerCompletedEventArgs(trigger, context, triggerInstructionCode));
}
#endregion
public void OnTriggerFired(TriggerFiredEventArgs args)
{
// Copy a reference to the delegate field now into a temporary field for thread safety
EventHandler<TriggerFiredEventArgs> temp = Interlocked.CompareExchange(ref TriggerFiredEvent, null, null);
if (temp != null)
{
temp(this, args);
}
}
public void OnTriggerCompleted(TriggerCompletedEventArgs args)
{
// Copy a reference to the delegate field into a temp var for thread safety
EventHandler<TriggerCompletedEventArgs> temp = Interlocked.CompareExchange(ref TriggerCompletedEvent, null, null);
if (temp != null)
{
temp(this, args);
}
}
}
}
Tuesday, March 29, 2011
Event Based Listeners in Quartz.NET
Since Quartz.NET is ported from Java, it does not have an inkling of what a delegate is or should be. Below, I show how to use events in your Quartz.Net implementation. You can rename the namespaces and classes.
Subscribe to:
Posts (Atom)