Search in sources :

Example 1 with JackrabbitEvent

use of org.apache.jackrabbit.api.observation.JackrabbitEvent in project jackrabbit by apache.

the class ListenerTracker method getTrackedListener.

public EventListener getTrackedListener() {
    return new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            eventDeliveries.incrementAndGet();
            final long start = nanoTime();
            try {
                beforeEventDelivery();
                listener.onEvent(new EventIteratorAdapter(events) {

                    long t0 = start;

                    private void recordTime(TimeSeriesRecorder recorder) {
                        recorder.getCounter().addAndGet(-(t0 - (t0 = nanoTime())));
                    }

                    @Override
                    public Object next() {
                        recordTime(eventConsumerTime);
                        eventsDelivered.incrementAndGet();
                        eventCount.getCounter().incrementAndGet();
                        Object object = super.next();
                        if (object instanceof JackrabbitEvent) {
                            object = new JackrabbitEventTracker(ListenerTracker.this, (JackrabbitEvent) object);
                        } else if (object instanceof Event) {
                            object = new EventTracker(ListenerTracker.this, (Event) object);
                        }
                        recordTime(eventProducerTime);
                        return object;
                    }

                    @Override
                    public boolean hasNext() {
                        recordTime(eventConsumerTime);
                        boolean result = super.hasNext();
                        recordTime(eventProducerTime);
                        return result;
                    }
                });
            } finally {
                afterEventDelivery();
                eventDeliveryTime.addAndGet(nanoTime() - start);
            }
        }

        @Override
        public String toString() {
            return ListenerTracker.this.toString();
        }
    };
}
Also used : EventIteratorAdapter(org.apache.jackrabbit.commons.iterator.EventIteratorAdapter) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) Event(javax.jcr.observation.Event) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) TimeSeriesRecorder(org.apache.jackrabbit.stats.TimeSeriesRecorder) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator)

Example 2 with JackrabbitEvent

use of org.apache.jackrabbit.api.observation.JackrabbitEvent in project jackrabbit-oak by apache.

the class ObservationTest method externalEvents.

@Test
public void externalEvents() throws Throwable {
    final Set<String> externallyAdded = synchronizedSet(new LinkedHashSet<String>());
    final List<Throwable> exceptions = Lists.newArrayList();
    ObservationManager obsMgr = s1.getWorkspace().getObservationManager();
    final AtomicLong localEvents = new AtomicLong();
    final AtomicLong externalEvents = new AtomicLong();
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            try {
                Set<String> paths = Sets.newHashSet();
                while (events.hasNext()) {
                    Event event = events.nextEvent();
                    String external = "";
                    AtomicLong counter = localEvents;
                    if (event instanceof JackrabbitEvent) {
                        if (((JackrabbitEvent) event).isExternal()) {
                            external = " (external)";
                            counter = externalEvents;
                            paths.add(event.getPath());
                        }
                    }
                    System.out.println(event.getPath() + external);
                    counter.incrementAndGet();
                }
                while (!paths.isEmpty()) {
                    Iterator<String> it = externallyAdded.iterator();
                    String p = it.next();
                    assertTrue("missing event for " + p, paths.remove(p));
                    it.remove();
                }
            } catch (Throwable e) {
                exceptions.add(e);
            }
        }
    };
    obsMgr.addEventListener(listener, Event.NODE_ADDED, "/", true, null, null, false);
    Future f1 = executor.submit(new Worker(s1, exceptions, new HashSet<String>()));
    Future f2 = executor.submit(new Worker(s2, exceptions, externallyAdded));
    f1.get();
    f2.get();
    Thread.sleep(10 * 1000);
    System.out.println("local events: " + localEvents.get());
    System.out.println("external events: " + externalEvents.get());
    for (Throwable t : exceptions) {
        throw t;
    }
}
Also used : ObservationManager(javax.jcr.observation.ObservationManager) AtomicLong(java.util.concurrent.atomic.AtomicLong) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) Event(javax.jcr.observation.Event) Future(java.util.concurrent.Future) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 3 with JackrabbitEvent

use of org.apache.jackrabbit.api.observation.JackrabbitEvent in project jackrabbit-oak by apache.

the class NonLocalObservationIT method addEventHandler.

private void addEventHandler(Session s, final String expectedNodeSuffix) throws Exception {
    ObservationManager o = s.getWorkspace().getObservationManager();
    o.addEventListener(new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                Event e = events.nextEvent();
                if (!(e instanceof JackrabbitEvent)) {
                    continue;
                }
                if (((JackrabbitEvent) e).isExternal()) {
                    continue;
                }
                String p;
                try {
                    p = e.getPath();
                    // "+expectedNodeSuffix+", path: " + p);
                    if (!p.endsWith(expectedNodeSuffix)) {
                        log.info("EXCEPTION: expectedNodeSuffix: " + expectedNodeSuffix + ", path: " + p);
                        throw new Exception("expectedNodeSuffix: " + expectedNodeSuffix + ", non-local path: " + p);
                    }
                } catch (Exception e1) {
                    exception.set(e1);
                }
            }
        }
    }, Event.NODE_ADDED, "/", true, null, null, false);
}
Also used : JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) RepositoryException(javax.jcr.RepositoryException) AssumptionViolatedException(org.junit.AssumptionViolatedException)

Aggregations

Event (javax.jcr.observation.Event)3 EventIterator (javax.jcr.observation.EventIterator)3 EventListener (javax.jcr.observation.EventListener)3 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)3 ObservationManager (javax.jcr.observation.ObservationManager)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Future (java.util.concurrent.Future)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 RepositoryException (javax.jcr.RepositoryException)1 EventIteratorAdapter (org.apache.jackrabbit.commons.iterator.EventIteratorAdapter)1 TimeSeriesRecorder (org.apache.jackrabbit.stats.TimeSeriesRecorder)1 AssumptionViolatedException (org.junit.AssumptionViolatedException)1 Test (org.junit.Test)1