2

我正在使用Apache Camel 的 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>

是否有直接访问param1,param2等的直接方式而无需解析 BODY?

4

1 回答 1

3

sql-stored-component生产者已经返回Map身体。扔掉你所有的解析逻辑,因为你正在解析的字符串toString()只是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]})"/>

然后在你的varpopbean中使用它:

public class Varpop{
    // Variant 1: Map as argument
    public void MYBEAN(Map<String, String> procedureResult){
        System.out.println(procedureResult.get("param1"));
        System.out.println(procedureResult.get("param2"));
        //...
    }

    // Variant 2: Multiple arguments
    public void MYBEAN(String param1, String param2, String param3, String param4){
        System.out.println(param1);
        System.out.println(param2);
        //...
    }
}
于 2019-01-15T20:06:33.740 回答