在駱駝處理器中,我設定了兩個帶有屬性名稱的靜態變數:
public static final String CI_PROPERTY = "ci";
public static final String IS_PDF_PROPERTY = "isPdf";
我分配喜歡:
exchange.setProperty(CI_PROPERTY, documentProperties.get(MAP_PARAMETER_ATTACHMENT_URI));
exchange.setProperty(IS_PDF_PROPERTY, documentProperties.get(MAP_PARAMETER_IS_PDF));
這些名稱應該在其他處理器中用于檢索屬性。
問題是:其他處理器可以訪問這些名稱嗎?或者我應該把他們轉移到另一個班級?萬一,在哪里?
uj5u.com熱心網友回復:
是的,您可以這樣做,如下所示:
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class FooProcessor implements Processor {
public static final String FOO_PROPERTY = "FOO";
@Override
public void process(Exchange exchange) throws Exception {
exchange.setProperty(FOO_PROPERTY, "This is a Foo property.");
}
}
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class BarProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getMessage().setBody(exchange.getProperty(FooProcessor.FOO_PROPERTY, String.class));
}
}
from("direct:mainRoute")
.routeId("MainRoute")
.log("MainRoute BEGINS: BODY: ${body}")
.process(new FooProcessor())
.process(new BarProcessor())
.log("MainRoute ENDS: BODY: ${body}")
.end()
;
當上述路線運行時,將按預期記錄以下內容:
MainRoute BEGINS: BODY:
MainRoute ENDS: BODY: This is a Foo property.
但是我認為處理器不應該對其他處理器具有編譯時間(或運行時)依賴性。像往常一樣將公共部分重構為處理器使用的另一個類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/483651.html
