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

时间:2016-07-19 13:43:52

标签: java apache-camel datasource apache-karaf blueprint

给定一个非常简单的Karaf的Camel包,用camel-archetype-blueprint生成,我想添加一个通过属性而不是blueprint.xml内配置的数据源。

我尝试配置PropertiesComponent并以各种方式访问​​MySQL数据源的property值内的属性,但似乎都没有。记录消息时,可以访问属性。

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

我特别需要为多个捆绑包使用相同的数据源配置,并区分生产/测试环境。我考虑过在构建过程中使用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>

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

Project layout

2 个答案:

答案 0 :(得分: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>

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>

从其他论坛引用

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

要在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>

使用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>

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

P.S。:如果你想从配置文件中删除明文密码,请使用像jasypt这样的东西。

答案 1 :(得分:1)

您好我可以看到两件事。第一个是你没有说过配置文件所在的类路径中的哪个文件夹。除非尚未扫描OSGI-INF文件夹,否则您应该:

第二件事是关于如何引用属性。除非另有说明,否则属性 prefixToken suffixToken 默认为“{{”“}}” http://camel.apache.org/properties.html

这意味着您需要将 $ {mysqlUser} 替换为 {{mysqlUser}}