Class MessageProducer

  • All Implemented Interfaces:
    jakarta.jms.MessageProducer, AutoCloseable
    Direct Known Subclasses:
    QueueSender, TopicPublisher

    public class MessageProducer
    extends Object
    implements jakarta.jms.MessageProducer
    Implements the jakarta.jms.MessageProducer interface.

    A client uses a MessageProducer object to send messages to a destination. A MessageProducer object is created by calling the createProducer method on the session object. A message producer is normally dedicated to a unique destination.
    A client also has the option of creating a message producer without supplying a unique destination. In this case, a destination must be provided with every send operation.
    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 each individual message.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean closed
      true if the producer is closed.
      private static boolean DEBUG  
      private long deliveryDelay
      Default delivery delay
      private int deliveryMode
      Default delivery mode.
      protected Destination dest
      The destination the producer sends messages to.
      private boolean identified
      true if the producer's destination is identified.
      private static Logger logger  
      private boolean messageIDDisabled
      true if the client requests not to use the message identifiers; however it is not taken into account, as our MOM needs message identifiers for managing acknowledgements.
      private int priority
      Default priority.
      protected Session sess
      The session the producer belongs to.
      private boolean timestampDisabled
      true if the time stamp is disabled.
      private long timeToLive
      Default time to live.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      API method.
      private void doSend​(Destination dest, jakarta.jms.Message message, int deliveryMode, int priority, long timeToLive)
      Actually sends a message to a given destination.
      private void doSend​(Destination dest, jakarta.jms.Message message, int deliveryMode, int priority, long timeToLive, jakarta.jms.CompletionListener completionListener)
      Actually sends a message to a given destination.
      long getDeliveryDelay()
      API 2.0 Get the delivery delay.
      int getDeliveryMode()
      API method.
      jakarta.jms.Destination getDestination()
      API method.
      boolean getDisableMessageID()
      API method.
      boolean getDisableMessageTimestamp()
      API method.
      int getPriority()
      API method.
      long getTimeToLive()
      API method.
      void send​(jakarta.jms.Destination dest, jakarta.jms.Message message)
      API method.
      void send​(jakarta.jms.Destination dest, jakarta.jms.Message message, int deliveryMode, int priority, long timeToLive)
      API method.
      void send​(jakarta.jms.Destination destination, jakarta.jms.Message message, int deliveryMode, int priority, long timeToLive, jakarta.jms.CompletionListener completionListener)
      API 2.0 method.
      void send​(jakarta.jms.Destination destination, jakarta.jms.Message message, jakarta.jms.CompletionListener completionListener)
      API 2.0 method.
      void send​(jakarta.jms.Message message)
      API method.
      void send​(jakarta.jms.Message message, int deliveryMode, int priority, long timeToLive)
      API method.
      void send​(jakarta.jms.Message message, int deliveryMode, int priority, long timeToLive, jakarta.jms.CompletionListener completionListener)
      API 2.0 method.
      void send​(jakarta.jms.Message message, jakarta.jms.CompletionListener completionListener)
      API 2.0 method.
      void setDeliveryDelay​(long deliveryDelay)
      API 2.0 Sets the minimum length of time in milliseconds that must elapse after a message is sent before the consumer can consume this message.
      void setDeliveryMode​(int deliveryMode)
      API method.
      void setDisableMessageID​(boolean value)
      API method, not taken into account.
      void setDisableMessageTimestamp​(boolean value)
      API method.
      void setPriority​(int priority)
      API method.
      void setTimeToLive​(long timeToLive)
      API method.
    • Field Detail

      • logger

        private static Logger logger
      • DEBUG

        private static final boolean DEBUG
      • deliveryMode

        private int deliveryMode
        Default delivery mode.
      • priority

        private int priority
        Default priority.
      • timeToLive

        private long timeToLive
        Default time to live.
      • deliveryDelay

        private long deliveryDelay
        Default delivery delay
      • messageIDDisabled

        private boolean messageIDDisabled
        true if the client requests not to use the message identifiers; however it is not taken into account, as our MOM needs message identifiers for managing acknowledgements.
      • timestampDisabled

        private boolean timestampDisabled
        true if the time stamp is disabled.
      • identified

        private boolean identified
        true if the producer's destination is identified.
      • closed

        protected boolean closed
        true if the producer is closed.
      • sess

        protected Session sess
        The session the producer belongs to.
      • dest

        protected Destination dest
        The destination the producer sends messages to.
    • Constructor Detail

      • MessageProducer

        MessageProducer​(Session sess,
                        Destination dest)
                 throws jakarta.jms.JMSException
        Constructs a producer.
        Parameters:
        sess - The session the producer belongs to.
        dest - The destination the producer sends messages to.
        Throws:
        jakarta.jms.InvalidDestinationException - if an invalid destination is specified.
        jakarta.jms.IllegalStateException - If the connection is broken.
        jakarta.jms.JMSException - If the creation fails for any other reason.
    • Method Detail

      • setDisableMessageID

        public void setDisableMessageID​(boolean value)
                                 throws jakarta.jms.JMSException
        API method, not taken into account. Message IDs are always enabled.
        Specified by:
        setDisableMessageID in interface jakarta.jms.MessageProducer
        Parameters:
        value - indicates if message IDs are disabled, not taken in account.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • setDeliveryMode

        public void setDeliveryMode​(int deliveryMode)
                             throws jakarta.jms.JMSException
        API method. Sets the producer's default delivery mode.

        Delivery mode is set to PERSISTENT by default.

        Specified by:
        setDeliveryMode in interface jakarta.jms.MessageProducer
        Parameters:
        deliveryMode - the message delivery mode for this message producer; legal values are DeliveryMode.NON_PERSISTENT and DeliveryMode.PERSISTENT.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException - When setting an invalid delivery mode.
      • setPriority

        public void setPriority​(int priority)
                         throws jakarta.jms.JMSException
        API method. Sets the producer's default priority.

        The JMS API defines ten levels of priority value, with 0 as the lowest priority and 9 as the highest. Clients should consider priorities 0-4 as gradations of normal priority and priorities 5-9 as gradations of expedited priority.

        Priority is set to 4 by default (Message.DEFAULT_PRIORITY).

        Specified by:
        setPriority in interface jakarta.jms.MessageProducer
        Parameters:
        priority - the message priority for this message producer; must be a value between 0 and 9.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException - When setting an invalid priority.
      • setTimeToLive

        public void setTimeToLive​(long timeToLive)
                           throws jakarta.jms.JMSException
        API method. Sets the default duration of time in milliseconds that a produced message should be retained by the provider.

        Time to live is set to zero by default (Message.DEFAULT_TIME_TO_LIVE).

        Specified by:
        setTimeToLive in interface jakarta.jms.MessageProducer
        Parameters:
        timeToLive - the message time to live in milliseconds; zero is unlimited.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • setDisableMessageTimestamp

        public void setDisableMessageTimestamp​(boolean value)
                                        throws jakarta.jms.JMSException
        API method. Sets whether message timestamps are disabled.

        Since timestamps take some effort to create and increase a message's size, Joram optimizes message overhead if it is given a hint that the timestamp is not used by an application. By calling the setDisableMessageTimestamp method on this message producer, a JMS client enables this potential optimization for all messages sent by this message producer (the produced messages have the timestamp set to zero).

        Message timestamps are enabled by default.

        Specified by:
        setDisableMessageTimestamp in interface jakarta.jms.MessageProducer
        Parameters:
        value - indicates if message timestamps are disabled.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • getDestination

        public jakarta.jms.Destination getDestination()
                                               throws jakarta.jms.JMSException
        API method. Gets the destination associated with this MessageProducer.
        Specified by:
        getDestination in interface jakarta.jms.MessageProducer
        Returns:
        the destination associated with this MessageProducer.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • getDisableMessageID

        public boolean getDisableMessageID()
                                    throws jakarta.jms.JMSException
        API method. Gets an indication of whether message IDs are disabled, always false.
        Specified by:
        getDisableMessageID in interface jakarta.jms.MessageProducer
        Returns:
        false.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • getDeliveryMode

        public int getDeliveryMode()
                            throws jakarta.jms.JMSException
        API method. Gets the producer's default delivery mode, by default Message.DEFAULT_DELIVERY_MODE.
        Specified by:
        getDeliveryMode in interface jakarta.jms.MessageProducer
        Returns:
        the message delivery mode for this message producer.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • getPriority

        public int getPriority()
                        throws jakarta.jms.JMSException
        API method. Gets the producer's default priority, by default Message.DEFAULT_PRIORITY.
        Specified by:
        getPriority in interface jakarta.jms.MessageProducer
        Returns:
        the message priority for this message producer.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • getTimeToLive

        public long getTimeToLive()
                           throws jakarta.jms.JMSException
        API method. Gets the default duration in milliseconds that a produced message should be retained by the provider, by default Message.DEFAULT_TIME_TO_LIVE.
        Specified by:
        getTimeToLive in interface jakarta.jms.MessageProducer
        Returns:
        the message time to live in milliseconds; zero is unlimited.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • getDisableMessageTimestamp

        public boolean getDisableMessageTimestamp()
                                           throws jakarta.jms.JMSException
        API method. Gets an indication of whether message timestamps are disabled.
        Specified by:
        getDisableMessageTimestamp in interface jakarta.jms.MessageProducer
        Returns:
        an indication of whether message timestamps are disabled.
        Throws:
        jakarta.jms.IllegalStateException - If the producer is closed.
        jakarta.jms.JMSException
      • send

        public void send​(jakarta.jms.Message message)
                  throws jakarta.jms.JMSException
        API method. Sends a message with the MessageProducer's default delivery parameters.
        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        message - the message to send.
        Throws:
        UnsupportedOperationException - If the dest is unidentified.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • send

        public void send​(jakarta.jms.Message message,
                         int deliveryMode,
                         int priority,
                         long timeToLive)
                  throws jakarta.jms.JMSException
        API method. Sends a message to the destination with given delivery parameters.
        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        message - the message to send.
        deliveryMode - the delivery mode to use.
        priority - the priority for this message.
        timeToLive - the message's lifetime in milliseconds.
        Throws:
        UnsupportedOperationException - If the dest is unidentified.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • send

        public void send​(jakarta.jms.Destination dest,
                         jakarta.jms.Message message)
                  throws jakarta.jms.JMSException
        API method. Sends a message to a destination for an unidentified message producer using default delivery parameters.

        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.

        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        dest - the destination to send this message to.
        message - the message to send.
        Throws:
        UnsupportedOperationException - When the producer did not properly identify itself.
        JMSSecurityException - If the user if not a WRITER on the specified destination.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • send

        public void send​(jakarta.jms.Destination dest,
                         jakarta.jms.Message message,
                         int deliveryMode,
                         int priority,
                         long timeToLive)
                  throws jakarta.jms.JMSException
        API method. Sends a message to a destination for an unidentified message producer with given delivery parameters.

        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.

        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        dest - the destination to send this message to.
        message - the message to send.
        deliveryMode - the delivery mode to use.
        priority - the priority for this message.
        timeToLive - the message's lifetime in milliseconds.
        Throws:
        UnsupportedOperationException - When the producer did not properly identify itself.
        JMSSecurityException - If the user if not a WRITER on the specified destination.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • send

        public void send​(jakarta.jms.Message message,
                         jakarta.jms.CompletionListener completionListener)
                  throws jakarta.jms.JMSException
        API 2.0 method. Sends asynchronous message with the MessageProducer's default delivery parameters.
        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        message - the message to send.
        completionListener - the completion listener (call back).
        Throws:
        UnsupportedOperationException - If the dest is unidentified.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • send

        public void send​(jakarta.jms.Message message,
                         int deliveryMode,
                         int priority,
                         long timeToLive,
                         jakarta.jms.CompletionListener completionListener)
                  throws jakarta.jms.JMSException
        API 2.0 method. Sends asynchronous message to the destination with given delivery parameters.
        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        message - the message to send.
        deliveryMode - the delivery mode to use.
        priority - the priority for this message.
        timeToLive - the message's lifetime in milliseconds.
        completionListener - the completion listener (call back).
        Throws:
        UnsupportedOperationException - If the dest is unidentified.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • send

        public void send​(jakarta.jms.Destination destination,
                         jakarta.jms.Message message,
                         jakarta.jms.CompletionListener completionListener)
                  throws jakarta.jms.JMSException
        API 2.0 method. Sends asynchronous message to a destination for an unidentified message producer using default delivery parameters.

        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.

        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        dest - the destination to send this message to.
        message - the message to send.
        completionListener - the completion listener (call back).
        Throws:
        UnsupportedOperationException - When the producer did not properly identify itself.
        JMSSecurityException - If the user if not a WRITER on the specified destination.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • send

        public void send​(jakarta.jms.Destination destination,
                         jakarta.jms.Message message,
                         int deliveryMode,
                         int priority,
                         long timeToLive,
                         jakarta.jms.CompletionListener completionListener)
                  throws jakarta.jms.JMSException
        API 2.0 method. Sends asynchronous message to a destination for an unidentified message producer with given delivery parameters.

        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.

        Specified by:
        send in interface jakarta.jms.MessageProducer
        Parameters:
        dest - the destination to send this message to.
        message - the message to send.
        deliveryMode - the delivery mode to use.
        priority - the priority for this message.
        timeToLive - the message's lifetime in milliseconds.
        completionListener - the completion listener (call back).
        Throws:
        UnsupportedOperationException - When the producer did not properly identify itself.
        JMSSecurityException - If the user if not a WRITER on the specified destination.
        jakarta.jms.IllegalStateException - If the producer is closed, or if the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • close

        public void close()
                   throws jakarta.jms.JMSException
        API method. Closes the message producer.

        In order to free significant resources allocated on behalf of a MessageProducer, clients should close them when they are not needed.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface jakarta.jms.MessageProducer
        Throws:
        jakarta.jms.JMSException - Actually never thrown.
      • doSend

        private void doSend​(Destination dest,
                            jakarta.jms.Message message,
                            int deliveryMode,
                            int priority,
                            long timeToLive)
                     throws jakarta.jms.JMSException
        Actually sends a message to a given destination.
        Throws:
        jakarta.jms.MessageFormatException - If the message to send is invalid.
        jakarta.jms.InvalidDestinationException - If the specified destination is invalid.
        jakarta.jms.IllegalStateException - If the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • doSend

        private void doSend​(Destination dest,
                            jakarta.jms.Message message,
                            int deliveryMode,
                            int priority,
                            long timeToLive,
                            jakarta.jms.CompletionListener completionListener)
                     throws jakarta.jms.JMSException
        Actually sends a message to a given destination.
        Throws:
        jakarta.jms.MessageFormatException - If the message to send is invalid.
        jakarta.jms.InvalidDestinationException - If the specified destination is invalid.
        jakarta.jms.IllegalStateException - If the connection is broken.
        jakarta.jms.JMSException - If the request fails for any other reason.
      • setDeliveryDelay

        public void setDeliveryDelay​(long deliveryDelay)
                              throws jakarta.jms.JMSException
        API 2.0 Sets the minimum length of time in milliseconds that must elapse after a message is sent before the consumer can consume this message. the delivery delay is set to zero by default
        Specified by:
        setDeliveryDelay in interface jakarta.jms.MessageProducer
        Parameters:
        deliveryDelay - the delivery delay in milliseconds.
        Throws:
        jakarta.jms.JMSException
      • getDeliveryDelay

        public long getDeliveryDelay()
                              throws jakarta.jms.JMSException
        API 2.0 Get the delivery delay.
        Specified by:
        getDeliveryDelay in interface jakarta.jms.MessageProducer
        Returns:
        the delivery delay in milliseconds.
        Throws:
        jakarta.jms.JMSException