2

I would like to export spring bean from one bundle context to another. Problems begin when this bean doesn't have an interface such as MongoClient. First bundle context register Mongoclient, but when I want to expose it to another one I get: "no bean could be found in the registry for mongo of type: com.mongodb.Mongo" from reference bundle. Is there any way to define a bean in OSGi registry by class, not interface?

Exception comes from reference bundle:

Exception in thread "SpringOsgiExtenderThread-86"
org.apache.camel.RuntimeCamelException:
org.apache.camel.FailedToCreateRouteException: Failed to create route article-author-getAll at: >>> Filter[{in ([header{operationName} ==
getAllAuthors])} -> [SetHeader[CamelMongoDbLimit, {2}],
To[mongodb:mongo?database=xxxx&collection=xxxx&operation=findAll], Log[after db select getAllAuthors ${body}]]] <<< in route:
Route(article-author-getAll)[[From[activemq:queue:backend.au...
because of Failed to resolve endpoint:
mongodb://mongo?collection=xxx&database=xxxx&operation=findAll due to: No bean could be found in the registry for: mongo of type:
com.mongodb.Mongo

In service bundle, everything looks good!

code looks like this in service bundle:

 <bean id="mongoDatasource" class="com.mongodb.MongoClient">
    <constructor-arg name="uri" ref="mongoClientUri" />       
</bean>

<bean id="mongoClientUri" class="com.mongodb.MongoClientURI">
<constructor-arg name="uri" value="${mongo_host}" />
</bean>

Code from reference bundle context:

<reference id="mongoDataSourceReference" bean-name="mongoDatasource" 
context-class-loader="service-provider" 
interface="com.mongodb.MongoClient"/>

MongoClient doesn't have interface and osgi:reference must have defined interface property.

I've tried to extend MongoClient class and implements Interface and then expose it to osgi registry I received it properly in reference bundle but then I got exception from camelMongo where I must define only MongoClient class!

Camel Mongo route look like this:

from("direct:findAll")
.to("mongodb:MYMONGOCLIENTBEAN?database=flights&collection=tickets&operation=findAll")

Camel mongo route expect MongoClient bean in connection string.

So is there any way to define the bean in osgi registry by class, not interface? or I should define MongoClient bean in the same bundle as camelMongo ?

rahul singh Chauhan
  • 323
  • 1
  • 4
  • 15
bartoszO
  • 21
  • 1

1 Answers1

0

Before getting a reference to existing OSGi service bean, you first need to export this bean as an OSGi service:

<osgi:service ref="beanToPublish" interface="com.xyz.MyService"/>

Although recommended, your service class does NOT need to implement an interface. See the specs: https://docs.spring.io/spring-osgi/docs/current/reference/html/service-registry.html#service-registry:export

The OSGi Service Platform Core Specification defines the term service interface to represent the specification of a service's public methods. Typically this will be a Java interface, but the specification also supports registering service objects under a class name, so the phrase service interface can be interpreted as referring to either an interface or a class.

So in theory nothing should prevent you from getting a reference to your Mongo bean using the full classname.

TacheDeChoco
  • 3,683
  • 1
  • 14
  • 17