How to reference a Talend data source from a Route

If you are using Talend data sources as a way of managing your database connections from Jobs, you’ll be pleased to know that you can reference these same data sources from Routes with a little additional configuration. This is particularly useful if you wish to use Camel’s SQL component in Talend ESB. Here’s an example of how to reference an OSGi datasource from a Talend Route.

Inspecting the datasource

To do this, first ensure that you have a Talend data source configured in your container. The following screenshot is from the Services tab in my Karaf Web Console. I have defined a datasource bean named jdbcIntegration, which is registered as an OSGi service with the name jdbcIntegration (blue box):

This data source can now be used in a Route.

Referencing the data source

In Talend Studio, modify the Route’s Spring configuration. Declare the following namespace in the <beans> element (the root element):

xmlns:osgi="http://www.springframework.org/schema/osgi"

Add the following text to the end of the schemaLocation attribute on the root element. This lets Spring know where to find the OSGi schema:

http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd

Then, still in the Spring configuration, add the following element to look up your datasource bean.

<osgi:reference
    id="dsIntegration"
    interface="javax.sql.DataSource"
    filter="(osgi.jndi.service.name=jdbcIntegration)" />

The filter option specifies the OSGi service name of the data source bean. Your Spring configuration should look something like this:

Finally, add a cConfig component to the Route canvas, and select aopalliance-1.0.jar as a new dependency:

Deployment

When the Route is deployed into Talend Runtime (not run directly in Studio), the Route will look for the data source in the OSGi registry. The data source can then be used with the Camel SQL component, once you’ve added the Spring JDBC dependencies (I’ll cover this in a future article).

You will also see some entries like this in the logs; these indicate that the datasource has been successfully found:

22:12:34,097 | INFO  | xtenderThread-11 | al.aop.ServiceDynamicInterceptor  470 | 93 - org.springframework.osgi.core - 1.2.1 | Looking for mandatory OSGi service dependency for bean [myDatasource] matching filter (&(objectClass=javax.sql.DataSource)(osgi.jndi.service.name=jdbcIntegration))
22:12:34,097 | INFO  | xtenderThread-11 | al.aop.ServiceDynamicInterceptor  476 | 93 - org.springframework.osgi.core - 1.2.1 | Found mandatory OSGi service for bean [myDatasource]

Has this post helped you use a Talend data source in a Route? Please post your feedback in the comments.