0

i am new to CQ5, when i am going through the OSGi concepts, i found some thing called services. I created a bundle which has an interface and implementation class for that, which overrides the method (simply returns a string "hello world").If i want to utilize that class in jsp/some java class i will create a new instance of that object and use its methods.So my question is, what is the exact difference between creating a new object and utilizing service. And please mention the advantages of services over a new object.And help with configuring services using spring-DM.

user2784095
  • 157
  • 1
  • 2
  • 12
  • Your question is about the same as this one: http://stackoverflow.com/questions/20323063/why-should-one-create-use-services-in-osgi/20323288#20323288. For the Spring-DM question, have a look at the Spring-DM site, but it is probably not the best way to go because it is not supported anymore. – Arie van Wijngaarden Feb 23 '14 at 14:24

2 Answers2

2

I would say that the major difference about components and services is that their life cycle is managed by OSGi. That means that their state is independent from the class using the component.

Your OSGi service has a state, it can be used to store information or respond to messages and they will live past the scope of the class that uses them. They also allow you to separate the interface from the actual implementation. You could easily swap an implementation of certain service in a running instance (IE, upgrade a provider from v1 to v1.0.1 ). Also, you might want to temporarily stop a component without shutting down your applications. The service will be wired again when it is activated.

The basic use case for each one of them is about their functionality. If a class is just a bean with some getters and setters, you will probably instantiate it, fill it and use it whenever you need it. On the other hand, if a class provide some kind of functionality (processing, storing, queueing, etc). it probably should be a service. Classes with a lot of static methods (helpers, managers), can normally be refactored as services very easily.

Not sure about spring-dm. But in Adobe CQ5 you normally use the Felix annotations for Declarative Services

santiagozky
  • 2,519
  • 22
  • 31
  • hi santiagozky, Thanks for your reply. Can you explain this(service vs new object instance)by use-case and example or provide the link where i can get information regarding this. – user2784095 Feb 24 '14 at 08:17
0

In addition to the great answer by @santiagozky, I will add that the other major difference is dependency management.

When you instantiate an object, you inherit all of its dependencies. You are forever bound to changes in the implementation that can change those dependencies. When utilizing a service, you are following a Java best practice of coding to an interface. Your code is implementation agnostic and is only dependent on the interfaces dependencies, which will always be equal or less than the implementation. Typically, a much smaller set of dependencies. Modifying an existing implementation or changing the implementation altogether has no impact on your code.

Robin
  • 24,062
  • 5
  • 49
  • 58