Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Virgo » Spring service publishing to wrong reference arbitrarily
Spring service publishing to wrong reference arbitrarily [message #676371] Sat, 04 June 2011 00:02 Go to next message
rshelley  is currently offline rshelley Friend
Messages: 59
Registered: April 2010
I'm seeing published services being injected into fields they aren't "@Qualified" for. And it changes as I start and stop those services. I might get the Authentication bean in the Policy field, stop and restart and I might get the Policy bean in the Authentication field, etc.

I have three beans that all have the same interface, from three different bundles. Each bundle publishes their service by "id" and interface, and the "id" values are all different via Spring "<osgi:service ref='...' interface='some.common.interface' />". Each bean uses @Component("someId") to explicitly set the ID used in the ref/id of the OSGI tags. They share a common interface for specific reasons.

I then have a service that consumes those published services via three Spring "<osgi:reference id='...' interface='some.common.interface' />" tags. The service consuming these references uses @Autowried and @Qualifier annotations to specify the specific bean ID to inject into specific fields.

I thought by specifying the bean's Component ID in the reference, id, and @Qualifier fields, it'd explicitly limit the published service by the bean ID and not just go off of the interface.

I could add a marker interface, but that kind of defeats the purpose. Is the interface the only marker for the inbound service reference? I don't see any other options on the <osgi:reference> tag that allows me to restrict by the service ID.
Re: Spring service publishing to wrong reference arbitrarily [message #676454 is a reply to message #676371] Sat, 04 June 2011 14:18 Go to previous messageGo to next message
Ryan  is currently offline Ryan Friend
Messages: 17
Registered: May 2011
Junior Member
If I understand properly what you're trying to do, you want to have @Qualifier values "cross over" the OSGi boundary between Spring contexts. To my knowledge, there's nothing to directly support that. The Spring DM reference (either 1.x or 2.x) doesn't even mention qualifiers, and in my testing, I've seen no evidence that a bean's qualifier value is added to the exported service in any way, though that seems like it would be convenient. Maybe you should file a feature request on Spring DM for that?

You're seeing seemingly indeterminate service importing behavior in your <osgi:reference> tags because the only constraint you're giving it is the interface that the service needs to implement. The id attribute of osgi:reference has no role in determining which service to select. It's just the id of the bean that the service is exported as. Therefore you're asking the OSGi container for the first service it finds that satisfies the given interface, where "first" would be governed by OSGi's service selection algorithm. Specifically:
"If multiple such service objects exist, the service object with the highest SERVICE_RANKING is returned. If there is a tie in ranking, the service object with the lowest SERVICE_ID (the service object that was registered first) is returned." --OSGi Service Platform Release 4, Version 4.3, Section 5.2.8.

In the meantime, I think you could get what you want by using the bean-name attribute of osgi:reference. That restricts the imported service by the name of the Spring bean that was exported. The bean name *is* a property of the exported service, stored in the service property "". Personally, I'd try not to use it, as it's basically by-name bean wiring, where I always find by-type + qualifier to be a much better model, and OSGi itself is very by-type-oriented.

The only other option I can think of would be to add custom service properties when you export the service using the <service-properties> element of <osgi:service> and then use a filter expression on your <osgi:reference>.
Re: Spring service publishing to wrong reference arbitrarily [message #676489 is a reply to message #676454] Sat, 04 June 2011 19:48 Go to previous messageGo to next message
rshelley  is currently offline rshelley Friend
Messages: 59
Registered: April 2010
Ok, that confirms my thoughts and adds some clarity to it. Thanks!

I wasn't aware that I could provide a "filter" (for some reason it doesn't show up in my auto-completion). I can go off the bundle's symbolic name, which works fine for what I want to do. I just need to figure out what the filter would be for that.

Would it be as simple as:

<osgi:reference id="fooBar" interface="" filter="(symbolicname='')" />

Maybe specifically setting a service property on the individual service is better though, so it would identify a single service and not just a bundle (which may or may not contain multiple services, although in my case it will only have one service).

I'll fiddle with it and see how it goes. I'll have to go back to SpringDM and look more closely at some of it's nuance configurations. Thanks for your input!
Re: Spring service publishing to wrong reference arbitrarily [message #676693 is a reply to message #676371] Mon, 06 June 2011 08:10 Go to previous message
Glyn Normington is currently offline Glyn NormingtonFriend
Messages: 1222
Registered: July 2009
Senior Member
I would recommend using service properties.
Previous Topic:Cannot add Virgo Server
Next Topic:Virgo on IRC
Goto Forum:

Current Time: Fri Jul 30 11:42:16 GMT 2021

Powered by FUDForum. Page generated in 0.02637 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top