Why use @Component annotation with each service in CQ
Asked Answered
O

2

10

I am bit confused about following things. I understand @Service and @Component annotations are main annotations when we define a component or a service in OSGi. I am referring to http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html and What is the difference between OSGi Components and Services

Questions:

  1. A service can not be created without @Component annotation, why is that?

  2. I understand once we define a service its life-cycle is managed by OSGi differently but what are the advantages of doing so?

  3. How do we use class defined as @Component as service can be accessed via sling.getService(ServiceName.class)

Olmsted answered 23/1, 2015 at 7:11 Comment(0)
D
9
  1. A service can be published without a @Component annotation, but you have to do it programmatically. If you use the annotation then you benefit from the automatic metadata generation in the build tool, and also from the Declarative Services runtime framework. This simplifies a lot of things. If you want to do it with low-level code you have to write an implementation of BundleActivator, declare that with the Bundle-Activator manifest header, call context.registerService etc. Bottom line: just use the @Component annotation!

  2. Simple: laziness. When a component is a service then it can be instantiated lazily "on-demand", i.e. only when consumer first tries to use the service. Non-service components, on the other hand, usually do other kinds of things inside themselves, e.g. running a web server or a GUI or a polling thread, whatever. These need to be running all the time, rather than on-demand.

3. I didn't understand this question.

  1. A component that is not published as a service cannot be accessed from outside the bundle. If you want it to be accessible then it has to be a service. In case you think this is useless, consider a component that creates an HTTP server. It opens port 80 and responds to network requests from the outside world. So it does something useful even though it's not a service and not accessible from other bundles. This kind of component is like a bridge between your application and the outside world; whereas services are a bridge between one part of your application and another part.
Dierdre answered 28/1, 2015 at 13:20 Comment(3)
Point 1 and 2 works. 3rd question was around how do we use a class which is only defined with @Component annotation. For example: a class which is service can be accessed via sling.getService(ServiceName.class)Olmsted
Classes that are @Component but don't publish as a service cannot be accessed from outside the bundle. That's the point... if you want to be called from the outside, then be a service.Dierdre
beautiful explanationPaulie
O
2
  • OSGi is the one where bundles are installed and manages. Everything that needs to be there in OSGi has to be a component be it simple component, a service or servlet. That is why we need to use @Component with service also.

  • Services are singleton. Everything that needs to be managed for Singleton class and using reference of service is done by OSGi. Nothing has to be done from our side. So everything is automatically managed.

  • You dont access components like that. Components are independently used. Quoting example from different post: Suppose you want to a write Server component that sits on socket and responds to requests over TCP/IP. When the component starts, it opens the socket and creates the thread(s) required to serve clients. When it stops, it closes the thread(s) and socket

Ogpu answered 23/1, 2015 at 15:49 Comment(2)
partially answers my questionsOlmsted
I'm not sure I buy most of this answer.Stillas

© 2022 - 2024 — McMap. All rights reserved.