On Saturday 31 July 2004 03:32, David Leangen wrote:
> Hello!
>
> I have created a component, as explained in the tutorials. Let's call it
> "Component". Component has two services, ServiceA and ServiceB. Both of
> these services take a while to complete, and should be executed in
> sequence, since ServiceB depends on the completion of ServiceA.
>
> I declare my services like this:

You need to get your capitalization right !!! :o)


> 1. When I was using only ServiceA, I only had to make a call to
>    getStuff() in initialize(). At that time, I didn't really
>    stop to think about the mechanism. I would like to know
>    what actually fires off the service.

Not sure what you are asking here.
Merlin will go through the life cycle methods of all components with the 
activation set to 'startup', PLUS all the components that those depends on. 
For each component that are 'continuous' in the running, they need to create 
a thread (typically in initialize() ) and start the thread (typically in 
start() ).

> 2. How can I get Component to wait for the completion of
>    ServiceA so I can fire ServiceB?

You should most probably declare a dependendency. If the ServiceB instance 
depends on ServiceA, then you are guaranteed that they are made available in 
correct order.

> I tried making a direct call to serviceA.start(), but that didn't work. I
> don't think that I should be making my service providers Runnable.

Agreed. The service shouldn't be made a sub-interface of Runnable. You can 
make the component implement Runnable, and create a thread with the component 
instance as the Runnable.

> So, there's something I'm not getting here...

You will probably need something simple like this;

/**
 * @avalon.component name="Comp1" 
 *                   lifestyle="singleton"
 * @avalon.service type="org.hedhman.niclas.demo.ServiceA"
 */
public class Comp1
    implements Initializable, Startable, ServiceA, 
               Runnable, ServiceB
{
    private Thread m_Thread;
    private Stuff  m_Stuff;

    public void initialize()
    {
        m_Stuff = ...  // initialized through conf or params?

        m_Thread = new Thread( this );
        // potentially more code, long time to complete
        // if too long, the deployment timeout needs to 
        // be extended
    }.

    public void start()
    {
        m_Thread.start();
    }

    public void run()
    {
        // continuously running code
    }

    public Stuff getStuff()
    {
        return m_Stuff;
    }
}


/**
 * @avalon.component name="Comp2" 
 *                   lifestyle="singleton"
 * @avalon.service type="org.hedhman.niclas.demo.ServiceB"
 */
public class Comp2
    implements Serviceable, Initializable, ServiceB
{
    private ServiceA m_ServiceA;

    /** 
     * @avalon.dependency type="org.hedhman.niclas.demo.ServiceA"
     *                    key="serviceA"
     */
    public void service( ServiceManager man )
    {
        m_ServiceA = (ServiceA) man.lookup( "serviceA" );
    }

    public void initialize()
    {
        // At this point, Merlin will guarantee that the 
        // Comp1 has been initialized and there is Stuff that
        // can be retrieved.
        Stuff stuff = m_ServiceA.getStuff();
    }
}


I think this will help you on your way.


Niclas
-- 
   +------//-------------------+
  / http://www.bali.ac        /
 / http://niclas.hedhman.org / 
+------//-------------------+


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to