You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Thomas Driessen <th...@ds-lab.org> on 2017/11/17 09:02:43 UTC

Re[2]: Declarative Services problems in Apache Karaf

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: 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