1

I am using the following Maven dependency and class in my Spring Boot application to send messages to 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);
    }*/
    
}

How could I use the same class or any other class within the same application to send messages to IBM MQ as well? If I add the add the below dependency how will the @Autowired JmsTemplate behave?

<dependency>
   <groupId>com.ibm.mq</groupId>
   <artifactId>mq-jms-spring-boot-starter</artifactId>
   <version>2.5.0</version>
</dependency>
chughts
  • 4,210
  • 2
  • 14
  • 27
  • I think it is the same as when you have multiple databases (for example). You have to define "@Bean" with a "@Qualifier" and inject one or the other as needed. Example for datasources: https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources – pringi Feb 15 '22 at 17:17

1 Answers1

1

You can get the IBM MQ client jars file with Maven dependency:

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

As to your @Autowired question, your problem is ambiguity by type. You will need JMS connections factories for the two brokers that have the same type. The article here has some good suggestions.

I prefer to only @Autowire in test cases, where the application context is small. For a practical application, I prefer explicit initialization. (maybe I'm just used to it). In any event, have a look here github for some examples that may have be useful.

Doug Grove
  • 962
  • 5
  • 5