使用 Apache Camel SQL 存储过程组件访问 OUT 参数

Accessing OUT parameters with Apache Camel SQL Stored Procedure Component

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

我正在调用的存储过程有几个 OUT 参数,它们都在 BODY 中作为字符串返回,例如:

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

目前我是 tokenizing/parsing 能够提取参数的 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?

sql-stored-component 制作人已经 returns Map 到 body。扔掉你所有的解析逻辑,因为你正在解析的字符串只是 Map.

toString() 表示
...
<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]})"/>

然后在您的 varpop bean 中使用它:

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);
        //...
    }
}