Permalink
Browse files

releasing job components after execution

  • Loading branch information...
1 parent 983180c commit c5549a870f187ddca57366007b459095f857f33b @mausch mausch committed Jun 1, 2011
@@ -7,6 +7,7 @@
using Castle.Windsor;
using NUnit.Framework;
using Quartz;
+using System.Threading;
namespace Castle.Facilities.QuartzIntegration.Tests {
[TestFixture]
@@ -61,7 +62,7 @@ public class FacilityTests {
var scheduler = (QuartzNetScheduler)c.Resolve<IScheduler>();
foreach (IJobListener l in scheduler.GlobalJobListeners)
Console.WriteLine(l.Name);
- Assert.AreEqual(2, scheduler.GlobalJobListeners.Count);
+ Assert.AreEqual(3, scheduler.GlobalJobListeners.Count);
}
[Test]
@@ -124,5 +125,49 @@ public class FacilityTests {
Assert.IsNotNull(trigli);
}
+
+ [Test]
+ public void FacilityRegistersReleasingJobListener() {
+ var c = new WindsorContainer();
+ var config = new MutableConfiguration("facility");
+ config.CreateChild("quartz");
+ c.Kernel.ConfigurationStore.AddFacilityConfiguration("quartz", config);
+ c.AddFacility("quartz", new QuartzFacility());
+ var scheduler = c.Resolve<IScheduler>();
+ Assert.IsNotNull(scheduler.GlobalJobListeners);
+ Assert.AreEqual(2, scheduler.GlobalJobListeners.Count);
+ Assert.IsInstanceOfType(typeof(ReleasingJobListener), scheduler.GlobalJobListeners[0]);
+ }
+
+ [Test]
+ public void DisposableJobIsDisposed() {
+ var c = new WindsorContainer();
+ c.Register(Component.For<DisposableJob>().LifeStyle.Transient);
+ var config = new MutableConfiguration("facility");
+ config.CreateChild("quartz");
+ c.Kernel.ConfigurationStore.AddFacilityConfiguration("quartz", config);
+ c.AddFacility("quartz", new QuartzFacility());
+ var scheduler = c.Resolve<IScheduler>();
+ var jobDetail = new JobDetail("somejob", typeof(DisposableJob));
+ var trigger = TriggerUtils.MakeSecondlyTrigger("sometrigger");
+ scheduler.ScheduleJob(jobDetail, trigger);
+ Assert.IsFalse(DisposableJob.Disposed);
+ scheduler.Start();
+ Thread.Sleep(1000);
+ Assert.IsTrue(DisposableJob.Disposed);
+ }
+
+ class DisposableJob : IJob, IDisposable {
+ public static bool Disposed = false;
+
+ public void Dispose() {
+ Console.WriteLine("Dispose");
+ Disposed = true;
+ }
+
+ public void Execute(JobExecutionContext context) {
+ Console.WriteLine("Execute");
+ }
+ }
}
}
@@ -86,6 +86,7 @@
<Compile Include="QuartzFacility.cs" />
<Compile Include="QuartzNetScheduler.cs" />
<Compile Include="QuartzNetSimpleScheduler.cs" />
+ <Compile Include="ReleasingJobListener.cs" />
<Compile Include="WindsorJobFactory.cs" />
</ItemGroup>
<ItemGroup>
@@ -7,6 +7,7 @@
using Quartz.Collection;
using Quartz.Impl;
using Quartz.Spi;
+using Castle.MicroKernel;
namespace Castle.Facilities.QuartzIntegration {
public class QuartzNetScheduler : IScheduler, IStartable, IDisposable {
@@ -71,13 +72,14 @@ public class QuartzNetScheduler : IScheduler, IStartable, IDisposable {
get { return scheduler.GlobalJobListeners; }
}
- public QuartzNetScheduler(IDictionary<string, string> props, IJobFactory jobFactory) {
+ public QuartzNetScheduler(IDictionary<string, string> props, IJobFactory jobFactory, IKernel kernel) {
foreach (var prop in props.Keys) {
properties[prop] = props[prop];
}
var sf = new StdSchedulerFactory(properties);
scheduler = sf.GetScheduler();
scheduler.JobFactory = jobFactory;
+ scheduler.AddGlobalJobListener(new ReleasingJobListener(kernel));
WaitForJobsToCompleteAtShutdown = true; // default
}
@@ -0,0 +1,28 @@
+using System;
+using Quartz;
+using Castle.MicroKernel;
+
+namespace Castle.Facilities.QuartzIntegration {
+ public class ReleasingJobListener: IJobListener {
+ private readonly IKernel kernel;
+
+ public ReleasingJobListener(IKernel kernel) {
+ this.kernel = kernel;
+ }
+
+ public void JobExecutionVetoed(JobExecutionContext context) {
+ kernel.ReleaseComponent(context.JobInstance);
+ }
+
+ public void JobToBeExecuted(JobExecutionContext context) {
+ }
+
+ public void JobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
+ kernel.ReleaseComponent(context.JobInstance);
+ }
+
+ public string Name {
+ get { return GetType().FullName; }
+ }
+ }
+}

0 comments on commit c5549a8

Please sign in to comment.