正如它在 DBLookup Mediator 的文档中所说,它只返回查询的第一行,其他结果(如果是)将被忽略。
我想知道是否有“最佳方式”来运行返回多条记录然后处理它们的查询(SELECT * FROM X)。现在有一天我们正在实现axis2服务,但是还有另一种方法可以使用wso2 esb提供的中介组合来完成这个要求?
提前致谢。
圣地亚哥。
是的 DBlookup 调解器不会返回多行。您可以使用两种选择。
1) 使用 WSO2 数据服务服务器创建数据服务并使用调用中介从 ESB 调用该服务。
2)您可以编写一个类中介来从数据库中查询数据,然后从中创建一个有效负载,然后通过序列发送它。
为了避免编写另一个服务或设置一个完整的 WSO2 数据服务服务器来克服 DB 查找中介器的缺点,我扩展了现有的中介器,但由于时间限制没有将他的代码贡献回社区。这是更新的 org.apache.synapse.mediators.db.DBLookupMediator 的代码。
基本上,它将 ResultSet 转换为 XML 格式并将结果设置为 DB_SEARCH_RESULT 属性。它可能也需要在比赛条件下进行一些抛光和测试。
package org.apache.synapse.mediators.db;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseException;
import org.apache.synapse.SynapseLog;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.StringWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Connection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* Simple database table lookup mediator. Designed only for read/lookup
*/
public class DBLookupMediator extends AbstractDBMediator {
public static final String DB_SEARCH_RESULTS_PROPERTY_NAME = "DB_SEARCH_RESULT";
protected void processStatement(Statement stmnt, MessageContext msgCtx) {
SynapseLog synLog = getLog(msgCtx);
// execute the prepared statement, and extract the first result row and
// set as message context properties, any results that have been specified
Connection con = null;
ResultSet rs = null;
try {
PreparedStatement ps = getPreparedStatement(stmnt, msgCtx);
con = ps.getConnection();
rs = ps.executeQuery();
// convert RS to XML
String rsXML = convertRSToXML(rs);
// add result XML to the Message Context
msgCtx.setProperty(DB_SEARCH_RESULTS_PROPERTY_NAME, rsXML);
// rollback to the beginning of ResultSet to allow standard processing
rs = ps.executeQuery();
if (rs.next()) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug(
"Processing the first row returned : " + stmnt.getRawStatement());
}
for (String propName : stmnt.getResultsMap().keySet()) {
String columnStr = stmnt.getResultsMap().get(propName);
Object obj;
try {
int colNum = Integer.parseInt(columnStr);
obj = rs.getObject(colNum);
} catch (NumberFormatException ignore) {
obj = rs.getObject(columnStr);
}
if (obj != null) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Column : " + columnStr +
" returned value : " + obj +
" Setting this as the message property : " + propName);
}
msgCtx.setProperty(propName, obj.toString());
} else {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebugWarn("Column : " + columnStr +
" returned null Skip setting message property : " + propName);
}
}
}
} else {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Statement : "
+ stmnt.getRawStatement() + " returned 0 rows");
}
}
} catch (SQLException e) {
handleException("Error executing statement : " + stmnt.getRawStatement() +
" against DataSource : " + getDSName(), e, msgCtx);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {}
}
if (con != null) {
try {
con.close();
} catch (SQLException ignore) {}
}
}
}
private String convertRSToXML(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
// create XML document
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
Document doc = null;
try {
docBuilder = dbfac.newDocumentBuilder();
doc = docBuilder.newDocument();
} catch (ParserConfigurationException pce) {
throw new SynapseException("Failed to transform Resultset to XML", pce);
}
// create Root element
Element rootElement = doc.createElement("table");
doc.appendChild(rootElement);
while (rs.next()) {
// add Record element
Element recordElement = doc.createElement("record");
rootElement.appendChild(recordElement);
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String columnName = rsmd.getColumnName(i);
String columnValue = rs.getObject(i).toString();
// add Field element
Element fieldElement = doc.createElement("field");
fieldElement.appendChild(doc.createTextNode(columnValue));
// set Name attribute to Field element
Attr nameAttr = doc.createAttribute("name");
nameAttr.setValue(columnName);
fieldElement.setAttributeNode(nameAttr);
// add Field to Record
recordElement.appendChild(fieldElement);
}
}
//Output the XML
String xmlString = null;
try {
//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
//create string from XML tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
xmlString = sw.toString();
} catch (javax.xml.transform.TransformerException te) {
throw new SynapseException("Failed to transform Resultset to XML", te);
}
return xmlString;
}
}
您不能使用 DBLookUp 调解器检索多行。但是您可以使用数据服务服务器 (DSS) 并创建查询。然后您可以使用调用中介或发送中介来调用它们。在 WSO2 EI 中,DSS 也可用。请参阅文档
https://docs.wso2.com/display/EI611/Generating+a+Data+Service https://docs.wso2.com/display/EI611/Exposing+Data+as+a+REST+Resource
此处数据可以作为 REST(在 DSS 资源中)或 SOAP(在 DSS 操作中)公开。
只需从这里https://github.com/ichakios/dbselect-wso2-mediator使用 DBSelect 调解器 易于使用并返回 JSON 结果