Search in sources :

Example 1 with Invocation

use of org.apache.cayenne.util.Invocation in project cayenne by apache.

the class DefaultEventManager method addListener.

protected void addListener(Object listener, String methodName, Class<?> eventParameterClass, EventSubject subject, Object sender, boolean blocking) {
    if (listener == null) {
        throw new IllegalArgumentException("Listener must not be null.");
    }
    if (eventParameterClass == null) {
        throw new IllegalArgumentException("Event class must not be null.");
    }
    if (subject == null) {
        throw new IllegalArgumentException("Subject must not be null.");
    }
    try {
        Invocation invocation = (blocking) ? new Invocation(listener, methodName, eventParameterClass) : new NonBlockingInvocation(listener, methodName, eventParameterClass);
        dispatchQueueForSubject(subject, true).addInvocation(invocation, sender);
    } catch (NoSuchMethodException nsm) {
        throw new CayenneRuntimeException("Error adding listener, method name: " + methodName, nsm);
    }
}
Also used : Invocation(org.apache.cayenne.util.Invocation) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 2 with Invocation

use of org.apache.cayenne.util.Invocation in project cayenne by apache.

the class DispatchQueue method addInvocation.

void addInvocation(Invocation invocation, Object sender) {
    ConcurrentMap<Invocation, Object> invocations;
    if (sender == null) {
        invocations = subjectInvocations;
    } else {
        invocations = invocationsForSender(sender, true);
    }
    // perform maintenance of the given invocations set, as failure to do that can
    // result in a memory leak per CAY-770. This seemed to happen when lots of
    // invocations got registered, but no events were dispatched (hence the stale
    // invocation removal during dispatch did not happen)
    Iterator<Invocation> it = invocations.keySet().iterator();
    while (it.hasNext()) {
        Invocation i = it.next();
        if (i.getTarget() == null) {
            it.remove();
        }
    }
    invocations.putIfAbsent(invocation, Boolean.TRUE);
}
Also used : Invocation(org.apache.cayenne.util.Invocation)

Example 3 with Invocation

use of org.apache.cayenne.util.Invocation in project cayenne by apache.

the class DispatchQueue method removeInvocations.

// removes all invocations for a given listener
private boolean removeInvocations(ConcurrentMap<Invocation, Object> invocations, Object listener) {
    if (invocations == null || invocations.isEmpty()) {
        return false;
    }
    boolean didRemove = false;
    Iterator<Invocation> invocationsIt = invocations.keySet().iterator();
    while (invocationsIt.hasNext()) {
        Invocation invocation = invocationsIt.next();
        if (invocation.getTarget() == listener) {
            invocationsIt.remove();
            didRemove = true;
        }
    }
    return didRemove;
}
Also used : Invocation(org.apache.cayenne.util.Invocation)

Aggregations

Invocation (org.apache.cayenne.util.Invocation)3 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1