为 JMS 2.0 消费者设置 clientID

Setting clientID for JMS 2.0 consumer

对于 JMS 1.x,clientId 用于在创建持久订阅时唯一标识客户端。此 解释了 JMS 中的 clientId 用法 1.x

对于 JMS 2.x clientId 是可选的。我想了解在 JMS 2.x.

中提供 clientId 的优缺点

来自 Oracle article on JMS 2.x features:

Shared durable subscriptions. These are available in JMS 2.0 only and are created using createSharedDurableConsumer. They can have any number of consumers. Setting the client identifier is optional. The subscription is identified by the combination of the subscription name and the client identifier, if it is set.

听起来现在订阅名称是唯一标识符,但为什么会有clientID?这些是会话 class 上的新方法,因此不能向后兼容。使用 JMS 2.x 设置 clientId 的任何好处或缺点?

MessageConsumer messageConsumer = session.createSharedDurableConsumer(topic, "myDurableSub");

JMS 2 规范在第 6.1.3 节中指出了客户端 ID 存在的原因:

The only use of a client identifier defined by JMS is its mandatory use in identifying an unshared durable subscription or its optional use in identifying a shared durable or non-durable subscription.

关于共享非持久订阅,规范在第 8.3.3 节中这样说:

A shared non-durable subscription is identified by a name specified by the client and by the client identifier if set. If the client identifier was set when the shared non-durable subscription was first created then a client which subsequently wishes to create a consumer on that shared non-durable subscription must use the same client identifier.

规范在第 8.3.4 节中说了关于共享持久订阅的相同基本内容:

A shared durable subscription is identified by a name specified by the client and by the client identifier if set. If the client identifier was set when the shared durable subscription was first created then a client which subsequently wishes to create a consumer on that shared durable subscription must use the same client identifier.

通过使客户端标识符对于共享的持久和非持久订阅是可选的,这使得共享订阅更加方便,因为每个客户端只需要提供订阅名称,而不是同时提供客户端标识符和订阅名称。这符合 JMS 2 的总体主题,即简化 API 使 JMS 更易于使用。