简体   繁体   中英

Convert javax.jms.Message to com.ibm.jms.JMSTextMessage

Schema :

com.ibm.jms.JMSTextMessage jtm ---> f(javax.jms.Message m)

I receive a JMSTextMessage , but my function parameter is an interface ( Message ). JMSTextMessage added a function: getText() that doesn't exist in Message .

com.ibm.jms.JMSTextMessage implements javax.jms.Message

How can I call that getText() method in my f function? Is there a method / cast that has to be done?

Of course that m.getText() will not work because getText() doesn't exist in Message .

URLs :

JMSTextMessage

Message

If your m object is an instance of JMSTextMessage (you might want to have a instanceof call to check) then you can just cast it and then call the getText method like this:

(JMSTextMessage)m).getText()

However, you probably don't want to cast it to the actual implementation as this then restricts the portability of your code (ie you would have to rewrite your code for Weblogic for example). So it is better to cast your Message object to the javax.jms.TextMessage interface like this:

(TextMessage)m).getText()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM