Search code examples
spring-cloudspring-jmsspring-cloud-azure

Connecting to Azure Service bus using spring-cloud-azure-starter-servicebus-jms


I am trying to follow microsoft documentation on connecting to azure service bus using spring-cloud-azure-starter-servicebus-jms.

Ref but no luck .

Pom file :

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.4</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.3-SNAPSHOT</version>
    <name>springcloud-servicebus-1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure.spring</groupId>
                <artifactId>spring-cloud-azure-dependencies</artifactId>
                <version>4.6.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Java Code

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;

@SpringBootApplication
@EnableJms
public class Sample implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(Sample.class);
    private static final String TOPIC_NAME = "loan-app";
    private static final String SUBSCRIPTION_NAME = "loan-app-sub";

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        SpringApplication.run(Sample.class, args);
    }

    @Override
    public void run(String... args) {
        LOGGER.info("Sending message");
        jmsTemplate.convertAndSend(TOPIC_NAME, "Hello Word");
    }

    @JmsListener(destination = TOPIC_NAME, containerFactory = "topicJmsListenerContainerFactory", subscription = SUBSCRIPTION_NAME)
    public void receiveMessage(String message) {
        LOGGER.info("Message received: {}", message);
    }

}

Error :


APPLICATION FAILED TO START


Description:

Field jmsTemplate in com.example.demo.Sample required a bean of type 'org.springframework.jms.core.JmsTemplate' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)


Solution

  • Spring Cloud Azure to Spring Boot version mapping shows that:

    1. Spring Cloud Azure 4.x is compatible with Spring Boot 2.x.
    2. Spring Cloud Azure 5.x is compatible with Spring Boot 3.x.

    Now you are using Spring Boot 3.0.4 with Spring Cloud Azure 4.6.0. They are not compatible. You have 2 options:

    1. Option 1: Change spring-boot-starter-parent's version from 3.0.4 to 2.7.8.
    2. Option 2: Change spring-cloud-azure-dependencies' version from 4.6.0 to 5.0.0.