jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From qcfireball <qcfireb...@yahoo.com>
Subject Re: RMI Observation
Date Thu, 29 Nov 2007 21:37:36 GMT

Right now, I am just checking Observation out - to see how it works, how it
responds to JCR changes.
Here is my test class:

public class WatchAll implements javax.jcr.observation.EventListener {
    /**
     * The version of this file, which is updated each time this class file
     * is modified.
     */
    public static final String VERSION = "November 29, 2007, 12:36 PM";
    
    /** Creates a new instance of WatchAll */
    public WatchAll() {}
    
    public static void main(String[] sa) {
        log("Starting Listener...");
        
        JcrHost host = new JcrHost();
        host.setCredential("guest", "guest");
        host.setHost("localhost");
        host.setJackrabbitSpecific(true);
        host.setPort(1099);
        host.setRepositoryName("jackrabbit.repository");
        host.setWorkspace("default");
        
        Session s = null;
        try {
            s = host.createSession();
            ClientObservationManager omgr = (ClientObservationManager)
s.getWorkspace().getObservationManager();
            
            String path0 = "/";
            String path1 = "/content/Companies/TNC/en";
            omgr.addEventListener(new WatchAll(), getAllTypes(), path0,
true,  null, null, true);
            
            synchronized(WatchAll.class) {
                WatchAll.class.wait();
            }
        } catch (Exception ex) {
            Logger l = LoggerConfig.getLogger(WatchAll.class);
            l.log(Level.SEVERE, "ERROR:", ex);
        } finally {
            if (s!= null) s.logout();
        }
    }
    
    private static int getAllTypes() {
        return Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED
|
                Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED;
    }
    
    public void onEvent(EventIterator eventIterator) {
        log("Processing Event");
        
        for ( ; eventIterator.hasNext() ; ) {
            Event event = eventIterator.nextEvent();
            switch (event.getType()) {
                case Event.NODE_ADDED : nodeAdded(event); break;
                case Event.NODE_REMOVED : nodeRemoved(event); break;
                case Event.PROPERTY_ADDED : propertyAdded(event); break;
                case Event.PROPERTY_CHANGED : propertyChanged(event); break;
                case Event.PROPERTY_REMOVED : propertyRemoved(event); break;
                default : break;
            }
        }
    }
    
    private void nodeAdded(Event event) {
        StringBuilder sb = new StringBuilder(256);
        sb.append("Node Added: ");
        showEvent(event, sb);
    }
    
    private void nodeRemoved(Event event) {
        StringBuilder sb = new StringBuilder(256);
        sb.append("Node Removed: ");
        showEvent(event, sb);
    }
    
    private void propertyAdded(Event event) {
        StringBuilder sb = new StringBuilder(256);
        sb.append("Property Added: ");
        showEvent(event, sb);
    }
    
    private void propertyChanged(Event event) {
        StringBuilder sb = new StringBuilder(256);
        sb.append("Property Changed: ");
        showEvent(event, sb);
    }
    
    private void propertyRemoved(Event event) {
        StringBuilder sb = new StringBuilder(256);
        sb.append("Property Removed: ");
        showEvent(event, sb);
    }
    
    private void showEvent(Event event, StringBuilder sb) {
        try {
            sb.append("Path = [").append(event.getPath()).append("] - ");
            sb.append("User = [").append(event.getUserID()).append("]\n");
            log(sb);
        } catch (RepositoryException ex) {
            Logger l = LoggerConfig.getLogger(getClass());
            l.log(Level.WARNING, "ERROR:", ex);
        }
    }
    
    private static void log(Object o) {
        if (o != null) {
            Logger l = LoggerConfig.getLogger(WatchAll.class);
            l.finest(o.toString());
        }
    }
}


Felix Meschberger-2 wrote:
> 
> Hi,
> 
> Not sure, what you are trying to accomplish. From your description it
> seems something like this:
> 
>    class MyThread extends Thread {
>       public void run() {
>           // register listener
>       }
>    }
> 
> If this is so, it is expected behaviour :-) You register the listener
> and then the events are dispatched to the onEvent method is called
> asynchronously. What you would want to do is something like this:
> 
>     class MyListener implements EventListener {
>         void start() {
>             // register listener
>         }
>         void stop() {
>             // unregister listener
>         }
>         public void onEvent(EventIterator events) {
>             //...
>         }
>     }
> 
> Regards
> Felix
> 
> Am Donnerstag, den 29.11.2007, 11:22 -0800 schrieb qcfireball:
>> I noticed that when registering a new Observer via RMI, if I do not
>> provide a
>> "thread trick" the Thread immediately dies, and the Observer with it.
>> 
>> Basically I am synchronizing on an arbitrary Object, then calling 'wait'. 
>> Not a very nice solution, but it works for now.  Seems like a hack to me.
>> 
>> I guess it makes sense that the Thread would immediately die without some
>> kind of effert.  I am wondering how other people are making sure that
>> their
>> Observer does not die?  Are there ways that are preferred over others?
>> 
>> Is this behavior going to be the same when registering an Observer within
>> a
>> web container, directly using the Jackrabbit API's?
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/RMI-Observation-tf4899371.html#a14035576
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.


Mime
View raw message