TopicSession (Java EE 5)

Java EE API


javax.jms Interface TopicSession

All Superinterfaces:
Runnable, Session

public interface TopicSession
extends Session

Implements: Session

TopicSession 对象提供创建 TopicPublisherTopicSubscriberTemporaryTopic 对象的方法。它还提供 删除其客户端持久订阅者的方法。

TopicSession 用于创建特定于 Pub/Sub 的对象。一般情况下,使用 Session 对象,而 使用 TopicSession 只是为了支持 现有代码。使用 Session 对象可简化 编程模型,并允许跨两个消息传送域 使用事务。

不能使用 TopicSession 创建特定于 点对点域的对象。下列方法继承自 Session,但如果在 TopicSession 中使用,则必定抛出 IllegalStateException

  • createBrowser
  • createQueue
  • createTemporaryQueue
英文文档:

A TopicSession object provides methods for creating TopicPublisher, TopicSubscriber, and TemporaryTopic objects. It also provides a method for deleting its client's durable subscribers.

A TopicSession is used for creating Pub/Sub specific objects. In general, use the Session object, and use TopicSession only to support existing code. Using the Session object simplifies the programming model, and allows transactions to be used across the two messaging domains.

A TopicSession cannot be used to create objects specific to the point-to-point domain. The following methods inherit from Session, but must throw an IllegalStateException if used from TopicSession:

  • createBrowser
  • createQueue
  • createTemporaryQueue

Version:
1.1 - April 9, 2002
Author:
Mark Hapner, Rich Burridge, Kate Stout
See Also:
Session, Connection.createSession(boolean, int), TopicConnection.createTopicSession(boolean, int), XATopicSession.getTopicSession()

Field Summary
 
Fields inherited from interface javax.jms.Session
 
Method Summary
 TopicSubscriber
 TopicSubscriber
 TopicPublisher
 TopicSubscriber
 TopicSubscriber
 TemporaryTopic
 Topic
 void
 
Methods inherited from interface javax.jms.Session
 

Method Detail

public Topic createTopic(String topicName) throws JMSException
创建给定 Topic 名称的主题身份。

此工具供某些特殊情况使用,这些情况下客户端需要动态操作主题身份。这允许创建具有特定于提供者的名称的主题身份。依赖于此能力的客户端是不可移植的。

注意,此方法不用于创建物理主题。物理创建主题是一项管理任务,不应由 JMS API 发起。有一个例外是创建临时主题,它可以使用 createTemporaryTopic 方法实现。

topicNameTopic 的名称
return 具有给定名称的 Topic
ThrowsJMSException: 如果会话由于某个内部错误无法创建主题。

英文文档:

createTopic

Topic createTopic(String topicName)
                  throws JMSException
Creates a topic identity given a Topic name.

This facility is provided for the rare cases where clients need to dynamically manipulate topic identity. This allows the creation of a topic identity with a provider-specific name. Clients that depend on this ability are not portable.

Note that this method is not for creating the physical topic. The physical creation of topics is an administrative task and is not to be initiated by the JMS API. The one exception is the creation of temporary topics, which is accomplished with the createTemporaryTopic method.

Specified by:
createTopic in interface Session
Parameters:
topicName - the name of this Topic
Returns:
a Topic with the given name
Throws:
JMSException - if the session fails to create a topic due to some internal error.

public TopicSubscriber createSubscriber(Topic topic) throws JMSException
创建指定主题的非持久订阅者。

客户端使用 TopicSubscriber 对象接收 已发布到主题的消息。

常规 TopicSubscriber 对象不是持久的。 它们仅接收在其活动状态下发布的消息。

在某些情况下,连接可以既发布又订阅 主题。订阅者 NoLocal 属性允许订阅者 禁止传送由其自己的连接发布的消息。 此属性的默认值为 false。

topic 要订阅的 Topic
ThrowsJMSException: 如果会话由于某个内部错误无法创建订阅者。
ThrowsInvalidDestinationException: 如果指定了无效的主题。

英文文档:

createSubscriber

TopicSubscriber createSubscriber(Topic topic)
                                 throws JMSException
Creates a nondurable subscriber to the specified topic.

A client uses a TopicSubscriber object to receive messages that have been published to a topic.

Regular TopicSubscriber objects are not durable. They receive only messages that are published while they are active.

In some cases, a connection may both publish and subscribe to a topic. The subscriber NoLocal attribute allows a subscriber to inhibit the delivery of messages published by its own connection. The default value for this attribute is false.

Parameters:
topic - the Topic to subscribe to
Throws:
JMSException - if the session fails to create a subscriber due to some internal error.
InvalidDestinationException - if an invalid topic is specified.

public TopicSubscriber createSubscriber(Topic topic, String messageSelector, boolean noLocal) throws JMSException
创建指定主题的非持久订阅者, 使用消息选择器或指定是否应将由其自己的连接发布的 消息传送给它。

客户端使用 TopicSubscriber 对象接收 已发布到主题的消息。

常规 TopicSubscriber 对象不是持久的。 它们仅接收在其活动状态下发布的消息。

订阅者的消息选择器过滤掉的消息将 绝不会传送给订阅者。从订阅者的角度来看, 这些消息不存在。

在某些情况下,连接可以既发布又订阅 主题。订阅者 NoLocal 属性允许订阅者 禁止传送由其自己的连接发布的消息。 此属性的默认值为 false。

topic 要订阅的 Topic
messageSelector 仅传送属性 与消息选择器表达式相匹配的消息。值为 null 或 空字符串表示该消息使用方没有消息选择器 。
noLocal 如果设置,则禁止传送由其自己的连接发布的消息
ThrowsJMSException: 如果会话由于某个内部错误无法创建订阅者。
ThrowsInvalidDestinationException: 如果指定了无效的主题。
ThrowsInvalidSelectorException: 如果消息选择器无效。

英文文档:

createSubscriber

TopicSubscriber createSubscriber(Topic topic,
                                 String messageSelector,
                                 boolean noLocal)
                                 throws JMSException
Creates a nondurable subscriber to the specified topic, using a message selector or specifying whether messages published by its own connection should be delivered to it.

A client uses a TopicSubscriber object to receive messages that have been published to a topic.

Regular TopicSubscriber objects are not durable. They receive only messages that are published while they are active.

Messages filtered out by a subscriber's message selector will never be delivered to the subscriber. From the subscriber's perspective, they do not exist.

In some cases, a connection may both publish and subscribe to a topic. The subscriber NoLocal attribute allows a subscriber to inhibit the delivery of messages published by its own connection. The default value for this attribute is false.

Parameters:
topic - the Topic to subscribe to
messageSelector - only messages with properties matching the message selector expression are delivered. A value of null or an empty string indicates that there is no message selector for the message consumer.
noLocal - if set, inhibits the delivery of messages published by its own connection
Throws:
JMSException - if the session fails to create a subscriber due to some internal error.
InvalidDestinationException - if an invalid topic is specified.
InvalidSelectorException - if the message selector is invalid.

public TopicSubscriber createDurableSubscriber(Topic topic, String name) throws JMSException
创建指定主题的持久订阅者。

如果客户端需要接收所有在主题上发布的消息,包括订阅者处于非活动状态时发布的消息,它使用持久 TopicSubscriber。JMS 提供者保留此持久订阅的记录并确保来自该主题发布者的所有消息能够一直保留,直到它们得到此持久订阅者的确认或消息过期。

具有持久订阅者的会话必须总是提供相同的客户端标识符。此外,每个客户端必须指定一个名称,该名称能够唯一地标识(在客户端标识符内)它所创建的每个持久订阅。一次只有一个会话可以拥有特定持久订阅的 TopicSubscriber

客户端可以通过创建具有相同名称和新主题和/或消息选择器的持久 TopicSubscriber 来更改现有持久订阅。更改持久订阅者等效于取消订阅(删除)旧的持久订阅,并创建一个新的持久订阅。

在某些情况下,连接可以既发布又订阅主题。订阅者 NoLocal 属性允许订阅者禁止传送由其自己的连接发布的消息。 此属性的默认值为 false。

topic 要订阅的非临时 Topic
name 用于标识此订阅的名称
ThrowsJMSException: 如果会话由于某个内部错误无法创建订阅者。
ThrowsInvalidDestinationException: 如果指定了无效的主题。

英文文档:

createDurableSubscriber

TopicSubscriber createDurableSubscriber(Topic topic,
                                        String name)
                                        throws JMSException
Creates a durable subscriber to the specified topic.

If a client needs to receive all the messages published on a topic, including the ones published while the subscriber is inactive, it uses a durable TopicSubscriber. The JMS provider retains a record of this durable subscription and insures that all messages from the topic's publishers are retained until they are acknowledged by this durable subscriber or they have expired.

Sessions with durable subscribers must always provide the same client identifier. In addition, each client must specify a name that uniquely identifies (within client identifier) each durable subscription it creates. Only one session at a time can have a TopicSubscriber for a particular durable subscription.

A client can change an existing durable subscription by creating a durable TopicSubscriber with the same name and a new topic and/or message selector. Changing a durable subscriber is equivalent to unsubscribing (deleting) the old one and creating a new one.

In some cases, a connection may both publish and subscribe to a topic. The subscriber NoLocal attribute allows a subscriber to inhibit the delivery of messages published by its own connection. The default value for this attribute is false.

Specified by:
createDurableSubscriber in interface Session
Parameters:
topic - the non-temporary Topic to subscribe to
name - the name used to identify this subscription
Throws:
JMSException - if the session fails to create a subscriber due to some internal error.
InvalidDestinationException - if an invalid topic is specified.

public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal) throws JMSException
创建指定主题的持久订阅者, 使用消息选择器或指定是否应将由其自己的连接发布的 消息传送给它。

如果客户端需要接收所有在主题上发布的消息 ,包括订阅者处于非活动状态时发布的消息, 它使用持久 TopicSubscriber。JMS 提供者 保留此 持久订阅的记录并确保来自该主题发布者的所有消息 能够一直保留,直到它们得到此持久订阅者的确认 或消息过期。

具有持久订阅者的会话必须总是提供相同的 客户端标识符。此外,每个客户端必须指定一个名称,该名称能够 唯一地标识(在客户端标识符内) 它所创建的每个持久订阅。一次只有一个会话可以拥有特定持久订阅的 TopicSubscriber。 非活动持久订阅者是存在但 当前没有与之关联的消息使用方的订阅者。

客户端可以通过创建具有相同名称和新主题和/或消息选择器的 持久 TopicSubscriber 来更改 现有 持久订阅。更改持久订阅者等效于 取消订阅(删除)旧的持久订阅,并创建一个新的持久订阅。

topic 要订阅的非临时 Topic
name 用于标识此订阅的名称
messageSelector 仅传送属性与消息选择器表达式相匹配的消息。值为 null 或空字符串表示该消息使用方没有消息选择器。
noLocal 如果设置,则禁止传送由其自己的连接发布的消息
ThrowsJMSException: 如果会话由于某个内部错误无法创建订阅者。
ThrowsInvalidDestinationException: 如果指定了无效的主题。
ThrowsInvalidSelectorException: 如果消息选择器无效。

英文文档:

createDurableSubscriber

TopicSubscriber createDurableSubscriber(Topic topic,
                                        String name,
                                        String messageSelector,
                                        boolean noLocal)
                                        throws JMSException
Creates a durable subscriber to the specified topic, using a message selector or specifying whether messages published by its own connection should be delivered to it.

If a client needs to receive all the messages published on a topic, including the ones published while the subscriber is inactive, it uses a durable TopicSubscriber. The JMS provider retains a record of this durable subscription and insures that all messages from the topic's publishers are retained until they are acknowledged by this durable subscriber or they have expired.

Sessions with durable subscribers must always provide the same client identifier. In addition, each client must specify a name which uniquely identifies (within client identifier) each durable subscription it creates. Only one session at a time can have a TopicSubscriber for a particular durable subscription. An inactive durable subscriber is one that exists but does not currently have a message consumer associated with it.

A client can change an existing durable subscription by creating a durable TopicSubscriber with the same name and a new topic and/or message selector. Changing a durable subscriber is equivalent to unsubscribing (deleting) the old one and creating a new one.

Specified by:
createDurableSubscriber in interface Session
Parameters:
topic - the non-temporary Topic to subscribe to
name - the name used to identify this subscription
messageSelector - only messages with properties matching the message selector expression are delivered. A value of null or an empty string indicates that there is no message selector for the message consumer.
noLocal - if set, inhibits the delivery of messages published by its own connection
Throws:
JMSException - if the session fails to create a subscriber due to some internal error.
InvalidDestinationException - if an invalid topic is specified.
InvalidSelectorException - if the message selector is invalid.

public TopicPublisher createPublisher(Topic topic) throws JMSException
创建指定主题的发布者。

客户端使用 TopicPublisher 对象在主题上发布 消息。 每次客户端在主题上创建 TopicPublisher 时,它 都会定义 一个新的消息序列,该消息序列与客户端以前发送的消息 没有顺序关系。

topic 要发布的 Topic;如果这是 未标识的生成方,则为 null
ThrowsJMSException: 如果会话由于某个内部错误 无法创建发布者。
ThrowsInvalidDestinationException: 如果指定了无效的主题。

英文文档:

createPublisher

TopicPublisher createPublisher(Topic topic)
                               throws JMSException
Creates a publisher for the specified topic.

A client uses a TopicPublisher object to publish messages on a topic. Each time a client creates a TopicPublisher on a topic, it defines a new sequence of messages that have no ordering relationship with the messages it has previously sent.

Parameters:
topic - the Topic to publish to, or null if this is an unidentified producer
Throws:
JMSException - if the session fails to create a publisher due to some internal error.
InvalidDestinationException - if an invalid topic is specified.

public TemporaryTopic createTemporaryTopic() throws JMSException
创建 TemporaryTopic 对象。其生命周期将是 TopicConnection 的生命周期,除非提前删除该对象。
return 临时主题身份
ThrowsJMSException: 如果会话由于某个内部错误无法创建临时主题。
英文文档:

createTemporaryTopic

TemporaryTopic createTemporaryTopic()
                                    throws JMSException
Creates a TemporaryTopic object. Its lifetime will be that of the TopicConnection unless it is deleted earlier.

Specified by:
createTemporaryTopic in interface Session
Returns:
a temporary topic identity
Throws:
JMSException - if the session fails to create a temporary topic due to some internal error.

public void unsubscribe(String name) throws JMSException
取消订阅客户端已创建的持久订阅。

此方法删除其提供者代表订阅者维持的状态 。

在该订阅存在活动的 TopicSubscriber 时, 或者当已使用的消息是挂起事务的一部分或 尚未在会话中得到确认时,客户端删除持久订阅是错误的。

name 用于标识此订阅的名称
ThrowsJMSException: 如果会话由于某个内部错误无法取消对持久订阅的订阅。
ThrowsInvalidDestinationException: 如果指定了无效的订阅名称。

英文文档:

unsubscribe

void unsubscribe(String name)
                 throws JMSException
Unsubscribes a durable subscription that has been created by a client.

This method deletes the state being maintained on behalf of the subscriber by its provider.

It is erroneous for a client to delete a durable subscription while there is an active TopicSubscriber for the subscription, or while a consumed message is part of a pending transaction or has not been acknowledged in the session.

Specified by:
unsubscribe in interface Session
Parameters:
name - the name used to identify this subscription
Throws:
JMSException - if the session fails to unsubscribe to the durable subscription due to some internal error.
InvalidDestinationException - if an invalid subscription name is specified.


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!