Пример #1
0
  public void testQueryEvents() {

    EventRegistry registry = EventRegistryFactory.forObjectContainer(db());

    EventRecorder recorder = new EventRecorder(fileSession().lock());

    registry.queryStarted().addListener(recorder);
    registry.queryFinished().addListener(recorder);

    Assert.areEqual(0, recorder.size());

    Query q = db().query();
    q.execute();

    Assert.areEqual(2, recorder.size());
    EventRecord e1 = recorder.get(0);
    Assert.areSame(registry.queryStarted(), e1.e);
    Assert.areSame(q, ((QueryEventArgs) e1.args).query());

    EventRecord e2 = recorder.get(1);
    Assert.areSame(registry.queryFinished(), e2.e);
    Assert.areSame(q, ((QueryEventArgs) e2.args).query());

    recorder.clear();

    registry.queryStarted().removeListener(recorder);
    registry.queryFinished().removeListener(recorder);

    db().query().execute();

    Assert.areEqual(0, recorder.size());
  }
  public void apply(InternalObjectContainer container) {
    final Queries queries = Db4oMBeans.newQueriesMBean(container);
    final CommonConfiguration config =
        Db4oLegacyConfigurationBridge.asCommonConfiguration(container.configure());
    config
        .diagnostic()
        .addListener(
            new DiagnosticListener() {
              public void onDiagnostic(Diagnostic d) {
                if (d instanceof LoadedFromClassIndex) {
                  queries.notifyClassIndexScan((LoadedFromClassIndex) d);
                }
              }
            });

    final EventRegistry events = EventRegistryFactory.forObjectContainer(container);
    events
        .queryStarted()
        .addListener(
            new EventListener4<QueryEventArgs>() {
              public void onEvent(Event4<QueryEventArgs> e, QueryEventArgs args) {
                queries.notifyQueryStarted();
              }
            });

    events
        .queryFinished()
        .addListener(
            new EventListener4<QueryEventArgs>() {
              public void onEvent(Event4<QueryEventArgs> e, QueryEventArgs args) {
                queries.notifyQueryFinished();
              }
            });
  }