如何将 ConnectionFactory class 设置为 Jms outboundAdapter? (在 Spring 集成中)

How to set a ConnectionFactory class to an Jms outboundAdapter? (within Spring Integration)

我正在使用 Spring 集成,我正在尝试(在 IntegrationFlow 中)设置 outboundAdapterJms 中的 connectionFactory。由于我是那些 classes 的新手,所以我无法创建任何不会在该代码片段中引发错误的内容。我如何定义 ConnectionFactory class 才能工作?

private MessageChannel workflowExample() {
    return IntegrationFlows
            .from(requests())
            .transform(someTransformer())
            .handle(Jms.outboundAdapter(connectionFactory)
                   .destination("somewhere"))
            .get();
}

您需要了解您将要使用的 JMS 客户端。 如果那是 ActiveMQ,那么 ConnectionFactory 可能是这样的:

ConnectionFactory amqFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false")

这就是我们在嵌入式模式下进行测试的方式。

如果您的 JMS 代理由 JEE 服务器管理,那么您需要为 ConnectionFactory 对象找出一个 JNDI 名称并使用 Spring JNDI 支持来获取它。

如果您处理 IBM MQ,则需要使用他们的 JMS 包装器才能正确实例化 ConnectionFactory。等等。有一个用于 Tibco JMS 的客户端,一些用于 Rabbit MQ 的 JMS 包装器等。

无论如何,ConnectionFactory 实例化不是 Spring 集成责任。您确实需要弄清楚您使用的是什么 JMS 客户端,并按照其文档如何创建。 Spring JMS 支持可以帮助您处理一些样板代码和依赖项管理:https://docs.spring.io/spring/docs/5.2.3.RELEASE/spring-framework-reference/integration.html#remoting-jms

您还可以查看 profile 支持,这样您就可以在本地针对嵌入式 ActiveMQ 测试您的流程,并在生产环境中连接到外部代理:https://docs.spring.io/spring/docs/5.2.3.RELEASE/spring-framework-reference/core.html#beans-definition-profiles