jsr343-experts@jms-spec.java.net

[jsr343-experts] Re: (JMS_SPEC-101) New method Message.getPayload(Class<T> c)

From: John D. Ament <john.d.ament_at_gmail.com>
Date: Tue, 23 Oct 2012 10:07:10 -0400

Ok, so looking throught it a bit more I think we have a few issues

1. You cannot pass byte[].class to the method (that's not a valid class).
I think we need a work around here.
2. I think the case should simply pass when
Class.isAssignableFrom(Otherclass.class) is passed. So if I pass in
CharSequence.class, I should be able to get back a CharSequence in the
following cases:

- The message is a TextMessage, the underlying result is a String back
(returned as a CharSequence instead).
- The message is an ObjectMessage, the underlying type is an implementation
of CharSequence (StringBuilder, StringBuffer, String).

IMHO from the initial messages about this method it seemed like the goal
was to make the client ignorant (lack of a better term) to the fact that
it's a text message or object message or map message. Also passing in
Object.class should just return the payload, regardless of type, as long as
it's not a StreamMessage.

Thoughts?

John

On Tue, Oct 23, 2012 at 8:01 AM, Nigel Deakin <nigel.deakin_at_oracle.com>wrote:

> On 23/10/2012 12:33, John D. Ament wrote:
>
>
> All,
>
> Just wondering - can we complement this with a method along the lines of
> "isPayloadType(Class<?> clazz)" that returns true if the underlying payload
> can be cast to the given class, false if it cannot be (or if it's a stream
> message?)
>
>
> Thanks for the suggestion.
>
> isPayloadType(String.class) would return true only if the message was a
> TextMessage
> isPayloadType(Map.class) would return true only if the message was a
> MapMessage
> isPayloadType(Serializable.class) would return true only if the message
> was a ObjectMessage
> isPayloadType(byte[].class) would return true only if the message was
> aBytesMessage
>
> isPayloadType(someOtherClass) would return true only if the message was a
> ObjectMessage and the payload could be cast to the specified class.
>
> Otherwise isPayloadType would return false. (I can't think of any other
> cases where it would return true).
>
> Is that what you had in mind?
>
> Nigel
>
>
>
> I know in my applications today I have up to three if blocks depending on
> the message implementation used, and would need this logic to check before
> processing. It may also be cleaner than catching the exception.
>
> John
>
> On Tue, Oct 23, 2012 at 7:29 AM, Nigel Deakin <nigel.deakin_at_oracle.com>wrote:
>
>> Following Pete's proposal (and Chris's support) I have logged this JIRA
>> issue:
>>
>> http://java.net/jira/browse/JMS_SPEC-101
>> New method Message.getPayload(Class<T> c)
>>
>> Please indicate whether you support adding this new method to JMS 2.0.
>>
>> 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);
>>
>>
>>
>>
>