Search in sources :

Example 21 with EventListener

use of javax.jcr.observation.EventListener in project jackrabbit-oak by apache.

the class ObservationTest method observationThroughput.

public void observationThroughput(final Repository repository, @Nullable Whiteboard whiteboard) throws RepositoryException, InterruptedException, ExecutionException {
    long t = 0;
    final AtomicInteger eventCount = new AtomicInteger();
    final AtomicInteger nodeCount = new AtomicInteger();
    List<Session> sessions = Lists.newArrayList();
    List<EventListener> listeners = Lists.newArrayList();
    List<String> testPaths = Lists.newArrayList();
    Session s = createSession(repository);
    String path = "/path/to/observation/benchmark-" + AbstractTest.TEST_ID;
    try {
        Node testRoot = JcrUtils.getOrCreateByPath(path, null, s);
        for (int i = 0; i < WRITER_COUNT; i++) {
            testPaths.add(testRoot.addNode("session-" + i).getPath());
        }
        s.save();
    } finally {
        s.logout();
    }
    String pathFilter = PATH_FILTER == null ? path : PATH_FILTER;
    System.out.println("Path filter for event listener: " + pathFilter);
    ExecutorService service = Executors.newFixedThreadPool(WRITER_COUNT);
    try {
        for (int k = 0; k < LISTENER_COUNT; k++) {
            sessions.add(createSession(repository));
            listeners.add(new Listener(eventCount));
            ObservationManager obsMgr = sessions.get(k).getWorkspace().getObservationManager();
            obsMgr.addEventListener(listeners.get(k), EVENT_TYPES, pathFilter, true, null, null, false);
        }
        // also add a listener on the root node
        addRootListener(repository, sessions, listeners);
        List<Future<Object>> createNodes = Lists.newArrayList();
        for (final String p : testPaths) {
            createNodes.add(service.submit(new Callable<Object>() {

                private final Session session = createSession(repository);

                private int numNodes = 0;

                @Override
                public Object call() throws Exception {
                    try {
                        Node testRoot = session.getNode(p);
                        createChildren(testRoot, 100);
                        for (Node m : JcrUtils.getChildNodes(testRoot)) {
                            createChildren(m, 100 / WRITER_COUNT);
                            for (Node n : JcrUtils.getChildNodes(m)) {
                                createChildren(n, 5);
                            }
                        }
                        session.save();
                    } finally {
                        session.logout();
                    }
                    return null;
                }

                private void createChildren(Node node, int count) throws RepositoryException {
                    for (int c = 0; c < count; c++) {
                        node.addNode("n" + c);
                        nodeCount.incrementAndGet();
                        if (++numNodes % SAVE_INTERVAL == 0) {
                            node.getSession().save();
                        }
                    }
                }
            }));
        }
        System.out.println("ms      #node   nodes/s #event  event/s event-ratio queue external");
        while (!isDone(createNodes) || (eventCount.get() / LISTENER_COUNT < nodeCount.get() * EVENTS_PER_NODE)) {
            long t0 = System.currentTimeMillis();
            Thread.sleep(OUTPUT_RESOLUTION);
            t += System.currentTimeMillis() - t0;
            int nc = nodeCount.get();
            int ec = eventCount.get() / LISTENER_COUNT;
            int[] ql = getObservationQueueLength(whiteboard);
            double nps = (double) nc / t * 1000;
            double eps = (double) ec / t * 1000;
            double epn = (double) ec / nc / EVENTS_PER_NODE;
            System.out.format("%7d %7d %7.1f %7d %7.1f %7.2f %7d %7d%n", t, nc, nps, ec, eps, epn, ql[0], ql[1]);
        }
        get(createNodes);
    } finally {
        for (int k = 0; k < sessions.size(); k++) {
            sessions.get(k).getWorkspace().getObservationManager().removeEventListener(listeners.get(k));
            sessions.get(k).logout();
        }
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : EventListener(javax.jcr.observation.EventListener) Node(javax.jcr.Node) ObservationManager(javax.jcr.observation.ObservationManager) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) EventListener(javax.jcr.observation.EventListener) Session(javax.jcr.Session)

Example 22 with EventListener

use of javax.jcr.observation.EventListener in project jackrabbit by apache.

the class GetRegisteredEventListenersTest method testGetSize.

public void testGetSize() throws RepositoryException {
    EventListener[] listeners = toArray(obsMgr.getRegisteredEventListeners());
    assertEquals("A new session must not have any event listeners registered.", 0, listeners.length);
    EventListener listener1 = new EventResult(log);
    EventListener listener2 = new EventResult(log);
    addEventListener(listener1);
    addEventListener(listener2);
    listeners = toArray(obsMgr.getRegisteredEventListeners());
    assertEquals("Wrong number of event listeners.", 2, listeners.length);
}
Also used : EventListener(javax.jcr.observation.EventListener)

Example 23 with EventListener

use of javax.jcr.observation.EventListener in project jackrabbit by apache.

the class RepositoryServiceImpl method executeWithLocalEvents.

private Object executeWithLocalEvents(Callable call, SessionInfoImpl sInfo) throws RepositoryException {
    if (supportsObservation) {
        // register local event listener
        Collection<EventSubscription> subscr = sInfo.getSubscriptions();
        if (subscr.size() != 0) {
            ObservationManager obsMgr = sInfo.getSession().getWorkspace().getObservationManager();
            List<EventListener> listeners = new ArrayList<EventListener>(subscr.size());
            try {
                for (EventSubscription s : subscr) {
                    EventListener listener = s.getLocalEventListener();
                    listeners.add(listener);
                    obsMgr.addEventListener(listener, EventSubscription.ALL_EVENTS, "/", true, null, null, false);
                }
                return call.run();
            } finally {
                for (EventListener listener : listeners) {
                    try {
                        obsMgr.removeEventListener(listener);
                    } catch (RepositoryException e) {
                    // ignore and remove next
                    }
                }
            }
        }
    }
    // if we get here simply run as is
    return call.run();
}
Also used : ArrayList(java.util.ArrayList) ObservationManager(javax.jcr.observation.ObservationManager) RepositoryException(javax.jcr.RepositoryException) EventListener(javax.jcr.observation.EventListener)

Example 24 with EventListener

use of javax.jcr.observation.EventListener in project jackrabbit-oak by apache.

the class ObservationTest method doIncludeAncestorsRemove_WithGlobs.

void doIncludeAncestorsRemove_WithGlobs(OakEventFilter oef) throws Exception {
    Node testNode = getNode(TEST_PATH);
    testNode.addNode("a").addNode("b").addNode("c").addNode("d.jsp").setProperty("e", 42);
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    final AtomicBoolean done = new AtomicBoolean(false);
    final AtomicBoolean unexpected = new AtomicBoolean(false);
    final AtomicBoolean failure = new AtomicBoolean(false);
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                Event event = events.nextEvent();
                System.out.println("got: " + event);
                String path = "";
                try {
                    path = event.getPath();
                } catch (RepositoryException e) {
                    e.printStackTrace();
                    failure.set(true);
                }
                if (path.equals(TEST_PATH + "/a/b") && event.getType() == NODE_REMOVED) {
                    done.set(true);
                } else if (path.equals(TEST_PATH + "/a/b/c/d.jsp") && event.getType() == NODE_REMOVED) {
                    done.set(true);
                } else if (path.equals(TEST_PATH + "/a/b/c/d.jsp/jcr:primaryType") && event.getType() == PROPERTY_REMOVED) {
                    done.set(true);
                } else {
                    System.out.println("Unexpected event: " + event);
                    unexpected.set(true);
                }
            }
        }
    };
    oManager.addEventListener(listener, oef);
    Node b = testNode.getNode("a").getNode("b");
    b.remove();
    testNode.getSession().save();
    Thread.sleep(1000);
    assertTrue("didnt get either event", done.get());
    assertFalse("did get unexpected events", unexpected.get());
    assertFalse("got an exception", failure.get());
    oManager.removeEventListener(listener);
    testNode.getNode("a").remove();
    testNode.getSession().save();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) RepositoryException(javax.jcr.RepositoryException) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator)

Example 25 with EventListener

use of javax.jcr.observation.EventListener in project sling by apache.

the class SuperimposingManagerImpl method deactivate.

@Deactivate
protected synchronized void deactivate(final ComponentContext ctx) throws RepositoryException {
    try {
        // make sure initialization has finished
        if (null != initialization && !initialization.isDone()) {
            initialization.cancel(/* myInterruptIfRunning */
            true);
        }
        // de-register JCR observation
        if (resolver != null) {
            final Session session = resolver.adaptTo(Session.class);
            if (session != null && this.observationEventListeners != null) {
                for (EventListener eventListener : this.observationEventListeners) {
                    session.getWorkspace().getObservationManager().removeEventListener(eventListener);
                }
            }
        }
        // de-register all superimpsing resource providers
        for (final SuperimposingResourceProviderImpl srp : superimposingProviders.values()) {
            srp.unregisterService();
        }
    } finally {
        if (null != resolver) {
            resolver.close();
            resolver = null;
        }
        initialization = null;
        superimposingProviders.clear();
    }
}
Also used : EventListener(javax.jcr.observation.EventListener) Session(javax.jcr.Session) Deactivate(org.apache.felix.scr.annotations.Deactivate)

Aggregations

EventListener (javax.jcr.observation.EventListener)26 EventIterator (javax.jcr.observation.EventIterator)12 RepositoryException (javax.jcr.RepositoryException)11 Session (javax.jcr.Session)10 ObservationManager (javax.jcr.observation.ObservationManager)10 Event (javax.jcr.observation.Event)8 Node (javax.jcr.Node)6 ArrayList (java.util.ArrayList)3 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)3 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Future (java.util.concurrent.Future)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 Lock (javax.jcr.lock.Lock)2 Version (javax.jcr.version.Version)2 Test (org.junit.Test)2 RemoteException (java.rmi.RemoteException)1 HashSet (java.util.HashSet)1