You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by qcfireball <qc...@yahoo.com> on 2007/11/29 20:22:27 UTC
RMI Observation
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#a14033021
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
Re: RMI Observation
Posted by Felix Meschberger <fm...@gmail.com>.
Hi,
Yes, this is quite clear, that you will never receive any events. When
the main method terminates, the VM is terminated unless there are any
non-daemon threads running. As there are daemon threads running after
you registered for the listener, the VM terminates after leaving the
main method.
Regards
Felix
Am Donnerstag, den 29.11.2007, 13:37 -0800 schrieb qcfireball:
> 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?
> >
> >
> >
>
Re: RMI Observation
Posted by qcfireball <qc...@yahoo.com>.
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.
Re: RMI Observation
Posted by Felix Meschberger <fm...@gmail.com>.
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?