On Wed, Aug 3, 2011 at 6:29 PM, mmjose26 <[hidden email]> wrote:
> Hi! I'm trying to implement some logic after of add some nodes to repository.
> I'm using asynchronous listener, and for each node of type X, the listener
> do version on it.
> I'm using transactional scenario, and all happen on commit. Almost always,
> the commit invalidate the session just before that the listener be notified
> at all. and the listener is not called.
Are you using the same session for both the listener and the
transaction? It's generally a good idea to use a separate long-lived
session for any event listeners you have.
Also, note that using the listener session for anything during the
onEvent() call is a bad practice, as you can't easily prevent
concurrent use of the session. Instead, if you need to do something
like create a new version in an event handler, I recommend either
using a task queue and a background worker thread (with its own
separate session) or launching a fresh new session for each new event
that needs repository writes.
Tanks very much for your reply and suggestions, I will try with queue and background worker thread.
I'm using a new fresh session for each new event, but onEvent() method never is called.
Maybe I found the reason of this behavior and I will try to explain what I found when debugging my app
Please correct me if I'm wrong with my assumptions.
1) We need register a Event Listener via observationManager, this observationManager uses the current session object, adding a new EventConsumer( using the current session object).
2) On commit:
a) Synchronous eventListeners are executed and the asynchronous listeners are queued.
b) Active session is invalidated after that.
But, because of we have dealing with threads,
in some point of time maybe after or before (here the point!) of session is invalidated,
notificationThread(the daemon thread of Observationdispatcher) call the run method and the EventConsumer.consumeEvent is called too. This method validate if the session (the object session used to register this evenConsumer) is valid.
if commit ends with session before that consumeEvent, the onEvent method is never called due to this validation, but we need to validate the session object, because Event object need it.