Timo,
Try the following:
ThreadListener listener = new YourListener();
ThreadEventDispatcher.getInstance().addListener(listener);
If you want a listener registration that persists across restarts you can add a jive property such as "eventListeners.ThreadListener.YourListener" -> "com.acme.your.Listener"
Javadoc on the event classes can be found here.
Regards,
Bruce Ritchie
Hey Bruce -- it's been a while, but thanks for the info!
When you say, "add a jive property", do you mean in the jiveHome/jive_startup.xml file? So the process would be to write my ThreadListener class, bundle it into a plugin jar, install the plugin, add a hook to it in the jive_start.xml file, and then restart clearspace to pick up the registration?
And the XML would literally be this?
<jive>
... other stuff...
<eventListeners>
<ThreadListener>
<TimListener>com.domain.clearspace.TimListener</TimListener>
</ThreadListener>
</eventListeners>
</jive>
Thanks,
Tim
Sorry, hit enter before digging around the API ref... found an example in the API doc for ThreadListener:
For example, to register the com.acme.CustomThreadListener class as a persistent listener the following code could be used:
// add persistent key to have the listener loaded and registered automatically at startup JiveGlobals.setJiveProperty("eventListeners.ThreadListener.customThreadListener", "com.acme.CustomThreadListener.class");
// now add it for this runtime instance ThreadEventDispatcher.addListener(new com.acme.CustomerThreadListener());
So that seems to suggest that the plugin can add properties via JiveGlobals, i.e. no need to edit XML manually?
I'll search around, but I'm assuming there is probably an method called on the plugin during "installation" and that's where the JiveGlobals should be set?
-Tim
Tim,
Yes, use the JiveGlobals.setJiveProperty() method to set the property. When you set something in the jive_startup.xml it's only valid for that node (if you're running in a cluster) and is only accessible via the api with the JiveGlobals.get/setLocalProperty() methods.
Bruce
Thanks again, Bruce.
It looks like the method initializePlugin(PluginManager manager, java.io.File pluginDirectory) will be called on once server-startup...or for 1.6 this seems to have changed from the online 1.3 javadoc to initializePlugin(PluginManager pluginManager, PluginMetaData pluginMetaData). Is that correct?
I setup a plugin.xml that points to my Plugin and added the following code to initialize (?) my eventListener. I see in catalina.out the "init", but nothing when I add/view a document given the code below. I don't see any other errors... so I'm not sure what I'm missing. Any ideas?
Thanks,
Tim
public class TimPlugin implements Plugin {
public void initializePlugin(PluginManager pluginManager, PluginMetaData pluginMetaData) {
System.out.println("Init TimPlugin!");
// Setup my listener to do fun stuff when events happen! JiveGlobals.setJiveProperty("eventListeners.DocumentListener.TimEventListener", "com.domain.clearspace.TimDocumentListener.class");
}
TimDocumentListener.java
package com.domain.clearspace;
import com.jivesoftware.community.event.DocumentEvent;
import com.jivesoftware.community.event.DocumentListener;
import static java.text.MessageFormat.format;
public class TimDocumentListener implements DocumentListener {
public TimDocumentListener() { }
public void documentAdded(DocumentEvent event) {
System.out.println(format("TimEvent: documentAdded= {0}", event.getDocument().getPlainSubject()));
}
public void documentViewed(DocumentEvent event) {
System.out.println(format("TimEvent: documentViewed= {0}", event.getDocument().getPlainSubject()));
}
public void documentDeleted(DocumentEvent event) {
}
public void documentMoved(DocumentEvent event) {
}
public void documentModified(DocumentEvent event) {
}
public void documentExpireWarning(DocumentEvent event) {
}
public void documentExpired(DocumentEvent event) {
}
public void documentRelatedObjectAdded(DocumentEvent event) {
}
public void documentRelatedObjectDeleted(DocumentEvent event) {
}
public void documentRated(DocumentEvent event) {
}
public void versionAdded(DocumentEvent event) {
}
public void versionModified(DocumentEvent event) {
}
public void versionDeleting(DocumentEvent event) {
}
public void binaryBodyDownloaded(DocumentEvent event) {
}
}
Aha...figured it out, but still have some questions.
Based on the javadoc for DocumentEventDispatcher, it says to both register the listener via JiveGlobals, and also to add it to the runtime instance.
The example is this:
// now add it for this runtime instance
DocumentEventDispatcher.addListener(new com.acme.CustomerDocumentListener());
That does not work because addListener is not static, so I simply got an instance first.
// Setup listener
DocumentEventDispatcher ded = DocumentEventDispatcher.getInstance();
ded.addListener(new TimDocumentListener());
This works! ![]()
However, I'm confused... if initializePlugin is called when the server starts up, then why bother with JiveGlobals if you have to attach the listener manually as well? Maybe I just don't fully understand when initializePlugin is called during server lifecycle.
Could you explain, please?
Thanks!
-Tim
And how do you get the current user from an event (ex. DocumentEvent) handed to the listener class?
-T
Hey Bruce -- I've searched around a bit, but have not found documentation on how to register an object to receive events that are dispatched. Is there a comprehensive list of the events that are available?
Can you point to a simple example and/or docs?
Thanks,
Timo