【问题标题】:Connect to both ActiveMQ and IBM MQ连接到 ActiveMQ 和 IBM MQ
【发布时间】:2022-02-17 21:41:07
【问题描述】:

我在我的 Spring Boot 应用程序中使用以下 Maven 依赖项和类向 ActiveMQ 发送消息:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
@EnableJms
public class MQSender {

    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMsgActiveMQ(String msg) {
        jmsTemplate.convertAndSend("DEV.QUEUE1", msg);
    }
    
/*    public void sendMsgIBMMQ(String msg) {
        jmsTemplate.convertAndSend("DEV.QUEUE1", msg);
    }*/
    
}

如何在同一应用程序中使用同一个类或任何其他类也将消息发送到 IBM MQ?如果我添加以下依赖项,@Autowired JmsTemplate 的行为会如何?

<dependency>
   <groupId>com.ibm.mq</groupId>
   <artifactId>mq-jms-spring-boot-starter</artifactId>
   <version>2.5.0</version>
</dependency>

【问题讨论】:

  • 我认为这与您拥有多个数据库时相同(例如)。您必须使用“@Qualifier”定义“@Bean”并根据需要注入其中一个。数据源示例:stackoverflow.com/questions/30337582/…

标签: spring-boot activemq ibm-mq spring-jms


【解决方案1】:

您可以获取具有 Maven 依赖项的 IBM MQ 客户端 jars 文件:

    <dependency>
        <groupId>com.ibm.mq</groupId>
        <artifactId>com.ibm.mq.allclient</artifactId>
        <version>9.1.0.6</version>
    </dependency>

至于您的@Autowired 问题,您的问题是类型不明确。对于具有相同类型的两个代理,您将需要 JMS 连接工厂。文章here有一些不错的建议。

我更喜欢在应用程序上下文很小的测试用例中只使用@Autowire。对于实际应用,我更喜欢显式初始化。 (也许我只是习惯了)。无论如何,请在此处查看 github 以获取一些可能有用的示例。

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2016-01-21
    • 2023-04-05
    • 2020-09-18
    • 2022-11-08
    • 2023-03-05
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多