hivemind-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Spencer Crissman <spencer.criss...@gmail.com>
Subject Re: Multiple Instances of Singleton Constructed?
Date Mon, 07 Nov 2005 19:22:50 GMT
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 <james@carmanconsulting.com> 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:spencer.crissman@gmail.com]
> *Sent:* Monday, November 07, 2005 9:55 AM
> *To:* hivemind-user@jakarta.apache.org
> *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* <james@carmanconsulting.com> 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:spencer.crissman@gmail.com]
> *Sent:* Monday, November 07, 2005 8:54 AM
> *To:* hivemind-user@jakarta.apache.org
> *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
>
>

Mime
View raw message