如何在没有硬编码的情况下调用多个数据库?

时间:2016-03-30 20:16:14

标签: apache apache-camel blueprint-osgi apache-servicemix

我有以下情况:

有许多站点/部门,每个站点都有自己的ServiceMix和不同数量的数据库。这些数据库是分组的,例如有两组:production_one和production_two。

在每个站点上,我将bundle作为数据源部署,每个数据库都有一个数据源。

数据源:blueprint.xml

<?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" xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
       xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
       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">


<!-- Properties -->
<!-- blueprint property placeholders, that will use etc/datasources.cfg as the properties file -->
<cm:property-placeholder id="db.placeholder" persistent-id="datasources">
</cm:property-placeholder>


<!-- Datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url"
              value="jdbc:sqlserver://${bundle_ds_1_db_host}:${bundle_ds_1_db_port};databaseName=${bundle_ds_1_db_database};" />
    <property name="username" value="${bundle_ds_1_db_username}" />
    <property name="password" value="${bundle_ds_1_db_password}" />
</bean>

<!-- Init database-->
<bean id="initDatabase"
      class="com.xyz.mssql.ds.DatabaseInitBean"
      init-method="create" destroy-method="destroy">
    <property name="ds" ref="dataSource" />
</bean>

<service ref="dataSource" interface="javax.sql.DataSource">
    <service-properties>
        <entry key="datasource.name" value="${bundle_ds_1_ds_name}" />
        <entry key="datasource.type" value="${bundle_ds_1_ds_type}" />
        <entry key="datasource.id" value="${bundle_ds_1_ds_id}" />
        <entry key="datasource.group_id" value="${bundle_ds_1_ds_group_id}" />
    </service-properties>
</service>

还有bundle,它通过cxf接收SOAP消息并通过调用database来处理它:

CXF:blueprint.xml

    <?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" xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
    xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
    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/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd
             http://camel.apache.org/schema/blueprint     http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <cxf:cxfEndpoint id="processXEndpoint" address="/mssql-soap1/"
        serviceClass="com.xyz.mssql.soap1.ProcessXService" />

    <bean id="saveDataProcessor"
        class="com.xyz.mssql.soap1.SaveDataProcessor">
        <property name="ds" ref="dataSource" />
    </bean>

    <bean id="getDataProcessor"
        class="com.xyz.mssql.soap1.GetDataProcessor">
        <property name="ds" ref="dataSource" />
    </bean>


    <!-- DataSource -->
    <reference id="dataSource" interface="javax.sql.DataSource"
        filter="(datasource.name=mssql-ds-1)">
        <reference-listener bind-method="onBind"
            unbind-method="onUnbind">
            <bean class="com.xyz.mssql.soap1.ListenerBean" />
        </reference-listener>
    </reference>

    <!-- Camel Context -->
    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route id="cxf">
            <from uri="cxf:bean:processXEndpoint" />
            <recipientList>
                <simple>direct:${header.operationName}</simple>
            </recipientList>
        </route>
        <route id="saveRoute">
            <from uri="direct:saveData" />
            <log message="Calling saveData" />
            <process ref="saveDataProcessor" />
        </route>
        <route id="getRoute">
            <from uri="direct:getData" />
            <log message="Call getData" />
            <process ref="getDataProcessor" />
        </route>
    </camelContext>
</blueprint>

问题是,以下代码注入bean(例如:SaveDataProcessor)只有一个数据源由它的名称“datasource.name = mssql-ds-1”解析,但我需要调用许多数据库并在每个servicemix上安装将是不同数量的。

我需要调用具有相同类型的所有数据库(数据源),例如“datasource.type = production_one”。 可能吗?怎么做?

如何向bean传递数据源数组,或者针对满足相同条件(名称,类型等)的每个数据源调用此bean。

0 个答案:

没有答案