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();
}
};
}
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;
}
}
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);
}
Aggregations