Mule ESB,JDBC转换器中的“IN”运算符

时间:2014-02-28 09:29:45

标签: java mule esb mule-el

让我们想要将列表传递给JDBC转换器。

例如,我在流程中创建列表:

<scripting:transformer><scripting:script engine="groovy"><scripting:text>
    // Some calculations and stuff

    def single_quote_alias = "'"
    def listString = seasonMbIds.join("',' ")           
    return single_quote_alias + listString + single_quote_alias // now it's like:  '11','22','33' 
</scripting:text></scripting:script></scripting:transformer>

然后将列表传递给另一个变换器:

<jdbc:outbound-endpoint connector-ref="XYZ" queryKey="someName" exchange-pattern="request-response" />

这是实际查询:

<jdbc:query key="someName" 
                value="SELECT anID FROM aTable WHERE aField IN (#[payload:])" />

它给了我“超出索引的索引异常”:

Message               : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 (javax.script.ScriptException)
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Index: 0, Size: 0 (java.lang.IndexOutOfBoundsException)
  java.util.ArrayList:635 (null)
2. java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 (javax.script.ScriptException)

也许有一些逃避问题。我使用了不同的分隔符,问题仍然存在......

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

由于查询格式选项有限,使用Mule进行更高级的JDBC操作可能非常非常痛苦。我知道您有两个选项:1)编写使用jdbc连接器执行查询的脚本,或2)为jdbc连接器定义自定义查询策略。这是一个简单的hack,您可以使用选项2使单个查询工作(虽然会破坏所有其他选择查询...):

包含您的自定义Java类:

<jdbc:connector name="Database" dataSource-ref="MySQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database">
    <jdbc:sqlStatementStrategyFactory class="org.mule.transport.jdbc.sqlstrategy.MySqlStatementStrategyFactory"/>
</jdbc:connector>

编写自定义工厂Java类:

package org.mule.transport.jdbc.sqlstrategy;

public class MySqlStatementStrategyFactory extends DefaultSqlStatementStrategyFactory {

  public MySqlStatementStrategyFactory()
  {
    super();
    selectSQLStrategy = new MySelectSqlStatementStrategy();
  }

}

创建您的策略类,并从here

复制默认选择策略的源代码

替换复制的源代码中的类定义:

public  class MySelectSqlStatementStrategy extends SelectSqlStatementStrategy

找到创建查询结果的两行,并替换为您自己的实现,例如:

result = connector.getQueryRunnerFor(endpoint).query(connection, readStmt.replace("?", (String)params[0]),
      connector.getResultSetHandler());
相关问题