[英]Using Apache Camel and Spring, can I build uri using properties?
[英]Apache Camel: Can I use @Produce without specifying the endpoint uri in the annotation?
在camel 文檔中 ,提到我們可以使用@Produce注釋注入ProducerTemplate,如下所示。
@Produce(uri = "activemq:foo")
private ProducerTemplate producer;
我想知道是否有可能不在注釋中指定uri並在其sendBody方法中將uri傳遞給生產者,如下所示,以便我可以在Util類中使用它來生成任何端點?
public class JmsProducerUtils {
@Produce
private ProducerTemplate producer;
public void publishMessageToEndpoint(String endpoint, String message) {
producer.sendBody(endpoint, message);
}
}
在使用它時是否有任何顧慮?
好吧,不用擔心,它應該可以工作 - 你可以編寫一個簡單的單元測試來驗證它 - 但除非你的URI是在運行時動態形成的,我建議使用他們在文檔中提到的方法,例如有一個接口:
@InOnly
public class JmsProducer {
void publish(String message);
}
然后將它注入您的類中,如下所示:
public class Foo {
@Produce(uri = "activemq:foo")
private JmsProducer fooProducer;
@Produce(uri = "activemq:bar")
private JmsProducer barProducer;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.