> Are there rules that should be followed for DS components' constructors
> and @Activate methods?
> 
> I.e. stuff like
> 1. Not run for a too long time (e.g. not block, waiting for an HTTP response)

Basic answer:
Yes, you should avoid blocking in callbacks generally. The thread is not your 
own and by blocking you could halt the entire Service Component Runtime, 
potentially halting (or even deadlocking) the system. If you need to do 
something long-running then create your own thread to do it.

Advanced answer:
If you have a long-running asynchronous startup then the default “lazy” 
behaviour of DS can be problematic. Your component will be registered as a 
service before it is activated, then activated when it is first retrieved. This 
activation will complete when your @Activate method exits, even though your 
asynchronous startup is still running. You will therefore either need to block 
incoming calls until the asynchronous startup finishes (including dealing with 
failures), or throw exceptions until you are ready (yuck!). 

A better option in these cases can be to *not* let DS register your component 
as a service, but instead to programatically register your service object after 
it has completed activation. In this case writing a DS component that is a 
lifecycle manager and collector of dependencies, but not the actual service 
object, can be very helpful.

> 2. Not fail (i.e. never throw an exception)

You should definitely fail if the component is broken. Throwing an exception 
from a constructor or @Activate method will cause the component to be 
unregistered from the service registry and marked as failed in the service 
component runtime. You should not “silently fail” and leave a broken service 
object.

> 3. Other?

Make sure to correctly define the optionality and cardinality of your 
dependencies, including dependencies that you have on configuration. Only let 
the container start your component when it is actually able to do its job.

I hope this helps,

Tim

> On 30 Aug 2020, at 10:50, Steinar Bang <[email protected]> wrote:
> 
>>>>>> Jean-Baptiste Onofre <[email protected]>:
> 
>> Hi,
>> I’m not sure I understand your question.
> 
> I was just asking about general guidelines to follow for DS
> components. I.e. things that should be avoided?
> 
> It's peripherally releated to the other question, but only
> peripherally.  
> 
> The thought struck me while pondering the issue of the other thread, so
> I tried to google for guidelines for DS components.  But didn't find any
> good answers, so I figured I might as well ask.
> 

Reply via email to