You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Vidar Ramdal <vi...@idium.no> on 2010/10/15 11:46:03 UTC

Is an ObservationManager safe to use across threads?

Regarding the rule that says that a session should not be used by
multiple threads, I wonder if the same applies to ObservationManager.

Take the following example:
public class SomeFactory {

  Map<String, SomeOtherClass> cache = Collections.synchronizedMap(new
HashMap<String, SomeOtherClass>());
  ObservationManager observationManager;

  public SomeFactory(Session session) {
     this.observationManager = session.getWorkspace().getObservationManager();
  }

  public get(String id) {
     if (!cache.containsKey(id)) {
        cache.put(id, new SomeOtherClass(this.observationManager, this));
     }
     return cache.get(id);
  }
}

public SomeOtherClass implements EventListener {

   private ObservationManager observationManager;
   private SomeFactory factory;

   public SomeOtherClass(ObservationManager observationManager,
SomeFactory factory) {
     this.factory = factory;
     this.observationManager = observationManager;
     this.observationManager.registerEventListener(this, ...);
  }

  public onEvent(EventIterator events) {
    // Invalidate ourselves in the factory's cache
    this.factory.cache.values().remove(this);
    this.observationManager.removeEventListener(this);
  }
}

Now, the case is that SomeFactory.get() can be called from different
threads, while SomeFactory.observationManager and SomeFactory.cache is
shared between threads.
Is this safe, or is the ObservationManager retrieved by
session.getWorkspace().getObservationManager() tied to the session
from which it was acquired?

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 22 00 84 76
Quando omni flunkus moritatus!

Re: Is an ObservationManager safe to use across threads?

Posted by Ian Boston <ie...@tfd.co.uk>.
On 15 Oct 2010, at 10:46, Vidar Ramdal wrote:

> Is this safe, or is the ObservationManager retrieved by
> session.getWorkspace().getObservationManager() tied to the session
> from which it was acquired?


In general its not safe since the session the observation manager was created with (which must stay logged in for the ObservationManager to remain active) is stored internally and used internally. There is one point where a check is made to ensure the same session is connected to dispatches,

however, in the case of removeEventListener although some new objects are created that are bound to the original session those objects are only used as keys to facilitate removal of the listener so you should be Ok.

BTW,
Using a session inside an event listener will almost certainly lead to massive blocking and potentially even deadlocks as the lower levels are protected (post 1.4 IIRC) to allow sessions to be shared between multiple threads without doing permanent damage, so think of the onEvent and the class/session that bound the on event as completely separate (and unfriendly) threads that will conflict. ie, be very careful what you share inside and outside the onEvent method.

HTH
Ian

Re: Is an ObservationManager safe to use across threads?

Posted by Vidar Ramdal <vi...@idium.no>.
> On Fri, Oct 15, 2010 at 11:46, Vidar Ramdal <vi...@idium.no> wrote:
>> Is this safe, or is the ObservationManager retrieved by
>> session.getWorkspace().getObservationManager() tied to the session
>> from which it was acquired?

On Fri, Oct 15, 2010 at 12:17 PM, Ian Boston <ie...@tfd.co.uk> wrote:
> In general its not safe since the session the observation manager was created with
> (which must stay logged in for the ObservationManager to remain active) is stored internally and used
> internally. There is one point where a check is made to ensure the same session is connected to dispatches,

On Fri, Oct 15, 2010 at 12:18 PM, Alexander Klimetschek
<ak...@day.com> wrote:
> Yes, it is session-specific (getWorkspace() is also a session-specific
> instance). So there is no guarantee that it works in multiple threads.

Guys, thanks for your explanations!

It is probably no surprise that I'm implementing a cache to hold some
data fetched from JCR, and that the cache should invalidate items if
they are modified (by listenting to events).

I now see that this will be messy, with a worker thread doing the JCR
stuff, and who knows what else. If anyone has pointers to best
practices/examples/experiences on anything like this, I will greatly
appreciate it.

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 22 00 84 76
Quando omni flunkus moritatus!

Re: Is an ObservationManager safe to use across threads?

Posted by Alexander Klimetschek <ak...@day.com>.
On Fri, Oct 15, 2010 at 11:46, Vidar Ramdal <vi...@idium.no> wrote:
> Is this safe, or is the ObservationManager retrieved by
> session.getWorkspace().getObservationManager() tied to the session
> from which it was acquired?

Yes, it is session-specific (getWorkspace() is also a session-specific
instance). So there is no guarantee that it works in multiple threads.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com