Message Producers

Message producers are used for sending messages. Message producers are created using the factory methods provided by sessions. The root interface for all message producers is defined by the interface javax.jms.MessageProducer. The interface javax.jms.QueueSender defines the message producer for the PTP model and the interface javax.jms.TopicPublisher defines the message producer for the Pub/Sub model. Queue sessions provide factory methods for creating queue senders and topic sessions provide factory methods for creating topic publishers.

Message Priority

Message producers can set priorities to all the messages they send, globally as well as individually. This overrides the FIFO behavior of the queue. The range of values is from 0 (lowest) to 9 (highest), with the default priority set to 4. This is defined by the public static member variable DEFAULT_PRIORITY in the interface javax.jms.Message. The interface javax.jms.MessageProducer defines methods for globally setting and getting the priorities for the messages it sends:

     public void setPriority(int priority) throws JMSException     public int getPriority() throws JMSException 

Delivery Mode

Message producers let JMS clients globally set the delivery mode for all the messages sent using the message producer. Delivery mode indicates whether the messages are to persist or not. Persistent messages are guaranteed to be delivered once and only once. Persistent messages are only guaranteed not to be lost during transmission, but this doesn't guarantee message persistence and retention at the provider. The interface javax.jms.MessageProducer defines methods for setting and getting the delivery mode:

     public void setDeliveryMode(int mode) throws JMSException     public int getDeliveryMode() throws JMSException 

javax.jms.DeliveryMode

The interface javax.jms.DeliveryMode defines two enumerated public static member variables to deine the different delivery modes:

  • PERSISTENT - This defines the persistent delivery mode.

  • NON_PERSISTENT - This defines the non-persistent delivery mode.

Message Time-to-Live

JMS clients can use message producers to globally define the number of milliseconds the message will be retained by the messaging system after the messages are sent. If the time-to-live is set to zero the message never expires:

     public void setTimeToLive(long time) throws JMSException     public long getimeToLive() throws JMSException 

Disabling Message Properties

Message producers provide methods for globally disabling the automatic setting of some of the properties for all the messages sent by the producer. The signatures of these methods are listed below:

Method

Description

public boolean getDisableMessageID() throws JMSException

Returns True if the setting of message ID is disabled.

public void setDisableMessageID (boolean val) throws JMSException

Used for disabling/enabling the setting of message ID.

public boolean getDisableMessageTimestamp() throws JMSException

Returns True if the setting of message timestamp is disabled.

public void setDisableMessageTimestamp (boolean val) throws JMSException

Used for disabling/enabling the setting of message timestamp.

Sending Messages

The methods used for sending messages are defined in the messaging-model-specific sub-interfaces of the interface javax.jms.MessageProducer. The interface javax.jms.QueueSender defines methods for sending messages in the PTP messaging model and javax.jms.TopicPublisher defines methods for sending messages in the Pub/Sub messaging model. Both these interfaces define a plethora of overloaded send/publish methods that can be used for overriding the global properties like delivery mode, time to live, priority, etc., set at the message producer level.

The send() methods for javax.jms.QueueSender are listed below:

Method

Description

public void send (Message msg) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the associated queue.

public void send (Message msg, int mode, int priority, long timetolive) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the associated queue. This method lets JMS clients override the globally set message properties.

public void send (Queue queue, Message msg) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the specified queue for unidentified message producers. Unidentified message producers need to specify the destination each time they send a message.

public void send (Queue queue, Message msg, int mode, int priority, long timetolive) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the specified queue. This method lets JMS clients override the globally set message properties.

The methods defined in javax.jms.TopicPublisher for publishing messages are listed below:

Method

Description

public void publish (Message msg) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the associated topic.

public void publish (Message msg, int mode, int priority, long timetolive) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the associated topic. This method lets JMS clients override the globally set message properties.

public void publish (Topic topic, Message msg) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the specified topic for unidentified publishers.

public void publish (Topic topic, Message msg, int mode, int priority, long timetolive) throws JMSException, MessageFormatException, InvalidDestinationException

Sends the message to the specified topic. This method lets JMS clients override the globally set message properties.

All the aforementioned methods throw JMSException if JMS fails to send the message due to some internal server error, MessageFormatException if the specified message is not valid, and InvalidDestinationException if the destination associated with the message producer is not valid.



Professional JMS
Professional JMS
ISBN: 1861004931
EAN: 2147483647
Year: 2000
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net