DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
EventRegistry.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 
20 
21 namespace Deveel.Data.Diagnostics {
22  public sealed class EventRegistry : ThreadedQueue<IEvent>, IEventRegistry, IDisposable {
23  private bool disposed;
24 
25  private readonly int threadCount;
26 
27  public EventRegistry(IContext context) {
28  if (context == null)
29  throw new ArgumentNullException("context");
30 
31  Context = context;
32 
33  var config = context.ResolveService<IConfiguration>();
34  threadCount = config.GetInt32("system.events.threadCount", 4);
35  }
36 
38  Dispose(false);
39  }
40 
41  public IContext Context { get; private set; }
42 
43  public override int ThreadCount {
44  get { return threadCount; }
45  }
46 
47  public void RegisterEvent(IEvent @event) {
48  Enqueue(@event);
49  }
50 
51  protected override void Consume(IEvent message) {
52  var routers = Context.ResolveAllServices<IEventRouter>();
53  foreach (var router in routers) {
54  try {
55  if (router.CanRoute(message))
56  router.RouteEvent(message);
57  } catch (Exception ex) {
58  Enqueue(new ErrorEvent(ex, -1, ErrorLevel.Critical));
59  }
60  }
61  }
62 
63  protected override void Dispose(bool disposing) {
64  if (disposed) {
65  Context = null;
66  disposed = true;
67  }
68 
69  base.Dispose(disposing);
70  }
71  }
72 }
This is an event occurred during the lifetime of a database.
Definition: IEvent.cs:24
void RouteEvent(IEvent e)
Routes the input event to the final destination.
virtual void Dispose(bool disposing)
Definition: Context.cs:63
ErrorLevel
In case of error messages, this enumerates the level of severity of the error.
Definition: ErrorLevel.cs:24
Implementations of this interface handle the registration of events fired within the system...
Defines the contract for the configuration node of a component within the system or of the system its...
override void Dispose(bool disposing)
void RegisterEvent(IEvent @event)
Adds the specified event object to the registry.
Defines the basic logic for the dispatching of events within a system workflow.
Definition: IEventRouter.cs:36
override void Consume(IEvent message)