use of javax.jcr.observation.EventIterator 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 javax.jcr.observation.EventIterator 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);
}
use of javax.jcr.observation.EventIterator in project camel by apache.
the class JcrConsumerTest method testJcrConsumer.
@Test
public void testJcrConsumer() throws Exception {
// start consumer thread first
JcrConsumerThread consumerThread = new JcrConsumerThread();
consumerThread.start();
// wait until the consumer thread has tried to receive event at least once
while (consumerThread.getReceiveTrialTimes() < 1) {
Thread.sleep(10L);
}
// now create a node under the specified event node path
Session session = openSession();
try {
Node folderNode = session.getRootNode();
for (String folderNodeName : absPath.split("\\/")) {
if (!"".equals(folderNodeName)) {
if (folderNode.hasNode(folderNodeName)) {
folderNode.getNode(folderNodeName).remove();
}
folderNode = folderNode.addNode(folderNodeName, "nt:unstructured");
}
}
folderNode.addNode("node", "nt:unstructured");
session.save();
} finally {
if (session != null && session.isLive()) {
session.logout();
}
}
// wait until the consumer thread captures an event
consumerThread.join();
Exchange exchange = consumerThread.getExchange();
assertNotNull(exchange);
Message message = exchange.getIn();
assertNotNull(message);
assertTrue(message instanceof JcrMessage);
EventIterator eventIterator = ((JcrMessage) message).getEventIterator();
assertNotNull(eventIterator);
assertEquals(1, eventIterator.getSize());
List<?> eventList = message.getBody(List.class);
assertEquals(1, eventList.size());
Event event = (Event) eventList.get(0);
assertEquals(Event.NODE_ADDED, event.getType());
assertNotNull(event.getPath());
assertTrue(event.getPath().startsWith(absPath));
}
use of javax.jcr.observation.EventIterator in project sling by apache.
the class SuperimposingManagerImpl method activate.
// ---------- SCR Integration
@Activate
protected synchronized void activate(final ComponentContext ctx) throws LoginException, RepositoryException {
// check enabled state
@SuppressWarnings("unchecked") final Dictionary<String, Object> props = ctx.getProperties();
this.enabled = PropertiesUtil.toBoolean(props.get(ENABLED_PROPERTY), ENABLED_DEFAULT);
log.debug("Config: " + "Enabled={} ", enabled);
if (!isEnabled()) {
return;
}
// get "find all" queries
this.findAllQueries = PropertiesUtil.toStringArray(props.get(FINDALLQUERIES_PROPERTY), new String[] { FINDALLQUERIES_DEFAULT });
this.obervationPaths = PropertiesUtil.toStringArray(props.get(OBSERVATION_PATHS_PROPERTY), new String[] { OBSERVATION_PATHS_DEFAULT });
if (null == resolver) {
bundleContext = ctx.getBundleContext();
resolver = resolverFactory.getAdministrativeResourceResolver(null);
// Watch for events on the root to register/deregister superimposings at runtime
// For each observed path create an event listener object which redirects the event to the main class
final Session session = resolver.adaptTo(Session.class);
if (session != null) {
this.observationEventListeners = new EventListener[this.obervationPaths.length];
for (int i = 0; i < this.obervationPaths.length; i++) {
this.observationEventListeners[i] = new EventListener() {
public void onEvent(EventIterator events) {
SuperimposingManagerImpl.this.onEvent(events);
}
};
session.getWorkspace().getObservationManager().addEventListener(this.observationEventListeners[i], Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, // absolute path
this.obervationPaths[i], // isDeep
true, // uuids
null, // node types
null, // noLocal
true);
}
}
// register all superimposing definitions that already exist
initialization = Executors.newSingleThreadExecutor().submit(new Runnable() {
public void run() {
try {
registerAllSuperimposings();
} catch (Throwable ex) {
log.warn("Error registering existing superimposing resources on service startup.", ex);
}
}
});
}
}
use of javax.jcr.observation.EventIterator in project sling by apache.
the class SuperimposingManagerImplTest method prepareNodeMoveEvent.
private EventIterator prepareNodeMoveEvent(Resource pResource, String pOldPath) throws RepositoryException {
String resourcePath = pResource.getPath();
Event nodeRemoveEvent = mock(Event.class);
when(nodeRemoveEvent.getType()).thenReturn(Event.NODE_REMOVED);
when(nodeRemoveEvent.getPath()).thenReturn(pOldPath);
Event nodeCreateEvent = mock(Event.class);
when(nodeCreateEvent.getType()).thenReturn(Event.NODE_ADDED);
when(nodeCreateEvent.getPath()).thenReturn(resourcePath);
EventIterator eventIterator = mock(EventIterator.class);
when(eventIterator.hasNext()).thenReturn(true, true, false);
when(eventIterator.nextEvent()).thenReturn(nodeRemoveEvent, nodeCreateEvent);
return eventIterator;
}
Aggregations