skip to Main Content

I am asking this question here to get help from Microsoft or community

I want to send messages to a topic using spring boot application

The azure page is service bus

My app properties

spring.cloud.azure.servicebus.namespace=<<some name>> 
spring.cloud.azure.servicebus.entity-name=<<some topic>> 

spring.cloud.azure.servicebus.processor.subscription-name=<<somesub>> 

spring.cloud.azure.servicebus.entity-type=topic

Error:

Caused by: java.lang.IllegalArgumentException: Subscription cannot be
null.

My code has the dependency management , spring-cloud-azure-starter-servicebus

The config class as mentioned in the pages – ServiceBusProcessorClientConfiguration

and the bootstrap class – ServiceBusQueueApplication

(I prefer to have a sender and a receiver in 2 different apps. Not like PING-PONG in the same program. May be I want to see/consume message in a logic app )

what am I missing ? How to send message with properties (see attached)

EDIT:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceBusReceiverClientBuilder' defined in class path resource [com/azure/spring/cloud/autoconfigure/implementation/servicebus/AzureServiceBusConsumerClientConfiguration$NoneSessionConsumerClientConfiguration.class]: Failed to instantiate [com.azure.messaging.servicebus.ServiceBusClientBuilder$ServiceBusReceiverClientBuilder]: Factory method 'serviceBusReceiverClientBuilder' threw exception with message: Subscription cannot be null.
    
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.azure.messaging.servicebus.ServiceBusClientBuilder$ServiceBusReceiverClientBuilder]: Factory method 'serviceBusReceiverClientBuilder' threw exception with message: Subscription cannot be null.
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:177) ~[spring-beans-6.1.6.jar:6.1.6]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644) ~[spring-beans-6.1.6.jar:6.1.6]
    ... 18 common frames omitted
Caused by: java.lang.IllegalArgumentException: Subscription cannot be null.
    at org.springframework.util.Assert.notNull(Assert.java:172) ~[spring-core-6.1.6.jar:6.1.6]
    at com.azure.spring.cloud.service.implementation.servicebus.factory.ServiceBusReceiverClientBuilderFactory.configureService(ServiceBusReceiverClientBuilderFactory.java:54) ~[spring-cloud-azure-service-5.11.0.jar:5.11.0]
    at com.azure.spring.cloud.service.implementation.servicebus.factory.ServiceBusReceiverClientBuilderFactory.configureService(ServiceBusReceiverClientBuilderFactory.java:15) ~[spring-cloud-azure-service-5.11.0.jar:5.11.0]
    at com.azure.spring.cloud.core.implementation.factory.AbstractAzureServiceClientBuilderFactory.build(AbstractAzureServiceClientBuilderFactory.java:128) ~[spring-cloud-azure-core-5.11.0.jar:5.11.0]
    at com.azure.spring.cloud.autoconfigure.implementation.servicebus.AzureServiceBusConsumerClientConfiguration$NoneSessionConsumerClientConfiguration.serviceBusReceiverClientBuilder(AzureServiceBusConsumerClientConfiguration.java:70) ~[spring-cloud-azure-autoconfigure-5.11.0.jar:5.11.0]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140) ~[spring-beans-6.1.6.jar:6.1.6]
    ... 19 common frames omitted

POM:

<?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.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>azureservicebus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>azureservicebus</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud-azure.version>5.11.0</spring-cloud-azure.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-servicebus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure.spring</groupId>
                <artifactId>spring-cloud-azure-dependencies</artifactId>
                <version>${spring-cloud-azure.version}</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>

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Microsoft has provided an answer and updated the documentation

    Add this property and the service bus topic works

    spring.cloud.azure.servicebus.consumer.subscription-name=<same as processor subscription>
    

    and about filter messages, send data with application properties and the rule works

    message.getApplicationProperties().put("status", data.getStatus());
    senderClient.sendMessage(message);
    

  2. I tried the sample Spring Boot code below to send messages to the Azure Service Bus Topic.

    Code :

    ServiceBusConfig.java :

    import com.azure.messaging.servicebus.ServiceBusClientBuilder;
    import com.azure.messaging.servicebus.ServiceBusProcessorClient;
    import com.azure.messaging.servicebus.ServiceBusSenderClient;
    import com.azure.messaging.servicebus.models.ServiceBusReceiveMode;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class ServiceBusConfig {
    
        @Value("${servicebus.connectionString}")
        private String connectionString;
    
        @Value("${servicebus.topic}")
        private String topic;
    
        @Value("${servicebus.subscription}")
        private String subscription;
    
        public ServiceBusProcessorClient serviceBusProcessorClient() {
            return new ServiceBusClientBuilder()
                    .connectionString(connectionString)
                    .processor()
                    .topicName(topic)
                    .subscriptionName(subscription)
                    .receiveMode(ServiceBusReceiveMode.PEEK_LOCK)
                    .processMessage(context -> {
                        System.out.printf("Received message from %s, %s: %s%n", topic, subscription, context.getMessage().getBody().toString());
                        context.complete();
                    })
                    .processError(context -> {
                        System.err.printf("Error occurred: %s%n", context.getException());
                    })
                    .buildProcessorClient();
        }
    
        public ServiceBusSenderClient serviceBusSenderClient() {
            return new ServiceBusClientBuilder()
                    .connectionString(connectionString)
                    .sender()
                    .topicName(topic)
                    .buildClient();
        }
    }
    

    ServiceBusController.java :

    import com.azure.messaging.servicebus.ServiceBusMessage;
    import com.azure.messaging.servicebus.ServiceBusProcessorClient;
    import com.azure.messaging.servicebus.ServiceBusSenderClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ServiceBusController {
    
        private final ServiceBusProcessorClient serviceBusProcessorClient;
        private final ServiceBusSenderClient serviceBusSenderClient;
    
        @Autowired
        public ServiceBusController(ServiceBusConfig serviceBusConfig) {
            this.serviceBusProcessorClient = serviceBusConfig.serviceBusProcessorClient();
            this.serviceBusSenderClient = serviceBusConfig.serviceBusSenderClient();
        }
    
        @PostMapping("/sendMessage")
        public String sendMessage(@RequestBody String message) {
            serviceBusSenderClient.sendMessage(new ServiceBusMessage(message));
            return "Message sent to Service Bus topic: " + message;
        }
    }
    

    pom.xml :

    <dependencies>
         <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
              <groupId>com.azure.spring</groupId>
              <artifactId>spring-cloud-azure-dependencies</artifactId>
              <version>5.12.0</version>
              <type>pom</type>
              <scope>import</scope>
              </dependency>
         <dependency>
              <groupId>com.azure</groupId>
              <artifactId>azure-messaging-servicebus</artifactId>
              <version>7.16.0</version>
         </dependency>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
         </dependency>
    </dependencies>
    

    application.properties :

    servicebus.connectionString=<connec_string>
    servicebus.topic=<topic_name>
    servicebus.subscription=<subscription_name>
    

    Local Output :

    enter image description here
    enter image description here

    Postman Output :

    I sent the message to the Azure Service Bus using Postman, as follows:

    http://localhost:8080/sendMessage
    
    {
    "message": "Hi Kamali.Welcome!"
    }
    

    enter image description here

    Azure Portal :

    The message was sent successfully to the Azure Service Bus, as shown below.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search