【发布时间】:2021-03-26 04:15:12
【问题描述】:
我正在尝试在 Spring Boot 中使用 JMS 将消息推送到 Azure 服务总线主题。
正如你在下面看到的,我已经引入了 azure-servicebus-jms-spring-boot-starter 的依赖项
我已经在我的应用程序中配置了@EnableJms,我还在我的 application.yml 文件中设置了连接字符串,我已经验证它是正确的,我可以看到它正确连接到服务总线,如在这些日志 -
2020-12-15 13:36:18.431 INFO 4487 --- [windows.net:-1]] o.a.qpid.jms.sasl.SaslMechanismFinder : Best match for SASL auth was: SASL-PLAIN
2020-12-15 13:36:18.704 INFO 4487 --- [windows.net:-1]] org.apache.qpid.jms.JmsConnection : Connection ID:MY_CONNECTION connected to remote Broker: amqps://**-****-*****.servicebus.windows.net
设置此连接后,我会尝试使用我在下面指定的服务向我的服务总线上的主题发送消息,但是我在服务总线上没有看到任何事务。我已经验证目的地是正确的。
因为我没有在此应用程序上消费订阅,所以我没有指定 spring.jms.servicebus.topic-client-id=<ServiceBusSubscriptionID> 指定这样做 here
在我指定主题客户端 ID(一个不同的应用程序)的消费端,我创建了一个虚拟控制器来向服务总线发送示例消息,它工作正常,这让我相信我缺少一些配置在这个应用程序上。
但是我不需要在这里指定任何特定的主题客户端 ID,因为我只想推送到主题。获得服务总线连接字符串后,我应该能够将消息推送到我指定的任何主题 - jmsTemplate.convertAndSend(<TOPIC_NAME>, <MESSAGE>)
@Service
class MessageServiceImpl : MessageService {
private val logger: Logger = LoggerFactory.getLogger(this.javaClass)
@Autowired
lateinit var jmsTemplate: JmsTemplate
override fun sendMessage(topic: Topic, message: Message): Result<Unit> = Result {
logger.info("Sending message $message to topic ${topic.destination}")
jmsTemplate.convertAndSend(topic.destination, message)
}
}
enum class Topic(val destination: String) {
AWARDS("awards")
}
open class Message(
val eventTrigger: String,
val eventTriggeredBy: String,
val eventTimestamp: LocalDateTime,
val eventSourceSystem: String
)
jms:
servicebus:
connection-string: ${JMS_CONNECTION_STRING:''}
idle-timeout: 180000
compile 'com.microsoft.azure:azure-servicebus-jms-spring-boot-starter:2.2.5'
@SpringBootApplication
@EnableJms
class WebServiceApplication {
companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(WebServiceApplication::class.java, *args)
}
}
}
【问题讨论】:
-
你能分享一下日志中的错误吗?
JMS_CONNECTION_STRING值? -
嗨@Paizo,原来我发送的类没有实现可序列化,所以虽然它没有返回任何错误,但请求默默地失败了。然而,与使我的类实现可序列化相反,我将类编组为 json 字符串并将消息作为 json 字符串发送。
标签: spring-boot spring-jms azure-servicebus-topics spring-kotlin azure-spring-boot