Expand my Community achievements bar.

SOLVED

Sling Component versus Service

Avatar

Level 9

Hi All,

I want to know difference between Sling Component and Service.What are the use cases to be considered to use and implement these two features.

Please help me in understanding both of these.

Thanks,

Kishore

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi

I have found a very good article, covering everything with example:

Link:- http://www.computepatterns.com/76/osgi-component-vs-service-in-aem/

AEM ships with an OSGi container Apache felix that implements Declarative Services (DS) component model. Most of the developers who are new to AEM often gets confused between OSGi components and services.

OSGi Component
If you want the life of your object to be managed by the OSGi container, you should declare it as a component. Using DS annotations, you could make a POJO a OSGi component by annotating it with@Component With this, you will get the ability to start, stop and configure the component using the felix web console.

OSGi Service
OSGi components can be made as OSGi service by marking it with @service annotation. All it mandates is that an interface Services should implement an interface (1 or more). When you mark a component as service, you could refer (call) this service from other osgi components.

OSGi Component Vs Service

  • All objects managed by OSGi container are components. You qualify components as services. This means that all services are components but not vice-versa.
  • Components can refer/call (using container injection  @reference) other services but not components. In other words, a component cannot be injected into another component / service. Only services can be injected into another component.

Go through the examples....

and in the last, 

OSGi Component Use cases
Now, you must be wondering, why you shouldnt be marking every component as service. Yes, you want your objects to be usable outside its body. However, in certain use-cases modeling your object as component (not marking it as service) makes sense. Here are few of them.

  • A server object in your application which listens to a socket.
  • A filter object which intercepts the requests.
  • An object which monitors a resource and report.
  • An objects which pulls data from external system and writes to the repository.

I hope this would be of some help.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

View solution in original post

3 Replies

Avatar

Level 10

Hi Kishore,

In addition with that link given by scott, look into  this thread which answers your question.

http://stackoverflow.com/questions/8886430/what-is-the-difference-between-osgi-components-and-servic...

Also, there are so many number of articles which creates custom OSGI bundle(contains components and services) in this link: https://helpx.adobe.com/marketing-cloud/experience-manager.html

Thanks,
Ratna Kumar.

Avatar

Correct answer by
Administrator

Hi

I have found a very good article, covering everything with example:

Link:- http://www.computepatterns.com/76/osgi-component-vs-service-in-aem/

AEM ships with an OSGi container Apache felix that implements Declarative Services (DS) component model. Most of the developers who are new to AEM often gets confused between OSGi components and services.

OSGi Component
If you want the life of your object to be managed by the OSGi container, you should declare it as a component. Using DS annotations, you could make a POJO a OSGi component by annotating it with@Component With this, you will get the ability to start, stop and configure the component using the felix web console.

OSGi Service
OSGi components can be made as OSGi service by marking it with @service annotation. All it mandates is that an interface Services should implement an interface (1 or more). When you mark a component as service, you could refer (call) this service from other osgi components.

OSGi Component Vs Service

  • All objects managed by OSGi container are components. You qualify components as services. This means that all services are components but not vice-versa.
  • Components can refer/call (using container injection  @reference) other services but not components. In other words, a component cannot be injected into another component / service. Only services can be injected into another component.

Go through the examples....

and in the last, 

OSGi Component Use cases
Now, you must be wondering, why you shouldnt be marking every component as service. Yes, you want your objects to be usable outside its body. However, in certain use-cases modeling your object as component (not marking it as service) makes sense. Here are few of them.

  • A server object in your application which listens to a socket.
  • A filter object which intercepts the requests.
  • An object which monitors a resource and report.
  • An objects which pulls data from external system and writes to the repository.

I hope this would be of some help.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni