你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将 Java 消息服务 2.0 API 与 Azure 服务总线高级版配合使用

本文介绍如何使用热门的 Java 消息服务 (JMS) 2.0 API 通过高级消息队列协议 (AMQP 1.0) 协议与 Azure 服务总结交互。

注意

仅高级层支持 Java 消息服务 (JMS) 2.0 API。>

先决条件

服务总线入门

此指南假定已有服务总线命名空间。 如果没有,则请使用 Azure 门户创建命名空间和队列。 若要详细了解如何创建服务总线命名空间和队列,请参阅通过 Azure 门户开始使用服务总线队列

设置 Java 开发环境

若要开发 Java 应用程序,需要设置适当的开发环境 -

  • 安装 JDK(Java 开发工具包)或 JRE (Java Runtime Environment)。
  • 在生成路径和适当的系统变量中添加 JDK 或 JRE。
  • 安装 Java IDE 以利用 JDK 或 JRE。 例如 Eclipse 或 IntelliJ。

若要详细了解如何在 Azure 上准备适用于 Java 的开发人员环境,请使用本指南

支持哪些 JMS 功能?

下表列出了 Azure 服务总线当前支持的 Java 消息服务 (JMS) 功能。 它还展示了不支持的功能。

功能 API 状态
队列
  • JMSContext.createQueue(String queueName)
支持
主题
  • JMSContext.createTopic(String topicName)
支持
临时队列
  • JMSContext.createTemporaryQueue()
支持
临时主题
  • JMSContext.createTemporaryTopic()
支持
消息生成者/
JMSProducer
  • JMSContext.createProducer()
支持
队列浏览器
  • JMSContext.createBrowser(Queue queue)
  • JMSContext.createBrowser(Queue queue, String messageSelector)
支持
消息使用者/
JMSConsumer
  • JMSContext.createConsumer(Destination destination)
  • JMSContext.createConsumer(Destination destination, String messageSelector)
  • JMSContext.createConsumer(Destination destination, String messageSelector, boolean noLocal)

目前不支持 noLocal
支持
共享持久订阅
  • JMSContext.createSharedDurableConsumer(Topic topic, String name)
  • JMSContext.createSharedDurableConsumer(Topic topic, String name, String messageSelector)
支持
非共享持久订阅
  • JMSContext.createDurableConsumer(Topic topic, String name)
  • createDurableConsumer(Topic topic, String name, String messageSelector, boolean noLocal)

目前不支持 noLocal,应将其设置为 false
支持
共享非持久订阅
  • JMSContext.createSharedConsumer(Topic topic, String sharedSubscriptionName)
  • JMSContext.createSharedConsumer(Topic topic, String sharedSubscriptionName, String messageSelector)
支持
非共享非持久订阅
  • JMSContext.createConsumer(Destination destination)
  • JMSContext.createConsumer(Destination destination, String messageSelector)
  • JMSContext.createConsumer(Destination destination, String messageSelector, boolean noLocal)

目前不支持 noLocal,应将其设置为 false
支持
消息选择器 取决于创建的使用者 支持
交付延迟(计划的消息)
  • JMSProducer.setDeliveryDelay(long deliveryDelay)
支持
创建的消息
  • JMSContext.createMessage()
  • JMSContext.createBytesMessage()
  • JMSContext.createMapMessage()
  • JMSContext.createObjectMessage(Serializable object)
  • JMSContext.createStreamMessage()
  • JMSContext.createTextMessage()
  • JMSContext.createTextMessage(String text)
支持
跨实体事务
  • Connection.createSession(true, Session.SESSION_TRANSACTED)
支持
分布式事务 不支持

下载 Java 消息服务 (JMS) 客户端库

若要利用高级层中提供的所有功能,请将以下库添加到项目的生成路径。

Azure-servicebus-jms

注意

若要将 Azure-servicebus-jms 添加到生成路径,请使用项目的首选依赖项管理工具,如 MavenGradle

为 Java 应用程序编码

导入依赖项后,可以采用独立于 JMS 提供程序的方式编写 Java 应用程序。

使用 JMS 连接到 Azure 服务总线

若要使用 JMS 客户端与 Azure 服务总线连接,需要连接字符串,该字符串在 Azure 门户中“共享访问策略”中的“主连接字符串”下提供 。

  1. 实例化 ServiceBusJmsConnectionFactorySettings

    ServiceBusJmsConnectionFactorySettings connFactorySettings = new ServiceBusJmsConnectionFactorySettings();
    connFactorySettings.setConnectionIdleTimeoutMS(20000);
    
  2. 使用相应的 ServiceBusConnectionString 实例化 ServiceBusJmsConnectionFactory

    String ServiceBusConnectionString = "<SERVICE_BUS_CONNECTION_STRING_WITH_MANAGE_PERMISSIONS>";
    ConnectionFactory factory = new ServiceBusJmsConnectionFactory(ServiceBusConnectionString, connFactorySettings);
    
  3. 使用 ConnectionFactory 创建 Connection,然后创建 Session

    Connection connection = factory.createConnection();
    Session session = connection.createSession();
    

    或者创建 JMSContext(用于 JMS 2.0 客户端)

    JMSContext jmsContext = factory.createContext();
    

    重要

    虽然 JMS“会话”和服务总线“会话”的名称相似,但它们完全不相关。

    在 JMS 1.1 中,会话是允许创建 MessageProducerMessageConsumerMessage 本身的 API 的基本构建基块。 有关详细信息,请参阅 JMS API 编程模型

    在服务总线中,会话是对队列和订阅实现 FIFO 处理的服务和客户端构造。

编写 JMS 应用程序

SessionJMSContext 经过实例化后,对于管理和数据这两方面的操作,应用程序都可以使用熟悉的 JMS API 来执行。 请参阅支持的 JMS 功能的列表,以了解哪些 API 受支持。 下面是一些用于开始使用 JMS 的示例代码片段 -

将消息发送到队列和主题

// Create the queue and topic
Queue queue = jmsContext.createQueue("basicQueue");
Topic topic = jmsContext.createTopic("basicTopic");
// Create the message
Message msg = jmsContext.createMessage();

// Create the JMS message producer
JMSProducer producer = jmsContext.createProducer();

// send the message to the queue
producer.send(queue, msg);
// send the message to the topic
producer.send(topic, msg);

从队列接收消息

// Create the queue
Queue queue = jmsContext.createQueue("basicQueue");

// Create the message consumer
JMSConsumer consumer = jmsContext.createConsumer(queue);

// Receive the message
Message msg = (Message) consumer.receive();

从针对某主题的共享持久订阅接收消息

// Create the topic
Topic topic = jmsContext.createTopic("basicTopic");

// Create a shared durable subscriber on the topic
JMSConsumer sharedDurableConsumer = jmsContext.createSharedDurableConsumer(topic, "sharedDurableConsumer");

// Receive the message
Message msg = (Message) sharedDurableConsumer.receive();

“摘要”

本指南展示了 Java 客户端应用程序如何使用 Java 消息服务 (JMS) 通过 AMQP 1.0 实现与 Azure 服务总线交互。

也可以通过其他语言(包括 .NET、C、Python 和 PHP)使用 Service Bus AMQP 1.0。 使用这些不同语言构建的组件可以使用服务总线中的 AMQP 1.0 支持可靠且完全无损地交换消息。

后续步骤

若要详细了解 Azure 服务总线以及 Java 消息服务 (JMS) 实体,请查看以下链接 -