java - Apache Camel : Can I use @Produce without specifying the endpoint uri in the annotation?

标签 java annotations apache-camel jms

在 Camel 中documentation ,其中提到我们可以使用@Produce注解注入(inject)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 是在运行时动态形成的,否则我建议使用他们在文档中提到的方法,例如有一个接口(interface):

@InOnly
public class JmsProducer {
  void publish(String message);
}

然后将其注入(inject)到您的类中,如下所示:

public class Foo {
  @Produce(uri = "activemq:foo")
  private JmsProducer fooProducer;

  @Produce(uri = "activemq:bar")
  private JmsProducer barProducer;
}

关于java - Apache Camel : Can I use @Produce without specifying the endpoint uri in the annotation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38349702/

相关文章:

annotations - guice 忽略提供者?

jms - Apache Camel 和 Stomp

http - 使用 Apache Camel 做一个火而忘记 HTTP 请求

java - Spring异步发送消息时 "topics"这个词是什么意思?

Java 相当于 Kotlin 的 arrayof()/listof()/setof()/mapof()

java - 在显示 JOptionPane.showMessageDialog 后重新启用 Java 中的工具提示

java - Apache Camel - 从 XML 获取 xpath 值

java - 在 android 中使用谷歌地图无法调用 SetOnItemClickListener 事件

spring - 访问通知中的注释值

java - 自动生成值对象