您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.
用于 Java 的服务总线库Service Bus libraries for Java
07/11/2017
本文内容
概述Overview
服务总线是一个企业级的事务消息传送平台服务,提供高度可靠的队列和发布/订阅主题,并具有依序传送、会话、分区、计划、复杂订阅以及工作流和事务处理等高级功能。Service Bus is an enterprise-class, transactional messaging platform service that provides highly reliable queues and publish/subscribe topics with deep feature capabilities such as ordered delivery, sessions, partitioning, scheduling, complex subscriptions, as well as workflow and transaction handling.
服务总线的功能与传统的高端本地消息中转站相当,并且往往优于后者。The Service Bus feature capabilities are comparable and often exceed those of high-end, on-premises legacy message brokers. 可通过 AMQP 1.0、HTTPS 等基于标准的协议来使用服务总线功能,所有协议手势均有完备的阐述,从而可以实现广泛的互操作性。The Service Bus features are available via standards-based protocols like AMQP 1.0 and HTTPS and all protocol gestures are fully documented, allowing for broad interoperability.
服务总线高级版注重高度可用和可靠的持久消息传送,即使在大规模的本地数据中心部署中也能提供有竞争力的吞吐量性能,同时可以消除硬件选用和采购流程、部署规划和执行,以及漫长的性能优化会议。Focusing on highly available and reliable durable messaging, the Service Bus Premium provides competitive throughput performance even with substantial local datacenter deployments, but without hardware selection and acquisition processes, deployment planning and execution, and endless performance optimization sessions.
服务总线高级版是完全托管型的产品,它采用以容量为导向的简单定价模型为每个租户保留专用容量实现可预测的性能,同时与商用本地中转站相比,总体成本极低。Service Bus Premium is a fully managed offering with dedicated capacity reserved for each tenant that yields predictable performance with a simple, capacity-oriented pricing model and at extremely lower overall cost than commercial on-premises brokers. 对于许多客户而言,服务总线高级版可以取代眼下的专用本地消息传送群集,即使附加的工作负荷不在云中运行。For many customers, Service Bus Premium can replace dedicated on-premises messaging clusters today, even if the attached workloads do not run in the cloud.
在消息传送文档部分中详细了解服务总线的概念
对于 Java 开发人员而言,服务总线提供 Microsoft 支持的本机 API,服务总线还能与符合 AMQP 1.0 规范的库(例如 Apache Qpid Proton 的 JMS 提供程序)配合使用。For Java developers, Service Bus provides a Microsoft supported native API and Service Bus can also be used with AMQP 1.0 compliant libraries such as Apache Qpid Proton's JMS provider.
客户端库Client library
The official Service Bus client is available in source code form on GitHub and binaries and packaged sources are available on Maven Central.
示例代码存储库包含的示例用于演示:The sample code repository contains samples for:
向 Maven 项目的 pom.xml 文件中添加依赖项,以便在自己的项目中使用库。Add a dependency to your Maven project's pom.xml file to use the library in your own project. 根据需要指定版本。Specify the version as desired.
向 Maven pom.xml 文件中添加依赖项,以便在项目中使用客户端库。Add a dependency to your Maven pom.xml file to use the client library in your project.
com.microsoft.azure
azure-servicebus
1.0.0
public class BasicSendReceiveWithQueueClient {
// Connection String for the namespace can be obtained from the Azure portal under the
// 'Shared Access policies' section.
private static final String connectionString = "{connection string}";
private static final String queueName = "{queue name}";
private static IQueueClient queueClient;
private static int totalSend = 100;
private static int totalReceived = 0;
public static void main(String[] args) throws Exception {
Log.log("Starting BasicSendReceiveWithQueueClient sample");
// create client
Log.log("Create queue client.");
queueClient = new QueueClient(new ConnectionStringBuilder(connectionString, queueName), ReceiveMode.PeekLock);
// send and receive
queueClient.registerMessageHandler(new MessageHandler(queueClient), new MessageHandlerOptions(1, false, Duration.ofMinutes(1)));
for (int i = 0; i < totalSend; i++) {
int j = i;
Log.log("Sending message #%d.", j);
queueClient.sendAsync(new Message("" + i)).thenRunAsync(() -> { Log.log("Sent message #%d.", j);});
}
while(totalReceived != totalSend) {
Thread.sleep(1000);
}
Log.log("Received all messages, exiting the sample.");
Log.log("Closing queue client.");
queueClient.close();
}
static class MessageHandler implements IMessageHandler {
private IQueueClient client;
public MessageHandler(IQueueClient client) {
this.client = client;
}
@Override
public CompletableFuture onMessageAsync(IMessage iMessage) {
Log.log("Received message with sq#: %d and lock token: %s.", iMessage.getSequenceNumber(), iMessage.getLockToken());
return this.client.completeAsync(iMessage.getLockToken()).thenRunAsync(() -> {
Log.log("Completed message sq#: %d and locktoken: %s", iMessage.getSequenceNumber(), iMessage.getLockToken());
totalReceived++;
});
}
@Override
public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
Log.log(exceptionPhase + "-" + throwable.getMessage());
}
}
}
管理 APIManagement API
使用管理 API 创建和管理命名空间、主题、队列与订阅。Create and manage namespaces, topics, queues, and subscriptions with the management API.
请查看下面的一些示例:Please find some examples here:
在项目中使用管理 API:
\Use the Management API in your project:
\
向 Maven pom.xml 文件中添加依赖项,以便在项目中使用管理 API。Add a dependency to your Maven pom.xml file to use the management API in your project.
com.microsoft.azure
azure-mgmt-servicebus
1.3.0