/
BroadcastJobListener.cs
116 lines (102 loc) · 3.83 KB
/
BroadcastJobListener.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#region License
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Quartz.Listener
{
/// <summary>
/// Holds a List of references to JobListener instances and broadcasts all
/// events to them (in order).
/// </summary>
/// <remarks>
/// <para>The broadcasting behavior of this listener to delegate listeners may be
/// more convenient than registering all of the listeners directly with the
/// Scheduler, and provides the flexibility of easily changing which listeners
/// get notified.</para>
/// </remarks>
/// <seealso cref="AddListener(IJobListener)" />
/// <seealso cref="RemoveListener(IJobListener)" />
/// <seealso cref="RemoveListener(string)" />
/// <author>James House (jhouse AT revolition DOT net)</author>
public class BroadcastJobListener : IJobListener
{
private readonly List<IJobListener> listeners;
/// <summary>
/// Construct an instance with the given name.
/// </summary>
/// <remarks>
/// (Remember to add some delegate listeners!)
/// </remarks>
/// <param name="name">the name of this instance</param>
public BroadcastJobListener(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name), "Listener name cannot be null!");
}
Name = name;
listeners = new List<IJobListener>();
}
/// <summary>
/// Construct an instance with the given name, and List of listeners.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="name">the name of this instance</param>
/// <param name="listeners">the initial List of JobListeners to broadcast to.</param>
public BroadcastJobListener(string name, List<IJobListener> listeners) : this(name)
{
this.listeners.AddRange(listeners);
}
public string Name { get; }
public void AddListener(IJobListener listener)
{
listeners.Add(listener);
}
public bool RemoveListener(IJobListener listener)
{
return listeners.Remove(listener);
}
public bool RemoveListener(string listenerName)
{
IJobListener listener = listeners.Find(x => x.Name == listenerName);
if (listener != null)
{
listeners.Remove(listener);
return true;
}
return false;
}
public IReadOnlyList<IJobListener> Listeners => listeners;
public Task JobToBeExecuted(IJobExecutionContext context)
{
return Task.WhenAll(listeners.Select(l => l.JobToBeExecuted(context)));
}
public Task JobExecutionVetoed(IJobExecutionContext context)
{
return Task.WhenAll(listeners.Select(l => l.JobExecutionVetoed(context)));
}
public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
return Task.WhenAll(listeners.Select(l => l.JobWasExecuted(context, jobException)));
}
}
}