Pergunta

I did setup a very simple route in Apache Camel where a query is sent to a JDBC component to execute. I got the Camel project up and running. What I'm trying to accomplish is to send dataSource1's database connection parameters in a RabbitMQ message's header. By connection parameters I mean driverClassName, url, username, password. The client of my app would enter all those parameters to decide what database to connect to. I will probably use routing slip depending on what driverClassName user has specified but that's a different thing.

Please note that in this example here I put the SQL statement in a file to make it simpler. How can I accomplish that?

Here's my Camel Context:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

  <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <camel:route>
      <camel:from uri="file:src/data?noop=true"/>
      <camel:to uri="jdbc:dataSource1"/>
      <camel:convertBodyTo type="java.lang.String"/>
      <camel:log message="${body}"/>
    </camel:route>
  </camel:camelContext>

  <bean id="dataSource1"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/employees"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
  </bean>
</beans>

And Maven project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>group1</groupId>
  <artifactId>com.mycompany</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>A Camel Spring Route</name>
  <url>http://www.myorganization.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.12.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>2.12.2</version>
    </dependency>

    <!-- logging -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!-- testing -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-test-spring</artifactId>
      <version>2.12.2</version>
      <scope>test</scope>
    </dependency>

      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.26</version>
      </dependency>

      <!-- Jdbc -->
      <dependency>
          <groupId>org.apache.camel</groupId>
          <artifactId>camel-jdbc</artifactId>
          <version>2.12.2</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>4.0.0.RELEASE</version>
      </dependency>
  </dependencies>

  <build>
    <defaultGoal>install</defaultGoal>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <!-- allows the route to be ran via 'mvn camel:run' -->
      <plugin>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-maven-plugin</artifactId>
        <version>2.12.2</version>
      </plugin>
    </plugins>
  </build>
</project>
Foi útil?

Solução

The datasource is a Spring bean. Create a bean of your own and have Spring inject the datasource. You can then plug that bean into your route before the JDBC step and use the exchange headers, which should contain the JMS headers, to update the properties of the datasource.

However, I think this design approach is asking for trouble:

  • If you ever wanted to use a pooled data source (and you should), then you need to reset the pool of connections which is expensive
  • You need to synchronize the state modifications of the (singleton) data source as it is not threadsafe in that respect and with Camel you are working in a multi-threaded framework
  • You are sending user names and passwords for your DBs around the place and you should protect that information while it is in transit

There is probably more...

Outras dicas

Maybe a few years later. Buy I achieved this using AbstractRoutingDataSource which allows to change the data source dynamically (you do not need to reset the connection pool or the route)

Reference Link: https://spring.io/blog/2007/01/23/dynamic-datasource-routing/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top