我在这里问这个问题是为了获得 Microsoft 或社区的帮助
我想使用 Spring Boot 应用程序向主题发送消息
蔚蓝页面是服务巴士
我的应用程序属性
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
错误:
原因:java.lang.IllegalArgumentException:无法订阅 空。
我的代码有依赖管理,spring-cloud-azure-starter-servicebus
页面中提到的配置类 - ServiceBusProcessorClientConfiguration
和引导类 - ServiceBusQueueApplication
(我更喜欢在两个不同的应用程序中拥有发送者和接收者。不像同一个程序中的 PING-PONG。可能我想在逻辑应用程序中查看/使用消息)
我错过了什么?如何发送带有属性的消息(见附件)
编辑:
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>
我尝试了下面的示例 Spring Boot 代码将消息发送到 Azure 服务总线主题。
代码:
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.Bean;
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;
@Bean
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();
}
@Bean
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(ServiceBusProcessorClient serviceBusProcessorClient, ServiceBusSenderClient serviceBusSenderClient) {
this.serviceBusProcessorClient = serviceBusProcessorClient;
this.serviceBusSenderClient = 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>
本地输出:
邮递员输出:
我使用Postman将消息发送到Azure Service Bus,如下:
http://localhost:8080/sendMessage
{
"message": "Hi Kamali.Welcome!"
}
Azure 门户:
消息已成功发送到Azure服务总线,如下所示。