You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Leszek Gawron <ou...@wlkp.org> on 2003/11/04 10:35:37 UTC

Singleton question

I know this question should be addressed to Avalon group but I am only cocoon
subscriber and feel much safer with you guys :)

How should I write an avalon component that is a pure singleton? The case is I
need a component that has only ONE instance and the access is strictly
synchronized. The component cannot be pooled because AFAIU the pool can grow
over it's maximum size set in cocoon.xconf. 

Normally I would implement a singleton with synchronized methods but I would
like to make it cocoon exercise and make this a component with full lifecycle.

regards
	lg
-- 
            __
         | /  \ |        Leszek Gawron            //  \\
        \_\\  //_/       ouzo@wlkp.org           _\\()//_
         .'/()\'.     Phone: +48(501)720812     / //  \\ \
          \\  //  recursive: adj; see recursive  | \__/ |


Re: Singleton question

Posted by Torsten Curdt <tc...@vafer.org>.
Leszek Gawron wrote:
> I know this question should be addressed to Avalon group but I am only cocoon
> subscriber and feel much safer with you guys :)
> 
> How should I write an avalon component that is a pure singleton? The case is I
> need a component that has only ONE instance and the access is strictly
> synchronized. The component cannot be pooled because AFAIU the pool can grow
> over it's maximum size set in cocoon.xconf. 
> 
> Normally I would implement a singleton with synchronized methods but I would
> like to make it cocoon exercise and make this a component with full lifecycle.

Implement the lifecycle interface ThreadSafe.
The CM will always return the same instance
--
Torsten


Re: Singleton question

Posted by Berin Loritsch <bl...@apache.org>.
Leszek Gawron wrote:

> I know this question should be addressed to Avalon group but I am only cocoon
> subscriber and feel much safer with you guys :)
> 
> How should I write an avalon component that is a pure singleton? The case is I
> need a component that has only ONE instance and the access is strictly
> synchronized. The component cannot be pooled because AFAIU the pool can grow
> over it's maximum size set in cocoon.xconf. 

In Cocoon 2.1 and prior you would have your component implement the ThreadSafe
interface.  In Cocoon 2.2 you would add the @x-avalon.lifstyle type="singleton"
meta tag to the JavaDoc for the class.


> 
> Normally I would implement a singleton with synchronized methods but I would
> like to make it cocoon exercise and make this a component with full lifecycle.

You can still use a "strict" singleton if you don't need access to other
components, but in servlet environments heavy use of synchronization can limit
the scalability of a system.  Of course the opposite is also true: insufficient
synchronization leads to an unstable system.  It is hard to find the right
balance, or to limit the points where you *need* synchronization.

-- 

"They that give up essential liberty to obtain a little temporary safety
  deserve neither liberty nor safety."
                 - Benjamin Franklin