![](/img/trans.png)
[英]Apache Camel: get consumer endpoint information in bean method called by the Camel route
[英]How bean method is called in Apache camel?
這是我的豆子
package beans;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class setFileNames {
public String getCityName(Document document) {
NodeList cities = document.getDocumentElement().getElementsByTagName(
"city");
Element city = (Element) cities.item(0);
String textContent = city.getTextContent();
return textContent+".xml";
}
}
這是我的路線
public void configure ( ) throws java .lang .Exception {
from ( uril1ap .get ("Sender cFile 1") ).routeId ( "Sender_cFile_ l").split()
.xpath ( "/ people/ person") .id ( "cSplitter_ l")
.setHeader ( "CamelFileName") .method ( beans.setFileNames.class)
.id ( "cSetHeader_ l") .to ( uril1ap .get ( "Receiver_cFile_2") )
.id ( "cFile_2") .process ( new org .apache .camel . Processor ( )
{ public void process ( org .apache .carnel .Exchange exchange)
throws Exception {
System .out .println ( "File rece ived : "
+ exchange .getIn ().getHeader ( "CamelFileName") ) ;
}) .id ( "cProcessor_ l");
您可能會在上述路線中發現一些語法問題,但它工作正常(此處復制的代碼不准確)
想知道,正如我們所看到的, CamelFileName
bean 正在路由中使用,它是類setFileNames
一個實例,但是它如何在路由中調用它的getCityName
方法? 路由如何知道調用和打印哪個方法?
正是這個語句調用了 Bean:
.method ( beans.setFileNames.class)
盡管在您的情況下該參數有點奇怪,但您有多種選擇可以傳遞給method
語句。
最常見的是
.method(MyBean.class)
.method(myBean)
.method("myBean")
名稱.method("myBean")
在您的情況下,方法選擇非常簡單,因為只有一種方法。
但是,Camel 有一個相當“精心”的算法來選擇 Bean 類中最合適的方法。
如果此算法選擇了錯誤的方法,您可以通過名稱顯式調用該方法: .method(MyBean.class, "myMethod")
據我所知,Camel 調用類中的公共方法,在這種情況下,它還將其參數Document
推斷為消息正文,因為沒有注釋(例如 @Header、@ExchangeProperty 等)與參數相關聯。
但是,如果您的 bean 類有多個公共方法,那么 Camel 會嘗試調用參數與消息正文類型匹配的方法。 通過查找參數具有相同類型的多個公共方法,然后它會由於歧義問題引發運行時異常。
正如@burki 所提到的,您可以指明要在路由中調用的方法。 但是您也可以通過使用@Handler
注釋來將 bean 類中的一個方法標記為默認方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.