通过Camel Blueprint中的属性配置SQL数据源(在Karaf中)

max*_*dev 7 java datasource apache-camel blueprint apache-karaf

给出了一个非常简单的Camel捆绑包,用于生成camel-archetype-blueprint,我希望添加一个通过属性而不是在属性中配置的数据源blueprint.xml.

我尝试以各种方式配置PropertiesComponent并访问propertyMySQL数据源的值中的属性,但似乎没有一个工作.记录消息时,可以访问属性.

如何使用属性文件中的参数值配置数据源?

我特别需要这个,为多个bundle使用相同的数据源配置,并区分生产/测试环境.我考虑过在构建过程中使用Maven编写属性,具体取决于目标环境.有关如何解决此数据源问题的其他最佳实践吗?

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="location" value="classpath:config.properties" />
    </bean>

    <bean id="dataSourceMySQL" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="url" value="jdbc:mysql://127.0.0.1/test_database" />
        <!-- This causes an error, as it tries to connect with
             `${mysqlUser}`@`localhost` without any evaluation -->
        <property name="user" value="${mysqlUser}" />
        <property name="password" value="${mysqlPassword}" />
    </bean>

    <service interface="javax.sql.DataSource" ref="dataSourceMySQL">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/mysqlDatasource" />
        </service-properties>
    </service>
    <bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
        <property name="dataSource" ref="dataSourceMySQL" />
    </bean>

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route id="messageQuery">
            <from uri="sql:SELECT * FROM messages" />
            <log message="The user property is: {{mysqlUser}}, the query result is: ${body}" />
        </route>
    </camelContext>

</blueprint>
Run Code Online (Sandbox Code Playgroud)

仅为概述,项目布局如下所示:

项目布局

rap*_*aëλ 7

a)与xml和bundle属性捆绑的数据源

您可以使用捆绑属性.以下示例可选择使用捆绑配置etc/org.camel.demo.cfg:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <!-- etc/org.camel.demo.cfg -->
    <cm:property-placeholder persistent-id="org.camel.demo" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0">
        <cm:default-properties>
            <cm:property name="mysqlUser" value="root"/>
            <cm:property name="mysqlPassword" value=""/>
        </cm:default-properties>
    </cm:property-placeholder>

    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="location" value="classpath:config.properties" />
    </bean>

    <bean id="dataSourceMySQL" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="url" value="jdbc:mysql://127.0.0.1/test_database" />
        <property name="user" value="${mysqlUser}" />
        <property name="password" value="${mysqlPassword}" />
    </bean>
</blueprint>
Run Code Online (Sandbox Code Playgroud)

b)共享数据源

另一种选择是使用共享数据源.只需部署一个仅包含数据源的蓝图文件(下面的示例使用postgres).

您也可以将其与配置属性结合使用,如上所示.

提供OSGi服务

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <!-- use config properties if needed -->

    <bean id="dataSource" class="org.postgresql.ds.PGPoolingDataSource" destroy-method="close">
        <property name="serverName" value=":"/>
        <property name="user" value="postgres"/>
        <property name="password" value="postgres"/>
        <property name="dataSourceName" value="demo"/>
        <property name="initialConnections" value="2"/>
        <property name="maxConnections" value="4" />
    </bean>

    <service interface="javax.sql.DataSource" ref="dataSource">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/demo"/>
        </service-properties>
    </service>
</blueprint>
Run Code Online (Sandbox Code Playgroud)

从另一个包引用

然后,您可以在捆绑包中查找数据源 osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/demo)

要在a中使用此数据源,SqlComponent必须像这样创建引用:

<reference id="dataSource" interface="javax.sql.DataSource"
    filter="(osgi.jndi.service.name=jdbc/mysql)">
</reference>
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
    <property name="dataSource" ref="dataSource" />
</bean>
Run Code Online (Sandbox Code Playgroud)

使用persistence.xml

确保导入org.demo.osgi.datasource.**.以下是一个用法示例persistence.xml:

<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" >

    <persistence-unit name="demo" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/demo)</jta-data-source>
        <mapping-file>META-INF/foo.xml</mapping-file>
    </persistence-unit>

</persistence>
Run Code Online (Sandbox Code Playgroud)

c)使用jdbc-feature(可选)

可以使用该jdbc功能创建和管理上面的xml文件.如果可用或不可用,它取决于您的版本:

JBossFuse:admin@545074693af1> features:install jdbc hibernate jndi
JBossFuse:admin@545074693af1> install mvn:org.postgresql/postgresql/9.4.1208
Bundle ID: 292
JBossFuse:admin@545074693af1> resolve 292
JBossFuse:admin@545074693af1> jdbc:create -t postgres -u postgres -p postgres  -url ${postgres.addr}:${postgres.port} demo
Run Code Online (Sandbox Code Playgroud)

PS:如果你想从配置文件中删除明文密码,请使用jasypt之类的东西.