You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by MarcoF90 <ma...@gmail.com> on 2017/11/16 16:06:36 UTC
Declarative Services problems in Apache Karaf
Hi,
I am using Declarative Services in Apache Karaf 4.1.3. I notice that in all
@Component using @Reference with policy DYNAMIC on attributes cause
Component deactivate on service changes. This is an example:
@Component(immediate = true)
public class Test3 {
private static final Logger logger = LoggerFactory.getLogger(Test3.class);
private GreetService greetService;
@Activate
public void activate(BundleContext bundleContext) {
logger.info("activate() - IN - input: bundleContext = [{}]",
bundleContext);
logger.info("activate() - OUT");
}
@Deactivate
public void deactivate() {
logger.info("deactivate() - IN");
logger.info("deactivate() - OUT");
}
// ----------------------------- greet service
--------------------------------- //
@Reference(policy = ReferencePolicy.DYNAMIC, policyOption =
ReferencePolicyOption.GREEDY)
public void setGreetService(GreetService greetService) {
logger.info("setGreetService() - NOTIFY - msg: greetService = [{}]",
greetService);
this.greetService = greetService;
}
public void unsetGreetService(GreetService greetService) {
logger.info("unsetGreetService() - NOTIFY - msg: greetService = [{}]",
greetService);
}
public GreetService getGreetService() {
return greetService;
}
}
When I deactivate GreetService I'd like to unset it in method
unsetGreetService() without DISABLE the entire component calling
deactivate() method. Is this possible? I am able to obtain this behaviour
only with @Reference properties DYNAMIC and MULTIPLE. Thanks for help.
Marco
--
Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-f4833200.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Declarative Services problems in Apache Karaf
Posted by MarcoF90 <ma...@gmail.com>.
Hi Ray,
thanks for your contribution. Now I finally understood all the aspects
thanks to everyone's help and deepening on the specifications. For me the
post is RESOLVED, but if someone wants to contribute well yet.
Kind regards.
Marco
--
Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-f4833200.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Declarative Services problems in Apache Karaf
Posted by Raymond Auge <ra...@liferay.com>.
On Nov 17, 2017 7:04 AM, "Raymond Auge" <ra...@liferay.com> wrote:
Hey Marco,
Now that your reference will be properly dynamic, multiple, I hope that
private B b;
as a non-volatile field is just an over simplification in your example. DS
concurrently calls
.. dynamic ..
bind methods on your component so setting of the non-volatile private field
will be dangerous. You'll want to ensure that you handle that, such as
making b volatile and making method local copies of b before using it. You
could also use an AtomicReference<B> instead.
There are also several other non-obvious concerns if you're gonna manage
your own "collection" of services. So take care with that.
Sincerely,
- Ray
On Nov 17, 2017 5:22 AM, "MarcoF90" <ma...@gmail.com> wrote:
Hi Neil,
thank you for your answer, this further difference is clear to me.
Kind regards,
Marco
--
Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-f
4833200.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Declarative Services problems in Apache Karaf
Posted by Raymond Auge <ra...@liferay.com>.
Hey Marco,
Now that your reference will be properly dynamic, multiple, I hope that
private B b;
as a non-volatile field is just an over simplification in your example. DS
concurrently calls bind methods on your component so setting of the
non-volatile private field will be dangerous. You'll want to ensure that
you handle that, such as making b volatile and making method local copies
of b before using it. You could also use an AtomicReference<B> instead.
There are also several other non-obvious concerns if you're gonna manage
your own "collection" of services. So take care with that.
Sincerely,
- Ray
On Nov 17, 2017 5:22 AM, "MarcoF90" <ma...@gmail.com> wrote:
Hi Neil,
thank you for your answer, this further difference is clear to me.
Kind regards,
Marco
--
Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-
f4833200.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Declarative Services problems in Apache Karaf
Posted by MarcoF90 <ma...@gmail.com>.
Hi Neil,
thank you for your answer, this further difference is clear to me.
Kind regards,
Marco
--
Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-f4833200.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Declarative Services problems in Apache Karaf
Posted by Neil Bartlett <nj...@gmail.com>.
There is a difference but it is subtle. If you are bound to a service that
goes away AND there already exists an alternative service that your
component could bind to, then a Dynamic Reference will re-bind to that
alternative service. This is called "Dynamic Service Replacement" in the
spec.
With a Static Reference in this scenario, your component instance has to be
destroyed, but a new instance will immediately be created that binds to the
alternative service.
Regards,
Neil
On Fri, Nov 17, 2017 at 8:35 AM, MarcoF90 <ma...@gmail.com>
wrote:
> Hi Thomas,
>
> thanks for reply. So for mandatory references there is no difference
> between
> STATIC, DYNAMIC or GREEDY and RELUCTANT properties? Each time the component
> is deactivated?
>
> Kind regards,
> Marco
>
>
>
> --
> Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-
> f4833200.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>
Re: Re[2]: Declarative Services problems in Apache Karaf
Posted by MarcoF90 <ma...@gmail.com>.
Hi Thomas,
thank you for the example. I will certainly go into the specifications in
more detail.
What I need is a component that collects all the services that will then be
used in the bundle.
When all services are available, the bundle starts all bootstrap operations
and starts processing logic.
If a service (of another bundle) updates itself,
I would like this service collector to update its reference without
reactivating.
If I have understood well the combination of properties that allows me to
obtain
this is: policy = DYNAMIC, policyOption = GREEDY, cardinality = MULTIPLE and
@Component (immediate = true).
EXAMPLE:
@Component
public class AImpl implements A {}
@Component
public class BImpl implements B {}
@Component(immediate = true)
public class ServicesCollector {
private A a;
private B b;
private Bootstrap boostrap;
@Activate
public void activate(BundleContext bundleContext) {
boostrap.start();
}
@Deactivate
public void deactivate() {
boostrap.stop();
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY)
public void setA(A a) {this.a = a}
public void unsetA(A a) {}
@Reference(cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY)
public void setB(B b) {this.b = b}
public void unsetB(B a) {}
}
Are there no other options right? thanks for your helps.
Kind regards,
Marco
--
Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-f4833200.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re[2]: Declarative Services problems in Apache Karaf
Posted by Thomas Driessen <th...@ds-lab.org>.
Hi Marco,
this depends on how many services are in your system that can be bound
to your reference.
Given the following setup:
@Component
public class AImpl1 {
@Reference
private B b;
}
@Component
public class BImpl1 implements B {}
then A gets activated after B has been activated and injected to A. As
soon as B is deactivated also A will be deactivated as the reference is
per default mandatory and your service A cannot exist without a service
B. In this ONE case it does not matter what you defined for policy and
policyOption.
This behavior changes as soon as you define your reference to be
optional via @Reference(cardinality=ReferenceCardinality.OPTIONAL). Now
A can start without B and if the reference is defined to be dynamic
activated B services will be injected at runtime as you expect.
For the different combinations of policy and policyOption and their
behaviors you should have a look at the Compendium specification at page
303:
https://osgi.org/download/r6/osgi.cmpn-6.0.0.pdf
Kind regards,
Thomas
------ Originalnachricht ------
Von: "MarcoF90" <ma...@gmail.com>
An: users@felix.apache.org
Gesendet: 17.11.2017 09:35:07
Betreff: Re: Declarative Services problems in Apache Karaf
>Hi Thomas,
>
>thanks for reply. So for mandatory references there is no difference
>between
>STATIC, DYNAMIC or GREEDY and RELUCTANT properties? Each time the
>component
>is deactivated?
>
>Kind regards,
>Marco
>
>
>
>--
>Sent from:
>http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-f4833200.html
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>For additional commands, e-mail: users-help@felix.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Declarative Services problems in Apache Karaf
Posted by MarcoF90 <ma...@gmail.com>.
Hi Thomas,
thanks for reply. So for mandatory references there is no difference between
STATIC, DYNAMIC or GREEDY and RELUCTANT properties? Each time the component
is deactivated?
Kind regards,
Marco
--
Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-f4833200.html
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: Declarative Services problems in Apache Karaf
Posted by Thomas Driessen <th...@ds-lab.org>.
Hi Marco,
I think your reference is mandatory by default. This means your service is
deactivated as soon as the need reference vanishes.
Try to make your service optional.
Kind regards,
Thomas
2017-11-16 17:06 GMT+01:00 MarcoF90 <ma...@gmail.com>:
> Hi,
>
> I am using Declarative Services in Apache Karaf 4.1.3. I notice that in all
> @Component using @Reference with policy DYNAMIC on attributes cause
> Component deactivate on service changes. This is an example:
>
> @Component(immediate = true)
> public class Test3 {
>
> private static final Logger logger = LoggerFactory.getLogger(Test3.
> class);
>
> private GreetService greetService;
>
> @Activate
> public void activate(BundleContext bundleContext) {
> logger.info("activate() - IN - input: bundleContext =
> [{}]",
> bundleContext);
> logger.info("activate() - OUT");
> }
>
> @Deactivate
> public void deactivate() {
> logger.info("deactivate() - IN");
> logger.info("deactivate() - OUT");
> }
>
> // ----------------------------- greet service
> --------------------------------- //
> @Reference(policy = ReferencePolicy.DYNAMIC, policyOption =
> ReferencePolicyOption.GREEDY)
> public void setGreetService(GreetService greetService) {
> logger.info("setGreetService() - NOTIFY - msg:
> greetService = [{}]",
> greetService);
> this.greetService = greetService;
> }
>
> public void unsetGreetService(GreetService greetService) {
> logger.info("unsetGreetService() - NOTIFY - msg:
> greetService = [{}]",
> greetService);
> }
>
> public GreetService getGreetService() {
> return greetService;
> }
>
> }
>
> When I deactivate GreetService I'd like to unset it in method
> unsetGreetService() without DISABLE the entire component calling
> deactivate() method. Is this possible? I am able to obtain this behaviour
> only with @Reference properties DYNAMIC and MULTIPLE. Thanks for help.
>
> Marco
>
>
>
> --
> Sent from: http://apache-felix.18485.x6.nabble.com/Apache-Felix-Users-
> f4833200.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>
--
M.Sc. Thomas Driessen
Software Methodologies for Distributed Systems
Institute of Computer Science
University of Augsburg
Universitätsstr. 6a
86135 Augsburg, Germany
Tel: +49 821 598-2486
email: thomas.driessen@informatik.uni-augsburg.de