Confusion around ej.service.ServiceFactory

Good morning,

I am looking through the ServiceFactory.class as it has been the source of some confusion on our project lately.

What is the difference between the ServiceRegistry and the ServiceLoader? When retrieving a SharedInterface implementation, is it ok to just use:

ServiceFactory.getService()

or should I use one of the following:

ServiceFactory.getServiceLoader().getService()
ServiceFactory.getServiceRegistry().getService()

It appears ServiceFactory.getService() looks in the ServiceLoader first and, if it doesn’t find anything, goes to the ServiceRegistry so surely it must be the best one to call?

Finally, when is it advisable to use:

ServiceFactory.getRequiredService()

Is the only difference between this and getService() that getRequiredService() throws a MissingServiceException if the service is not found?

If I am correct, it appears we should be using ServiceFactory.getRequiredService() for any 'build-in functionality (services provided by standalone apps) such as the EventManager, ServiceManager, etc… and that we should simply use ServiceFactory.getService() when retrieving a shared interface implementation. Is this correct? Or should I be specifying the ServiceLoader / ServiceRegistry ?

Regards,

Steve

1 Like

Hi @ssampson,

ServiceFactory.getService() uses both of the service loaders you showed after, so you should use this one.

Is the only difference between this and getService() that getRequiredService() throws a MissingServiceException if the service is not found?

Yes.

If I am correct, it appears we should be using ServiceFactory.getRequiredService() for any 'build-in functionality (services provided by standalone apps) such as the EventManager, ServiceManager, etc… and that we should simply use ServiceFactory.getService() when retrieving a shared interface implementation. Is this correct?

Exactly.

Regards,