您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.
向 Azure 服务总线队列发送消息并从中接收消息 (Java)Send messages to and receive messages from Azure Service Bus queues (Java)
11/09/2020
本文内容
在本快速入门中,你将创建一个 Java 应用,以便向 Azure 服务总线队列发送消息,并从中接收消息。In this quickstart, you'll create a Java app to send messages to and receive messages from an Azure Service Bus queue.
重要
本快速入门使用新的 azure-messaging-servicebus 包。This quickstart uses the new azure-messaging-servicebus package. 有关使用旧 azure-servicebus 包的快速入门,请参阅使用 azure-servicebus 发送和接收消息。For a quickstart that uses the old azure-servicebus package, see Send and receive messages using azure-servicebus.
先决条件Prerequisites
Azure 订阅。An Azure subscription. 若要完成本教程,需要一个 Azure 帐户。To complete this tutorial, you need an Azure account. You can activate your MSDN subscriber benefits or sign up for a free account.
如果没有可使用的队列,请遵循使用 Azure 门户创建服务总线队列一文来创建队列。If you don't have a queue to work with, follow steps in the Use Azure portal to create a Service Bus queue article to create a queue. 记下服务总线命名空间的连接字符串以及创建的队列的名称 。Note down the connection string for your Service Bus namespace and the name of the queue you created.
如果使用 Eclipse,则可安装 Azure Toolkit for Eclipse,其中包含用于 Java 的 Azure SDK。If you're using Eclipse, you can install the Azure Toolkit for Eclipse that includes the Azure SDK for Java. 然后,可将“适用于 Java 的 Microsoft Azure 库”添加到项目 。You can then add the Microsoft Azure Libraries for Java to your project.
向队列发送消息Send messages to a queue
在本部分中,你将创建一个 Java 控制台项目,并添加代码以将消息发送到之前创建的队列。In this section, you'll create a Java console project, and add code to send messages to the queue that you created earlier.
创建 Java 控制台项目Create a Java console project
使用 Eclipse 或所选工具创建 Java 项目。Create a Java project using Eclipse or a tool of your choice.
配置应用程序以使用服务总线Configure your application to use Service Bus
添加对 Azure 服务总线库的引用。Add a reference to Azure Service Bus library. Maven 中心存储库中提供了服务总线的 Java 客户端库。The Java client library for Service Bus is