public class MessageConsumer extends Object implements MessageConsumer
javax.jms.MessageConsumer
interface.
A client uses a MessageConsumer object to receive messages from a
destination. A MessageConsumer object is created by calling the
createConsumer method on a session object. A message consumer is
normally dedicated to a unique destination.
A message consumer can be created with a message selector. A message
selector allows the client to restrict the messages delivered to the
message consumer to those that match the selector.
A client may either synchronously receive a message consumer's messages or
have the consumer asynchronously deliver them as they arrive:
Modifier and Type | Class and Description |
---|---|
(package private) class |
MessageConsumer.Closer
This class synchronizes the close.
|
private static class |
MessageConsumer.Status
Status of the message consumer.
|
Modifier and Type | Field and Description |
---|---|
private MessageConsumer.Closer |
closer
Used to synchronize the method close()
|
protected Destination |
dest
The destination the consumer gets its messages from.
|
private boolean |
durableSubscriber
true for a durable subscriber. |
private static Logger |
logger |
private MessageListener |
messageListener |
protected boolean |
noLocal
true if the subscriber does not wish to consume messages
produced by its connection. |
(package private) boolean |
queueMode
true if the consumer is a queue consumer. |
(package private) String |
selector
The selector for filtering messages.
|
protected Session |
sess
The session the consumer belongs to.
|
private boolean |
shared |
private int |
status
Status of the message consumer
OPEN, CLOSE
|
(package private) String |
targetName
The consumer server side target is either a queue or a subscription on
its proxy.
|
Constructor and Description |
---|
MessageConsumer(Session sess,
Destination dest,
String selector)
Constructs a consumer.
|
MessageConsumer(Session sess,
Destination dest,
String selector,
String subName,
boolean noLocal,
boolean shared,
boolean durableSubscriber)
Constructs a consumer.
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkClosed() |
void |
close()
API method.
|
(package private) void |
doClose() |
Destination |
getDest() |
MessageListener |
getMessageListener()
API method.
|
String |
getMessageSelector()
API method.
|
boolean |
getQueueMode() |
String |
getTargetName() |
boolean |
isOpen() |
boolean |
isQueueMode() |
Message |
receive()
API method.
|
Message |
receive(long timeOut)
API method.
|
Message |
receiveNoWait()
API method.
|
void |
setMessageListener(MessageListener messageListener)
Sets the message consumer's MessageListener.
|
private void |
setStatus(int status) |
String |
toString()
Returns a string view of this consumer.
|
private static Logger logger
String selector
private boolean durableSubscriber
true
for a durable subscriber.protected Destination dest
protected boolean noLocal
true
if the subscriber does not wish to consume messages
produced by its connection.protected Session sess
String targetName
boolean queueMode
true
if the consumer is a queue consumer.private int status
private MessageConsumer.Closer closer
private boolean shared
private MessageListener messageListener
MessageConsumer(Session sess, Destination dest, String selector, String subName, boolean noLocal, boolean shared, boolean durableSubscriber) throws JMSException
sess
- The session the consumer belongs to.dest
- The destination the consumer gets messages from.selector
- Selector for filtering messages.subName
- The durableSubscriber subscription's name, if any.noLocal
- true
for a subscriber not wishing to consume
messages produced by its connection.shared
- true if shared consumer.durableSubscriber
- true if durableInvalidDestinationException
- if an invalid destination is specified.InvalidSelectorException
- If the selector syntax is invalid.IllegalStateException
- If the connection is broken, or if the
subscription is durable and already
activated.JMSException
- Generic exception.MessageConsumer(Session sess, Destination dest, String selector) throws JMSException
sess
- The session the consumer belongs to.dest
- The destination the consumer gets messages from.selector
- Selector for filtering messages.InvalidDestinationException
- if an invalid destination is specified.InvalidSelectorException
- If the selector syntax is invalid.IllegalStateException
- If the connection is broken, or if the
subscription is durable and already
activated.JMSException
- Generic exception.private void setStatus(int status)
public final String getTargetName()
public final boolean getQueueMode()
protected void checkClosed() throws IllegalStateException
IllegalStateException
public String toString()
public void setMessageListener(MessageListener messageListener) throws JMSException
This method must not be called if the connection the consumer belongs to is started, because the session would then be accessed by the thread calling this method and by the thread controlling asynchronous deliveries. This situation is clearly forbidden by the single threaded nature of sessions. Moreover, unsetting a message listener without stopping the connection may lead to the situation where asynchronous deliveries would arrive on the connection, the session or the consumer without being able to reach their target listener!
setMessageListener
in interface MessageConsumer
messageListener
- the listener to which the messages are to be delivered.IllegalStateException
- If the consumer is closed, or if the
connection is broken.JMSException
- If the request fails for any other reason.public MessageListener getMessageListener() throws JMSException
getMessageListener
in interface MessageConsumer
IllegalStateException
- If the consumer is closed.JMSException
public final String getMessageSelector() throws JMSException
getMessageSelector
in interface MessageConsumer
IllegalStateException
- If the consumer is closed.JMSException
public Message receive(long timeOut) throws JMSException
This call blocks until a message is available, the timeout expires, or this message consumer is closed. A timeout of zero never expires, and the call blocks indefinitely.
receive
in interface MessageConsumer
timeOut
- the timeout value (in milliseconds).IllegalStateException
- If the consumer is closed, or if the
connection is broken.JMSSecurityException
- If the requester is not a READER on the
destination.JMSException
- If the request fails for any other reason.public Message receive() throws JMSException
If this receive is done within a transaction, the consumer retains the message until the transaction commits.
receive
in interface MessageConsumer
IllegalStateException
- If the consumer is closed, or if the
connection is broken.JMSSecurityException
- If the requester is not a READER on the
destination.JMSException
- If the request fails for any other reason.public Message receiveNoWait() throws JMSException
receiveNoWait
in interface MessageConsumer
IllegalStateException
- If the consumer is closed, or if the
connection is broken.JMSSecurityException
- If the requester is not a READER on the
destination.JMSException
- If the request fails for any other reason.public void close() throws JMSException
In order to free significant resources allocated on behalf of a MessageConsumer, clients should close them when they are not needed.
This call blocks until a receive or message listener in progress has completed. A blocked message consumer receive call returns null when this message consumer is closed.
close
in interface MessageConsumer
JMSException
- if closing the consumer fails due to some internal error.void doClose() throws JMSException
JMSException
public Destination getDest()
public boolean isQueueMode()
public boolean isOpen()
Copyright © 2022 ScalAgent D.T.. All rights reserved.