📄 jmsqueuesession.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
*/
package org.exolab.jms.client;
import javax.jms.InvalidDestinationException;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueBrowser;
import javax.jms.TemporaryQueue;
/**
* Client implementation of the <code>javax.jms.QueueSession</code> interface
*
* @version $Revision: 1.22 $ $Date: 2004/01/20 14:14:21 $
* @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
*/
class JmsQueueSession
extends JmsSession
implements QueueSession {
/**
* Construct a new <code>JmsQueueSession</code>
*
* @param connection the owner of the session
* @param transacted if <code>true</code>, the session is transacted.
* @param ackMode indicates whether the consumer or the client will
* acknowledge any messages it receives. This parameter will be ignored if
* the session is transacted. Legal values are
* <code>Session.AUTO_ACKNOWLEDGE</code>,
* <code>Session.CLIENT_ACKNOWLEDGE</code> and
* <code>Session.DUPS_OK_ACKNOWLEDGE</code>.
* @throws JMSException if the session cannot be created
*/
public JmsQueueSession(JmsQueueConnection connection, boolean transacted,
int ackMode) throws JMSException {
super(connection, transacted, ackMode);
}
/**
* Create a queue identity given its name
*
* @param queueName the queue name
* @return a queue with the given name.
* @throws JMSException if the queue can't be created
*/
public synchronized Queue createQueue(String queueName)
throws JMSException {
ensureOpen();
JmsQueue queue = null;
if (queueName != null && queueName.length() > 0) {
queue = new JmsQueue(queueName);
} else {
throw new JMSException(
"Cannot create a queue with null or empty name");
}
return queue;
}
/**
* Create a receiver to receive messages from the specified queue
*
* @param queue the queue to access
* @return the new receiver
* @throws JMSException if the receiver cannot be created
*/
public QueueReceiver createReceiver(Queue queue) throws JMSException {
return createReceiver(queue, null);
}
/**
* Create a receiver to receive messages from the specified queue
*
* @param queue the queue to access
* @param selector the message selector to filter messages.
* May be <code>null</code>
* @return the new receiver
* @throws JMSException if the receiver cannot be created
*/
public synchronized QueueReceiver createReceiver(Queue queue,
String selector)
throws JMSException {
JmsQueueReceiver receiver = null;
ensureOpen();
if (queue == null) {
throw new InvalidDestinationException(
"Cannot create receiver: argument 'queue' is null");
}
// check to see if the queue is temporary. A temporary queue
// can only be used within the context of the owning connection
if (!checkForValidTemporaryDestination((JmsDestination) queue)) {
throw new InvalidDestinationException(
"Cannot create a receiver for a temp queue that is not "
+ "bound to this connection");
}
receiver = new JmsQueueReceiver(this, getNextConsumerId(),
(JmsQueue) queue, selector);
addReceiver(receiver);
return receiver;
}
/**
* Create a sender to send messages to the specified queue.
*
* @param queue the queue to access, or <code>null</code> if this is an
* unidentified producer
* @return the new sender
* @throws JMSException if the sender can't be created
*/
public synchronized QueueSender createSender(Queue queue)
throws JMSException {
ensureOpen();
JmsQueueSender sender = new JmsQueueSender(this, (JmsQueue) queue);
addSender(sender);
return sender;
}
/**
* Create a new queue browser
*
* @param queue the queue to access
* @return the new queue browser
* @throws JMSException if the browser can't be created
*/
public QueueBrowser createBrowser(Queue queue) throws JMSException {
return createBrowser(queue, null);
}
/**
* Create a new queue browser
*
* @param queue the queue to access
* @param selector the message selector to filter messages.
* May be <code>null</code>
* @return the new queue browser
* @throws JMSException if the browser can't be created
*/
public synchronized QueueBrowser createBrowser(Queue queue,
String selector)
throws JMSException {
ensureOpen();
if (queue == null) {
throw new InvalidDestinationException(
"Cannot create browser: argument 'queue' is null");
}
JmsQueueBrowser browser = null;
// check to see if the queue is temporary. A temporary queue
// can only be used within the context of the owning connection
if (!checkForValidTemporaryDestination((JmsDestination) queue)) {
throw new InvalidDestinationException(
"Cannot create a queue browser for a temp queue "
+ "that is not bound to this connection");
}
browser = new JmsQueueBrowser(
this, getNextConsumerId(), (JmsQueue) queue, selector);
addBrowser(browser);
return browser;
}
/**
* Create a temporary queue. It's lifetime is that of the QueueConnection,
* unless deleted earlier.
*
* @return a new temporary queue
* @throws JMSException if the queue cannot be created
*/
public synchronized TemporaryQueue createTemporaryQueue()
throws JMSException {
ensureOpen();
JmsTemporaryQueue queue = new JmsTemporaryQueue();
queue.setOwningConnection(getConnection());
return queue;
}
/**
* Register a receiver
*
* @param receiver the receiver to register
* @throws JMSException if the receiver cannot be registered with the
* server
*/
protected void addReceiver(JmsQueueReceiver receiver) throws JMSException {
// create it on the server
getJmsSessionStub().createReceiver(
(JmsQueue) receiver.getQueue(), receiver.getClientId(),
receiver.getMessageSelector());
// register locally
addConsumer(receiver);
}
/**
* Register a sender
*
* @param sender the sender to register
*/
protected void addSender(JmsQueueSender sender) {
// getJmsSessionStub().createSender((JmsQueue)sender.getQueue());
// -- no longer used.
// register locally
addProducer(sender);
}
/**
* Register a browser
*
* @param browser the browser to register
* @throws JMSException if the browser cannot be registered with the
* server
*/
protected void addBrowser(JmsQueueBrowser browser) throws JMSException {
// create it on the server
getJmsSessionStub().createBrowser((JmsQueue) browser.getQueue(),
browser.getClientId(), browser.getMessageSelector());
// register locally
addConsumer(browser);
}
/**
* Deregister a receiver
*
* @param receiver the receiver to deregister
* @throws JMSException if the receiver cannot be deregistered from the
* server
*/
protected synchronized void removeReceiver(JmsQueueReceiver receiver)
throws JMSException {
// unregister the message listener. This must be called before
// deleting the receiver for correct clean up
if (!isClosed()) {
removeMessageListener(receiver);
getJmsSessionStub().deleteReceiver(receiver.getClientId());
}
// local clean up
removeConsumer(receiver);
}
/**
* Deregister a sender
*
* @param sender the sender to deregister
*/
protected synchronized void removeSender(JmsQueueSender sender) {
// local clean up
removeProducer(sender);
}
/**
* Deregister a queue browser
*
* @param browser the browser to deregister
* @throws JMSException if the browser cannot be deregistered from the
* server
*/
protected synchronized void removeBrowser(JmsQueueBrowser browser)
throws JMSException {
// deregister from the server
if (!isClosed()) {
getJmsSessionStub().deleteBrowser(browser.getClientId());
}
// local clean up
removeConsumer(browser);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -