Re: Best way to inform (and handle cached object refreshing) of externally-updated database records?

From: Mike Kienenberger (mkienen..laska.net)
Date: Thu Dec 02 2004 - 13:43:40 EST

  • Next message: Andrus Adamchik: "Re: New User alert"

    Andrus Adamchik <andru..bjectstyle.org> wrote:
    > I haven't tried this, but it looks like local-VM cache synching may
    > actually work, even if you don't have a shared stack!
    >
    > As DataRowStore dispatches events using DataDomain name as event
    > "subject", you can send the events to peer caches... I think the only
    > caveat is that if remote events are disabled for DataDomain,
    > DataRowStore never registers to listen for events (even though it still
    > dispatches its own updates). So you can either
    >
    > (1) manually add DataRowStore as a listener whenever a new DataContext
    > is created:
    >
    > // there is code in "DataRowStore.processRemoteEvent" to ignore certain
    > events
    > // so this is something to tweak ... also need to make sure there is no
    > event loops
    > EventManager.getDefaultManager().addListener(
    > this,
    > "processRemoteEvent",
    > SnapshotEvent.class,
    > getSnapshotEventSubject());
    >
    >
    > ... or (2) you can register your own EventBridgeFactory in the modeler
    > and implement a "local" EventBridge, faking remote processing
    > (re-injecting the same event into the EventManager from the bridge,
    > thus channeling updates to specific caches [DataRowStore ignores all
    > events that are not coming from its own EventBridge]).
    >
    > Not sure which option is simpler. The second seems more reliable.

    Thanks!

    I went with option (2), with a new EventBridge for each DataRowStore. I
    tried to do it with a singleton, but I couldn't do it without getting event
    loops.

    Also, I'm assuming that I only care about SnapshotEvents.

    It took me hours to figure out all the details, but as usual, when all was
    said and done, the actual solution was a trivial subclass of EventBridge.
    For anyone who cares, the only lines of substance are as follows:

    =====================================================================
        protected void startupExternal() throws Exception { }

        protected void shutdownExternal() throws Exception { }

        protected void sendExternalEvent(CayenneEvent localEvent) throws
    Exception
        {
            if (localEvent.getSource() instanceof LocalEventBridge) return;

            if (localEvent instanceof SnapshotEvent)
            {
                SnapshotEvent snapshotEvent = (SnapshotEvent)localEvent;
                SnapshotEvent newSnapshotEvent = new SnapshotEvent(this,
    snapshotEvent.getPostedBy(), snapshotEvent.getModifiedDiffs(),
    snapshotEvent.getDeletedIds(), snapshotEvent.getIndirectlyModifiedIds());
                if (eventManager != null)
                {
                    eventManager.postEvent(newSnapshotEvent, localSubject);
                }
                else {
                    throw new IllegalStateException(
                        "Can't post events. EventBridge was not started
    properly. "
                            + "EventManager is null.");
                }
            }
        }
    =====================================================================



    This archive was generated by hypermail 2.0.0 : Thu Dec 02 2004 - 13:42:07 EST