J2EE Messaging 893 Message Header Clients and the JMS provider can use the message header to identify and route the message. The complete message header is transmitted to all recipients of the message. The message producer can set the following header fields: q The JMSDeliveryMode property specifies the mode of delivery for the message. The two types of delivery mode that JMS supports are PERSISTENT and NON_PERSISTENT. The PERSISTENT mode instructs the JMS provider to take extra care to ensure that the message is not lost in transit. Persistent messages can survive a JMS server crash as they are written to the disk as soon as the message server receives them from the message producer. The server then keeps track of which recipients successfully receive each message. If the server fails then restarts, it picks up where it left off delivering the messages. Delivering persistent messages involves more overhead than delivering non-persistent messages because the server stores the message to disk in persistent mode so non-persistent messages perform better. You should use non-persistent messages rather than persistent ones in cases in which it is acceptable if some messages are lost. In most enterprise applications, however, messages are persistent because it is important that they are delivered. A message has default JMSDeliveryMode of NON_PERSISTENT. q The JMSPriority property specifies the message's priority. 0 is the lowest priority and 9 is the highest priority. The message priority determines the order in which the message consumer processes messages. Messages with higher priority are processed first and messages with the same priority are processed in the order in which they arrive. A message has default JMSPriority of 4. q The JMSExpiration property specifies the time until the message expires. A value of 0 means that the message never expires. A message has default JMSExpiration value of 0. q The JMSReplyTo property specifies a destination to which message replies should be sent. It is of type javax.jms.Destination (either a topic or a queue). If the message consumer chooses to reply to the message it will reply to the specified destination. q The JMSCorrelationID property can be used to associate the message with a previously sent message. It is commonly used to specify the message to which the current message is a reply. q The JMSType property specifies the type of the message – but it is not the same as the message types described in the "Message Body" section of this chapter. Most vendors do not currently support the JMSType property. The publish() and send() methods introduced earlier have alternative signatures that allow the developer to set the JMSDeliveryMode, JMSPriority, and the JMSExpiration header fields. The signatures for these methods are given below: publish(Message message, int deliveryMode, int priority, long timeToLive) send (Message message, int deliveryMode, int priority, long timeToLive) The deliveryMode and priority parameters set the JMSDeliveryMode and JMSPriority parameters. The timeToLive parameter is added to the current time to determine the JMSExpiration field. If the timeToLive parameter is 0 then the JMSExpiration field will be set to 0 and the message will never expire.