使用JDBC传输从Mule流中的PostgreSQL数据库中获取数据

时间:2014-07-23 07:05:07

标签: postgresql jdbc mule

我有一个Mule流来从PostgreSQL数据库中的表中获取数据,并将数据转换为XML格式并写入文件:

<mule ...>
    <spring:bean id="Postgres-jdbcDataSource"
        class="org.enhydra.jdbc.standard.StandardDataSource" destroy-method="shutdown">
        <spring:property name="driverName" value="org.postgresql.Driver" />
        <spring:property name="url"
            value="jdbc:postgresql://host:port/schema?user=username&password=password" />
    </spring:bean>

    <jdbc:connector name="Postgres-jdbcConnector"
        dataSource-ref="Postgres-jdbcDataSource" pollingFrequency="60000"
        transactionPerMessage="false">
        <jdbc:query key="read" value="SELECT * FROM tablename" />
    </jdbc:connector>

    <file:connector name="file_connector" fileAge="500"
        streaming="false" pollingFrequency="60000" />

    <flow name="Postgres-flow">
        <jdbc:inbound-endpoint queryKey="read"
            connector-ref="Postgres-jdbcConnector">
            <jdbc:transaction action="ALWAYS_BEGIN" />
            <property key="receiveMessageInTransaction" value="true" />
        </jdbc:inbound-endpoint>

        <custom-transformer name="Postgres-transformer"
            class="com.example.transformer.DbToXmlTransformer" ignoreBadInput="false"
            encoding="UTF-8" />

        <file:outbound-endpoint connector-ref="file_connector"
            path="/home/path" outputPattern="file.xml" responseTimeout="10000"
            encoding="UTF-8" />
    </flow>
</mule>

当我运行此流程时,流程不会从DB获取数据并写入文件。它也不会抛出任何错误或异常。但是当我为MySQL或SQLServer数据库运行相同的流程时,相应地更改driverName和url属性,流程工作正常。

知道为什么Postgres数据库不起作用?可能它需要不同的DataSource类?

1 个答案:

答案 0 :(得分:2)

还有一个用于mule的Postgre数据源,您可以使用它代替spring bean:

<jdbc:postgresql-data-source name="PostgreSQL_Data_Source" user="your user name" password="your pwd" url="jdbc:postgresql://localhost:5432/TestDB" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>

无论如何......,使用现有配置,您可以通过将JDBC入站端点保留在poll组件中并在File outbound之前放置一个记录器来检查有效负载值来进行检查。如果它得到了记录器中的有效负载值..这意味着它正在获取值..让我知道它是否有效......您可以尝试以下方法: -

   <mule ...>
        <spring:bean id="Postgres-jdbcDataSource"
            class="org.enhydra.jdbc.standard.StandardDataSource" destroy-method="shutdown">
            <spring:property name="driverName" value="org.postgresql.Driver" />
            <spring:property name="url"
                value="jdbc:postgresql://host:port/schema?user=username&password=password" />
        </spring:bean>

        <jdbc:connector name="Postgres-jdbcConnector"
            dataSource-ref="Postgres-jdbcDataSource" pollingFrequency="60000"
            transactionPerMessage="false">
            <jdbc:query key="read" value="SELECT * FROM tablename" />
        </jdbc:connector>

        <file:connector name="file_connector" fileAge="500"
            streaming="false" pollingFrequency="60000" />

        <flow name="Postgres-flow">
          <poll frequency="1000" doc:name="Poll">
            <jdbc:inbound-endpoint queryKey="read"
                connector-ref="Postgres-jdbcConnector">
                <jdbc:transaction action="ALWAYS_BEGIN" />
                <property key="receiveMessageInTransaction" value="true" />
            </jdbc:inbound-endpoint>
    </poll>

     <!-- You can also use object to xml transformer if you are not using any custom transformer -->
     <!--<mulexml:object-to-xml-transformer doc:name="Object to XML"/> -->

            <custom-transformer name="Postgres-transformer"
                class="com.example.transformer.DbToXmlTransformer" ignoreBadInput="false"
                encoding="UTF-8" />

<logger message="Payload :- #[message.payload]" level="INFO" doc:name="Logger"/>

            <file:outbound-endpoint connector-ref="file_connector"
                path="/home/path" outputPattern="file.xml" responseTimeout="10000"
                encoding="UTF-8" />
        </flow>
    </mule>

有关更多信息,请参阅此处有关Mule的Postgre数据库的参考: - http://www.dotnetfunda.com/articles/show/2068/using-mule-studio-to-read-data-from-postgresqlinbound-and-write-it-to