org.apache.activemq
public class: ActiveMQMessageProducer [javadoc |
source]
java.lang.Object
org.apache.activemq.ActiveMQMessageProducerSupport
org.apache.activemq.ActiveMQMessageProducer
All Implemented Interfaces:
StatsCapable, Disposable, javax.jms.MessageProducer, Closeable
Direct Known Subclasses:
ActiveMQTopicPublisher, ActiveMQQueueSender
A client uses a
MessageProducer object to send messages to a
destination. A
MessageProducer object is created by passing a
Destination object to a message-producer creation method
supplied by a session.
MessageProducer is the parent interface for all message
producers.
A client also has the option of creating a message producer without supplying
a destination. In this case, a destination must be provided with every send
operation. A typical use for this kind of message producer is to send replies
to requests using the request's JMSReplyTo destination.
A client can specify a default delivery mode, priority, and time to live for
messages sent by a message producer. It can also specify the delivery mode,
priority, and time to live for an individual message.
A client can specify a time-to-live value in milliseconds for each message it
sends. This value defines a message expiration time that is the sum of the
message's time-to-live and the GMT when it is sent (for transacted sends,
this is the time the client sends the message, not the time the transaction
is committed).
A JMS provider should do its best to expire messages accurately; however, the
JMS API does not define the accuracy provided.
| Field Summary |
|---|
| protected ProducerInfo | info | |
| protected boolean | closed | |
| Constructor: |
protected ActiveMQMessageProducer(ActiveMQSession session,
ProducerId producerId,
ActiveMQDestination destination,
int sendTimeout) throws JMSException {
super(session);
this.info = new ProducerInfo(producerId);
this.info.setWindowSize(session.connection.getProducerWindowSize());
if (destination != null && destination.getOptions() != null) {
Map< String, String > options = new HashMap< String, String >(destination.getOptions());
IntrospectionSupport.setProperties(this.info, options, "producer.");
}
this.info.setDestination(destination);
// Enable producer window flow control if protocol > 3 and the window
// size > 0
if (session.connection.getProtocolVersion() >= 3 && this.info.getWindowSize() > 0) {
producerWindow = new MemoryUsage("Producer Window: " + producerId);
producerWindow.setLimit(this.info.getWindowSize());
producerWindow.start();
}
this.defaultDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
this.defaultPriority = Message.DEFAULT_PRIORITY;
this.defaultTimeToLive = Message.DEFAULT_TIME_TO_LIVE;
this.startTime = System.currentTimeMillis();
this.messageSequence = new AtomicLong(0);
this.stats = new JMSProducerStatsImpl(session.getSessionStats(), destination);
this.session.addProducer(this);
this.session.asyncSendPacket(info);
this.setSendTimeout(sendTimeout);
setTransformer(session.getTransformer());
}
|
| Method from org.apache.activemq.ActiveMQMessageProducer Summary: |
|---|
|
checkClosed, close, dispose, getDestination, getMessageSequence, getProducerInfo, getProducerStats, getStartTime, getStats, getTransformer, onProducerAck, send, setMessageSequence, setProducerInfo, setTransformer, toString |
| Methods from org.apache.activemq.ActiveMQMessageProducerSupport: |
|---|
|
checkClosed, getDeliveryMode, getDisableMessageID, getDisableMessageTimestamp, getPriority, getSendTimeout, getTimeToLive, send, send, send, setDeliveryMode, setDisableMessageID, setDisableMessageTimestamp, setPriority, setSendTimeout, setTimeToLive |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.apache.activemq.ActiveMQMessageProducer Detail: |
protected void checkClosed() throws IllegalStateException {
if (closed) {
throw new IllegalStateException("The producer is closed");
}
}
Check if the instance of this producer has been closed. |
public void close() throws JMSException {
if (!closed) {
dispose();
this.session.asyncSendPacket(info.createRemoveCommand());
}
}
Closes the message producer.
Since a provider may allocate some resources on behalf of a
MessageProducer
outside the Java virtual machine, clients should close them when they are
not needed. Relying on garbage collection to eventually reclaim these
resources may not be timely enough. |
public void dispose() {
if (!closed) {
this.session.removeProducer(this);
if (producerWindow != null) {
producerWindow.stop();
}
closed = true;
}
}
|
public Destination getDestination() throws JMSException {
checkClosed();
return this.info.getDestination();
}
Gets the destination associated with this MessageProducer. |
protected long getMessageSequence() {
return messageSequence.incrementAndGet();
}
|
protected ProducerInfo getProducerInfo() {
return this.info != null ? this.info : null;
}
|
public JMSProducerStatsImpl getProducerStats() {
return stats;
}
|
protected long getStartTime() {
return this.startTime;
}
|
public StatsImpl getStats() {
return stats;
}
|
public MessageTransformer getTransformer() {
return transformer;
}
|
public void onProducerAck(ProducerAck pa) {
if (this.producerWindow != null) {
this.producerWindow.decreaseUsage(pa.getSize());
}
}
|
public void send(Destination destination,
Message message,
int deliveryMode,
int priority,
long timeToLive) throws JMSException {
checkClosed();
if (destination == null) {
if (info.getDestination() == null) {
throw new UnsupportedOperationException("A destination must be specified.");
}
throw new InvalidDestinationException("Don't understand null destinations");
}
ActiveMQDestination dest;
if (destination == info.getDestination()) {
dest = (ActiveMQDestination)destination;
} else if (info.getDestination() == null) {
dest = ActiveMQDestination.transform(destination);
} else {
throw new UnsupportedOperationException("This producer can only send messages to: " + this.info.getDestination().getPhysicalName());
}
if (dest == null) {
throw new JMSException("No destination specified");
}
if (transformer != null) {
Message transformedMessage = transformer.producerTransform(session, this, message);
if (transformedMessage != null) {
message = transformedMessage;
}
}
if (producerWindow != null) {
try {
producerWindow.waitForSpace();
} catch (InterruptedException e) {
throw new JMSException("Send aborted due to thread interrupt.");
}
}
this.session.send(this, dest, message, deliveryMode, priority, timeToLive, producerWindow,sendTimeout);
stats.onMessage();
}
Sends a message to a destination for an unidentified message producer,
specifying delivery mode, priority and time to live.
Typically, a message producer is assigned a destination at creation time;
however, the JMS API also supports unidentified message producers, which
require that the destination be supplied every time a message is sent. |
protected void setMessageSequence(AtomicLong messageSequence) {
this.messageSequence = messageSequence;
}
|
protected void setProducerInfo(ProducerInfo info) {
this.info = info;
}
|
public void setTransformer(MessageTransformer transformer) {
this.transformer = transformer;
}
Sets the transformer used to transform messages before they are sent on
to the JMS bus |
public String toString() {
return "ActiveMQMessageProducer { value=" + info.getProducerId() + " }";
}
|