使用Apache Camel SQL存储过程组件

时间:2019-01-15 13:50:15

标签: apache-camel camel-sql

我正在使用Apache Camel's SQL Stored Procedure Component调用Oracle存储过程。

我正在调用的存储过程具有多个OUT参数,这些参数都作为字符串返回到BODY中,例如:

{param1=0, param2=-, param3=<?xmlversion="1.0"?><Client><Item><PHONE>1234567890</PHONE></Item></Client>}

当前,我正在标记/解析BODY以能够提取参数。这可以工作,但是效率不高(并且容易出错)。

我的代码如下:

...
<bean id="od" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driver">
        <bean class="oracle.jdbc.OracleDriver"/>
    </property>
    <property name="url" value="${connURL}"/>
</bean>
...
<camel:to uri="sql-stored:PACKAGE.STORED_PROCEDURE(VARCHAR ${exchangeProperty.param0},OUT VARCHAR param1,OUT VARCHAR param2,OUT VARCHAR param3,OUT VARCHAR param4)?dataSource=od"> 
...
<!-- I tried this but "abc" always has no value -->
<camel:setHeader headerName="abc">
    <camel:description layoutX="280" layoutY="20" layoutWidth="120" layoutHeight="120"/>
    <camel:simple>$simple{header.param3}</camel:simple>
</camel:setHeader>
<!-- This is how I parse the BODY (inefficient!) -->
<camel:groovy><![CDATA[

    def outParams = [
        param1: "NO_VALUE",
        param2: "NO_VALUE",
        param3: "NO_VALUE",
        param4: "NO_VALUE"   
    ]
    def retERR = "yes"
    def t = ""
    def bd = exchange.getIn().getBody(String.class)

    try {
        def varList = bd.replaceAll("\n","").replaceAll("^\\{","").replaceAll("\\}\$","")
        varList = varList.tokenize(',')
        for (keyVal in varList) {
            keyVal = keyVal.trim()
            i = keyVal.indexOf('=')
            outParams[keyVal.take(i)] = keyVal.substring(i+1)
        }
        retERR = "no"
    } catch(all) {
        retERR = "yes"
    }

    exchange.setProperty("outParams",outParams)
    exchange.setProperty("retERR",retERR)

]]>
</camel:groovy>
...
<camel:to uri="bean:varpop?method=MYBEAN(&apos;outParams,retERR,param3,abc&apos;)">
    <camel:description layoutX="20" layoutY="470" layoutWidth="120" layoutHeight="120"/>
</camel:to>

是否有直接方法直接访问param1param2等,而不必解析BODY?

1 个答案:

答案 0 :(得分:2)

sql-stored-component生产者已经将Map返回到正文。丢弃所有解析逻辑,因为您正在解析的String只是toString()的{​​{1}}表示形式。

Map

然后在您的... <camel:to uri="sql-stored:PACKAGE.STORED_PROCEDURE(VARCHAR ${exchangeProperty.param0},OUT VARCHAR param1,OUT VARCHAR param2,OUT VARCHAR param3,OUT VARCHAR param4)?dataSource=od"/> <!-- Variant 1: Pass Map to MYBEAN method --> <camel:to uri="bean:varpop?method=MYBEAN(${body})"/> <!-- Variant 2: Get params from Map and pass it as arguments --> <camel:to uri="bean:varpop?method=MYBEAN(${body[param1]},${body[param2]},${body[param3]},${body[param4]})"/> bean中使用它:

varpop