6 Replies Latest reply: Jul 26, 2012 6:02 AM by 951645 RSS

    jms convert

    951645
      hi everybody i am new on this site
      just one question about jms

      How can you do a convert of an ObjectMessageImpl in a ObjectMessage ?
      i just want to make the object safe of the number

      when i compile i get the error cannot convert ObjectMessageImpl in MessageQ1

      public MessageQ1 receive() throws JMSException {

      Message msg = replyProducer.receive();
      ObjectMessage fins = (ObjectMessage) msg;

      MessageQ1 marseille = new MessageQ1("");
      marseille = (MessageQ1) fins;

      return marseille;
      }
        • 1. Re: jms convert
          nigeldeakin
          >
          MessageQ1 marseille = new MessageQ1("");
          marseille = (MessageQ1) fins;
          Why do you initialise the variable "marseille" to a newly-created MessageQ1 instance, and then immediately overwrite it? This is a programming error of some kind.

          As for the cast error, you're trying to cast a ObjectMessageImpl (your JMS provider's implementation of the javax.jms.Message interface) to a MessageQ1 and getting an exception. That's because ObjectMessageImpl is not a MessageQ1.

          What are you trying to achieve? You say "How can you do a convert of an ObjectMessageImpl in a ObjectMessage ?"

          You don't need to convert it. An ObjectMessageImpl is already an ObjectMessage (i.e. ObjectMessage implements the ObjectMessage interface).

          Nigel

          Edited by: nigeldeakin on 25-Jul-2012 03:23
          • 2. Re: jms convert
            951645
            thanks for your first answer

            just because i want to cast the ObjectMessage into a MessageQ1 object.
            for the return of the "function":

            public MessageQ1 receive throws JMSException {

            when i realize the operation with the jms server i get the following error:

            ObjectMessageImpl cannot be cast to question1.MessageQ1

            Edited by: 948642 on 25 juil. 2012 06:08

            Edited by: 948642 on 25 juil. 2012 06:11

            Edited by: 948642 on 25 juil. 2012 06:11
            • 3. Re: jms convert
              nigeldeakin
              I want to cast the ObjectMessage into a MessageQ1 object.
              You can only cast an object to a MessageQ1 if the object in question is a MessageQ1. But it isn't, it is an ObjectMessageImpl, which is a class provided by your JMS provider which implements the ObjectMessage interface.

              (This is just a guess, but is the message payload a MessageQ1? If so then you need to call getObject() to extract it from the ObjectMessage)

              Nigel
              • 4. Re: jms convert
                951645
                okay i have follow your instructions with this code

                MessageQ1 m = (MessageQ1) ((ObjectMessage)msg).getObject();

                but when i do this the server says the result of the extraction is a java.lang.String, what i should get as you say is an object to make the cast

                i summarize here the sending in the consumer class

                public void send(String s) throws JMSException
                {
                MessageQ1 envoi = null;
                envoi = new MessageQ1(s);
                try{

                TextMessage messagep = session.createTextMessage();
                messagep.setText(envoi.toString());
                messagep.setJMSReplyTo(destb);
                requestConsumer.send(messagep);
                ===================================
                and the answer by the producer class

                public void onMessage(Message message) {
                try {
                if (message instanceof TextMessage)
                {
                     TextMessage requestMessage = (TextMessage) message;

                     String contents = requestMessage.getText();
                     Destination replyDestination = message.getJMSReplyTo();
                     MessageProducer replyProducer = session.createProducer(replyDestination);

                     TextMessage replyMessage = session.createTextMessage();
                     
                     
                     ObjectMessage obj = session.createObjectMessage();
                obj.setObject(contents);
                     
                     replyProducer.send(obj);
                ========================================
                here is the constructor of the class MessageQ1 it's better for understanding get all the elements.
                MessageQ1 is an object with two elements not only a String

                     public MessageQ1(String texte){
                          this.texte = texte;
                          this.estampille = new Date();
                     }

                thanks again

                Edited by: 948642 on 25 juil. 2012 08:11

                Edited by: 948642 on 25 juil. 2012 08:11

                Edited by: 948642 on 25 juil. 2012 08:20
                • 5. Re: jms convert
                  nigeldeakin
                       String contents = ...
                  ...
                       
                       ObjectMessage obj = session.createObjectMessage();
                  obj.setObject(contents);
                       
                       replyProducer.send(obj);
                  Yes, so if the code that receives that message calls getObject() you will get a String - the same String you put in it.

                  If you want to use that String to create a MessageQ1 then do so - that's your code, nothing to do with JMS.

                  Nigel
                  • 6. Re: jms convert
                    951645
                    thanks i have successfuly done my code.