Search code examples
springapache-camelosgiapache-servicemix

Accessing a datasource from an OSGi bundle


I have a blueprint file containing a datasource deployed to Apache ServiceMix. I was able to query the datasource from Apache Karaf console. How can I access this datasource from a Camel Spring-DM bundle application? This is my blueprint file:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
   <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
        <property name="URL" value="URL"/>
        <property name="user" value="USER"/>
        <property name="password" value="PASSWORD"/>
    </bean>
  <service interface="javax.sql.DataSource" ref="dataSource" id="ds">
    <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/ds"/>
    </service-properties>
  </service>
</blueprint>

Solution

  • You can bind the DataSource as an OSGi service. In spring dm this is osgi:reference, in blueprint it would be reference.

    <reference id="dataSource" interface="javax.sql.DataSource"/>
    

    You can then inject the DataSource for example into the SqlComponent.

    As an example see a fix I did for this camel route. This is blueprint but it is almost the same for spring dm.

    <bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
        <property name="dataSource" ref="dataSource"/>
    </bean>