jms-spec
  1. jms-spec
  2. JMS_SPEC-101

New methods Message.getBody(Class<T> c) and isBodyAssignableTo(Class c)

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.1
    • Fix Version/s: 2.0PD, 2.0
    • Labels:
      None

      Description

      This is a proposal to add a new method to javax.jms.Message which allows the message payload to be obtained without the need to cast the object to the appropriate subtype first. This can slightly simplify the code of a MessageListener's, onMessage method, where the object passed in is always declared to be a javax.jms.Message.

      Here is the proposed API:

      	/**
      	 * Returns the messages's payload, which must be of the specified type. If
      	 * the message has no payload then null is returned. This method may be used
      	 * to obtain the payload of any type of message except for
      	 * <tt>StreamMessage</tt>.
      	 * 
      	 * @param c
      	 *            The class of the payload.<br/>
      	 *            If the message is a <code>TextMessage</code> then this should
      	 *            be set to <code>String.class</code>.<br/>
      	 *            If the message is a <code>ObjectMessage</code> then this
      	 *            should be set to <code>java.io.Serializable.class</code>. <br/>
      	 *            If the message is a <code>MapMessage</code> then this should
      	 *            be set to <code>java.util.Map.class</code>.<br/>
      	 *            If the message is a <code>BytesMessage</code> then this should
      	 *            be set to <code>byte[].class</code>.<br/>
      	 *            If the message payload is not of the specified type a
      	 *            <code>MessageFormatException</code> will be thrown
      	 * 
      	 * @return the messages's payload
      	 * 
      	 * @exception JMSException
      	 *                if the JMS provider fails to get the payload due to some
      	 *                internal error.
      	 * @exception MessageFormatException
      	 *                if the payload is not of the specified type or, if the
      	 *                message is an ObjectMessage, if object deserialization
      	 *                fails.
      	 * @Exception MessageNotReadableException - if the message is a BytesMessage
      	 *                and the message is in write-only mode.
      	 */
      	<T> T getPayload(Class<T> c) throws JMSException;
      

      This means that instead of something like:

          public void onMessage(Message message) {
              String payload = ((TextMessage) message).getText();
      

      we could have

          public void onMessage(Message message) {
              String payload2 = message.getPayload(String.class); 
      

        Issue Links

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              Nigel Deakin
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: