将 Java 消息服务 2.0 API 与 Azure 服务总线高级版配合使用(预览版)Use Java Message Service 2.0 API with Azure Service Bus Premium (Preview)

本文介绍如何使用热门的 Java 消息服务 (JMS) 2.0 API 通过高级消息队列协议 (AMQP 1.0) 协议与 Azure 服务总结交互。This article explains how to use the popular Java Message Service (JMS) 2.0 API to interact with Azure Service Bus over the Advanced Message Queueing Protocol (AMQP 1.0) protocol.

备注

对 Java 消息服务 (JMS) 2.0 API 的支持只在 Azure 服务总线高级层提供,且目前是预览版 。Support for Java Message Service (JMS) 2.0 API is only available on the Azure Service Bus Premium tier and is currently in preview.

服务总线入门Get started with Service Bus

此指南假定已有服务总线命名空间。This guide assumes that you already have a Service Bus namespace. 如果没有,则可以使用 Azure 经典门户创建命名空间和队列If you don't, then you can create the namespace and queue using the Azure portal.

若要详细了解如何创建服务总线命名空间和队列,请参阅通过 Azure 门户开始使用服务总线队列For more information about how to create Service Bus namespaces and queues, see Get started with Service Bus queues through the Azure portal.

支持哪些 JMS 功能?What JMS features are supported?

下表列出了 Azure 服务总线当前支持的 Java 消息服务 (JMS) 功能。The following table lists the Java Message Service (JMS) features that Azure Service Bus currently supports. 它还展示了不支持的功能。It also shows features that are unsupported.

功能Feature APIAPI 状态Status
队列Queues
  • JMSContext.createQueue(String queueName)JMSContext.createQueue( String queueName)
支持Supported
主题Topics
  • JMSContext.createTopic(String topicName)JMSContext.createTopic( String topicName)
支持Supported
临时队列Temporary queues
  • JMSContext.createTemporaryQueue()JMSContext.createTemporaryQueue()
支持Supported
临时主题Temporary topics
  • JMSContext.createTemporaryTopic()JMSContext.createTemporaryTopic()
支持Supported
消息生成者/Message Producer /
JMSProducerJMSProducer
  • JMSContext.createProducer()JMSContext.createProducer()
支持Supported
队列浏览器Queue browsers
  • JMSContext.createBrowser(Queue queue)JMSContext.createBrowser(Queue queue)
  • JMSContext.createBrowser(Queue queue, String messageSelector)JMSContext.createBrowser(Queue queue, String messageSelector)
支持Supported
消息使用者/Message Consumer/
JMSConsumerJMSConsumer
  • JMSContext.createConsumer(Destination destination)JMSContext.createConsumer( Destination destination)
  • JMSContext.createConsumer(Destination destination, String messageSelector)JMSContext.createConsumer( Destination destination, String messageSelector)
  • JMSContext.createConsumer(Destination destination, String messageSelector, boolean noLocal)JMSContext.createConsumer( Destination destination, String messageSelector, boolean noLocal)

目前不支持 noLocalnoLocal is currently not supported
支持Supported
共享持久订阅Shared durable subscriptions
  • JMSContext.createSharedDurableConsumer(Topic topic, String name)JMSContext.createSharedDurableConsumer(Topic topic, String name)
  • JMSContext.createSharedDurableConsumer(Topic topic, String name, String messageSelector)JMSContext.createSharedDurableConsumer(Topic topic, String name, String messageSelector)
支持Supported
非共享持久订阅Unshared durable subscriptions
  • JMSContext.createDurableConsumer(Topic topic, String name)JMSContext.createDurableConsumer(Topic topic, String name)
  • createDurableConsumer(Topic topic, String name, String messageSelector, boolean noLocal)createDurableConsumer(Topic topic, String name, String messageSelector, boolean noLocal)

目前不支持 noLocal,应将其设置为 falsenoLocal is currently not supported and should be set to false
支持Supported
共享非持久订阅Shared non-durable subscriptions
  • JMSContext.createSharedConsumer(Topic topic, String sharedSubscriptionName)JMSContext.createSharedConsumer(Topic topic, String sharedSubscriptionName)
  • JMSContext.createSharedConsumer(Topic topic, String sharedSubscriptionName, String messageSelector)JMSContext.createSharedConsumer(Topic topic, String sharedSubscriptionName, String messageSelector)
支持Supported
非共享非持久订阅Unshared non-durable subscriptions
  • JMSContext.createConsumer(Destination destination)JMSContext.createConsumer(Destination destination)
  • JMSContext.createConsumer(Destination destination, String messageSelector)JMSContext.createConsumer( Destination destination, String messageSelector)
  • JMSContext.createConsumer(Destination destination, String messageSelector, boolean noLocal)JMSContext.createConsumer( Destination destination, String messageSelector, boolean noLocal)

目前不支持 noLocal,应将其设置为 falsenoLocal is currently not supported and should be set to false
支持Supported
消息选择器Message selectors 取决于创建的使用者depends on the consumer created 支持Supported
交付延迟(计划的消息)Delivery Delay (scheduled messages)
  • JMSProducer.setDeliveryDelay(long deliveryDelay)JMSProducer.setDeliveryDelay( long deliveryDelay)
支持Supported
创建的消息Message created
  • JMSContext.createMessage()JMSContext.createMessage()
  • JMSContext.createBytesMessage()JMSContext.createBytesMessage()
  • JMSContext.createMapMessage()JMSContext.createMapMessage()
  • JMSContext.createObjectMessage(Serializable object)JMSContext.createObjectMessage( Serializable object)
  • JMSContext.createStreamMessage()JMSContext.createStreamMessage()
  • JMSContext.createTextMessage()JMSContext.createTextMessage()
  • JMSContext.createTextMessage(String text)JMSContext.createTextMessage( String text)
支持Supported
分布式事务Distributed transactions 不支持Not supported

下载 Java 消息服务 (JMS) 客户端库Downloading the Java Message Service (JMS) client library

若要利用 Azure 服务总线高级层上提供的所有功能,必须将下面的库添加到项目的生成路径。To utilize all the features available on Azure Service Bus Premium tier, the below library must be added to the build path of the project.

Azure-servicebus-jmsAzure-servicebus-jms

备注

若要将 Azure-servicebus-jms 添加到生成路径,请利用项目的首选依赖项管理工具,如 MavenGradleTo add the Azure-servicebus-jms to the build path, utilize the preferred dependency management tool for your project like Maven or Gradle.

为 Java 应用程序编码Coding Java applications

导入依赖项后,可以采用独立于 JMS 提供程序的方式编写 Java 应用程序。Once the dependencies have been imported, the Java applications can be written in a JMS provider agnostic manner.

使用 JMS 连接到 Azure 服务总线Connecting to Azure Service Bus using JMS

若要使用 JMS 客户端与 Azure 服务总线连接,需要连接字符串,该字符串在 Azure 门户中“共享访问策略”中的“主连接字符串”下提供 。To connect with Azure Service Bus using JMS clients, you need the connection string that is available in the 'Shared Access Policies' in the Azure portal under Primary Connection String.

  1. 实例化 ServiceBusJmsConnectionFactorySettingsInstantiate the ServiceBusJmsConnectionFactorySettings

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

    String ServiceBusConnectionString = "<SERVICE_BUS_CONNECTION_STRING_WITH_MANAGE_PERMISSIONS>";
    ConnectionFactory factory = new ServiceBusJmsConnectionFactory(ServiceBusConnectionString, connFactorySettings);
    
  3. 使用 ConnectionFactory 创建 Connection,然后创建 SessionUse the ConnectionFactory to either create a Connection and then a Session

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

    或者创建 JMSContext(用于 JMS 2.0 客户端)or a JMSContext (for JMS 2.0 clients)

    JMSContext jmsContext = factory.createContext();
    

编写 JMS 应用程序Write the JMS application

SessionJMSContext 经过实例化后,对于管理和数据这两方面的操作,应用程序都可以使用熟悉的 JMS API 来执行。Once the Session or JMSContext has been instantiated, your application can use the familiar JMS APIs to perform both management and data operations.

请参阅支持的 JMS 功能的列表,以了解哪些 API 在此预览版中受支持。Refer to the list of supported JMS features to see which APIs are supported as part of this preview.

下面是一些用于开始使用 JMS 的示例代码片段 -Below are some sample code snippets to get started with JMS -

将消息发送到队列和主题Sending messages to a queue and topic

// 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);

从队列接收消息Receiving messages from a queue

// 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();

从针对某主题的共享持久订阅接收消息Receiving messages from a shared durable subscription on a topic

// 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();

“摘要”Summary

本指南展示了 Java 客户端应用程序如何使用 Java 消息服务 (JMS) 通过 AMQP 1.0 实现与 Azure 服务总线交互。This guide showcased how Java client applications using Java Message Service (JMS) over AMQP 1.0 can interact with Azure Service Bus.

也可以通过其他语言(包括 .NET、C、Python 和 PHP)使用 Service Bus AMQP 1.0。You can also use Service Bus AMQP 1.0 from other languages, including .NET, C, Python, and PHP. 使用这些不同语言构建的组件可以使用服务总线中的 AMQP 1.0 支持可靠且完全无损地交换消息。Components built using these different languages can exchange messages reliably and at full fidelity using the AMQP 1.0 support in Service Bus.

后续步骤Next steps

若要详细了解 Azure 服务总线以及 Java 消息服务 (JMS) 实体,请查看以下链接 -For more information on Azure Service Bus and details about Java Message Service (JMS) entities, check out the links below -