Also, my understanding was that in a tapestry app (and in any webapp using the hivemind servlet filter), the registry is created per application, and the registry startup/shutdown would correspond with the application startup/shutdown. Your comment makes me question that. Is there a case when the hivemind registry startup/shutdown would not be the equivalent of the webapp's startup/shutdown?
I guess it would make sense if the startup/shutdown logic really needs to happen when the HiveMind registry is started/shutdown. However, in a web application, it might be better to use a ServletContextListener if you're looking for *application* startup/shutdown hooks. Just a thought. You might also want to mark your service as private, but since the service interface is empty, there's probably no need to. An outside user can't really do anything with it anyway if it has no methods.
From: Spencer Crissman [mailto:[EMAIL PROTECTED]]
Sent: Monday, November 07, 2005 2:23 PM
To: [email protected]
Subject: Re: Multiple Instances of Singleton Constructed?
I'm using 1.1. That's a good pointer about the automatic registration.
I need the setup and shutdown code to occur once and only once per startup/shutdown. When I supplied the class as the interface to the service, the code was getting called twice, once when the proxy object was created, and once when the actual object was instanciated, based upon what you had said in your email. So, in order to do away with the call that was being performed by the proxy class, I created the empty interface, implemented it in the class, and supplied the interface as the service interface. Since the proxy class wasn't generated, the call occurs only once. In a real app, the interface probably wouldn't be empty(?).
It seems to be working as far as I can tell, but does the reasoning make sense? I want some code to be called once and only once on startup, and some additional code called once and only once on shutdown. I don't know what the best practice would be using hivemind, but this seems reasonable.
This is just some test code to try to get my mind around hivemind prior to putting it to use in my next tapestry app.
On 11/7/05, James Carman < [EMAIL PROTECTED]> wrote:
No problem! What version of HiveMind are you using? If you're using 1.1, you don't have to manually register your implementation object as a shutdown listener They will be registered for you automatically (http://issues.apache.org/jira/browse/HIVEMIND-103 ) if the implementation class implements RegistryShutdownListener.
Also, what purpose would an empty service interface serve? Are you just trying to perform some operations upon registry shutdown? I'm just trying to figure out what a use case would be for that scenario. Of course, you could just be playing around with HiveMind's features too! :-) Anyway, glad we could help. Enjoy!
From: Spencer Crissman [mailto:[EMAIL PROTECTED]]
Sent: Monday, November 07, 2005 9:55 AM
To: [email protected]
Subject: Re: Multiple Instances of Singleton Constructed?
Thank you! This was very helpful. Creating an empty interface ICleaner and having Cleaner implement it resulting in the expected behavior (single constructed and shutdown message).
On 11/7/05, James Carman < [EMAIL PROTECTED]> wrote:
This happens because the actual proxy object is an instance of a subclass of Cleaner. You are not proxying via an interface, so HiveMind is generating a subclass to do the proxying.
From: Spencer Crissman [mailto:[EMAIL PROTECTED]]
Sent: Monday, November 07, 2005 8:54 AM
To: [email protected]
Subject: Multiple Instances of Singleton Constructed?
I have a Tapestry application in which I am attempting to use hivemind for some startup and shutdown handling. Basically starting some services when loaded, and then cleaning up after them when the application is undeployed or shutdown.
As a simple test, I have added the following to my hivemodule.xml:
<service-point id="Cleaner" interface="Cleaner">
<invoke-factory model="singleton">
<construct class="Cleaner">
<event-listener service-id="hivemind.ShutdownCoordinator"/>
</construct>
</invoke-factory>
</service-point>
<contribution configuration-id="hivemind.EagerLoad">
<load service-id="Cleaner"/>
</contribution>
And the following class to my project:
public class Cleaner implements RegistryShutdownListener {
public static int c;
public Cleaner( ) {
c = c + 1;
System.out.println("Cleaner constructed! (" + String.valueOf(c) + ")");
}
public void registryDidShutdown() {
System.out.println("The registry has shutdown! (" + String.valueOf(c) + ")" );
c = c - 1;
}
}
Oddly, when I go through the process of starting up and shutting down, I see the following:
Cleaner constructed! (1)
Cleaner constructed! (2)
The registry has shutdown! (2)
The registry has shutdown! (1)
It appears that two instances of the Cleaner class are being constructed, when what I really want is a singleton that gets created one time on startup, and then gets called a single time at shutdown. From the messages it appears that the problem is with the startup portion of the code, because if two instances were not created, then there would be only one instance registered for the shutdown event.
Where is hivemind being instructed to construct the second instance, and how could I prevent it from happening?
Thanks in Advance,
Spencer
