14 Replies Latest reply on Feb 4, 2009 7:33 AM by chicken

    ClassCastException when casting ObjectMessage object

      Dear all

      I have a problem with JMS on JBoss 4.3.0.
      My queue works well but whenever after I reload my application I could not cast the queue object to my custom message object (CustomMessageBean ):

      ERROR: java.lang.ClassCastException: CustomMessageBean

      public void onMessage(Message message) {
       try {
       if (message instanceof ObjectMessage) {
       String messageId = message.getJMSMessageID();
       ObjectMessage qObjectMessage = (ObjectMessage) message;
       CustomMessageBean mailBeanJMS = (CustomMessageBean) qObjectMessage
       .getObject();
       MailMgr receiverMailMgr = new MailMgr();
       rlog.debug("messageid: " + messageId);
       ......
      
       }
       } catch (Exception e) {
       System.out.println("ERROR: " + e.toString());
       }
      }


      Here is configuration content for the queue:

      <mbean code="org.jboss.jms.server.destination.QueueService"
      name="jboss.messaging.destination:service=Queue,name=myQueue"
       xmbean-dd="xmdesc/Queue-xmbean.xml">
       <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
       <depends>jboss.messaging:service=PostOffice</depends>
      </mbean>


      To solve this problem, I have to restart JBoss Server every times I apply new source code!

      please help me solve this problem.

      Thanks in advance!

        • 1. Re: ClassCastException when casting ObjectMessage object
          gaohoward

          Do you mean the onMessage() method will always throw the exception? What do you mean by reload? and can you post your CustomBessageBean here? Thanks.

          • 2. Re: ClassCastException when casting ObjectMessage object

            thank you so much for your quickly help

            I mean I reload my web application: I usually modify the web.xml in my application so that the application reloads and applies my new source.

            the onMessage() method only throws the exception after I reload my application and run the function that uses JMS.
            usually it works well.
            When I develop my web application, I usually have to reload my application, and I always face the exception so that I have to restart JBoss application server (not reload my application) and It will take more time than reloading my web application.

            here is my CustomMessageBean.java:

            import java.io.Serializable;
            import java.util.Date;
            
            public class CustomMessageBean implements Serializable {
             private String server;
            
             private String from;
            
             private String to;
            
             private String toNew;
            
             private String cc;
            
             private String bcc;
            
             private String subject;
            
             private String content;
            
             private boolean isHTML;
            
             private String charSet;
            
             private String attachment;
            
             private Date date;
            
             private String admMemNo;
            
             private String user_id;
            
             private String serverName;
            
             public MessageBean() {
             }
            
             public CustomMessageBean (String server, String from, String to, String toNew,
             String cc, String bcc, String subject, String content,
             boolean isHTML, String charSet, String attachment, Date date,
             String admMemNo, String user_id, String serverName) {
             this.server = server;
             this.from = from;
             this.to = to;
             this.toNew = toNew;
             this.cc = cc;
             this.bcc = bcc;
             this.subject = subject;
             this.content = content;
             this.isHTML = isHTML;
             this.charSet = charSet;
             this.attachment = attachment;
             this.date = date;
             this.admMemNo = admMemNo;
             this.user_id = user_id;
             this.serverName = serverName;
             }
            
             public void setServer(String server) {
             this.server = server;
             }
            
             public String getServer() {
             return server;
             }
            
             public void setFrom(String from) {
             this.from = from;
             }
            
             public String getFrom() {
             return from;
             }
            
             public void setTo(String to) {
             this.to = to;
             }
            
             public String getTo() {
             return to;
             }
            
             public void setToNew(String toNew) {
             this.toNew = toNew;
             }
            
             public String getToNew() {
             return toNew;
             }
            
             public void setCc(String cc) {
             this.cc = cc;
             }
            
             public String getCc() {
             return cc;
             }
            
             public void setBcc(String bcc) {
             this.bcc = bcc;
             }
            
             public String getBcc() {
             return bcc;
             }
            
             public void setSubject(String subject) {
             this.subject = subject;
             }
            
             public String getSubject() {
             return subject;
             }
            
             public void setContent(String content) {
             this.content = content;
             }
            
             public String getContent() {
             return content;
             }
            
             public void setIsHTML(boolean isHTML) {
             this.isHTML = isHTML;
             }
            
             public boolean getIsHTML() {
             return isHTML;
             }
            
             public void setCharSet(String charSet) {
             this.charSet = charSet;
             }
            
             public String getCharSet() {
             return charSet;
             }
            
             public void setAttachment(String attachment) {
             this.attachment = attachment;
             }
            
             public String getAttachment() {
             return attachment;
             }
            
             public void setDate(Date date) {
             this.date = date;
             }
            
             public Date getDate() {
             return date;
             }
            
             public void setAdmMemNo(String admMemNo) {
             this.admMemNo = admMemNo;
             }
            
             public String getAdmMemNo() {
             return admMemNo;
             }
            
             public void setUser_id(String user_id) {
             this.user_id = user_id;
             }
            
             public String getUser_id() {
             return user_id;
             }
            
             public void setServerName(String serverName) {
             this.serverName = serverName;
             }
            
             public String getServerName() {
             return serverName;
             }
            }


            • 3. Re: ClassCastException when casting ObjectMessage object
              gaohoward

              ok, please try to give a serialVersionID to your Bean class, for example:

              private static final long serialVersionUID = 8462102430717730566L;
              



              • 4. Re: ClassCastException when casting ObjectMessage object

                after I add the serialVersionID, i get the old error and new error about log4j, I am using log4j 1.2.15

                17:08:13,650 INFO [STDOUT] ERROR: java.lang.ClassCastException: CustomMessageBean
                17:08:13,652 INFO [WebappClassLoader] Illegal access: this web application instance has been stopped already. Could not load org.apache.log4j.spi.ThrowableInformation. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
                java.lang.IllegalStateException
                 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1244)
                 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
                 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                 at org.apache.log4j.spi.LoggingEvent.<init>(LoggingEvent.java:159)
                 at org.apache.log4j.Category.forcedLog(Category.java:391)
                 at org.apache.log4j.Category.error(Category.java:322)
                 at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:146)
                 at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                 at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                 at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                 at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                 at java.lang.Thread.run(Thread.java:595)
                17:08:13,658 ERROR [ClientConsumer] Failed to deliver message
                java.lang.NoClassDefFoundError: org/apache/log4j/spi/ThrowableInformation
                 at org.apache.log4j.spi.LoggingEvent.<init>(LoggingEvent.java:159)
                 at org.apache.log4j.Category.forcedLog(Category.java:391)
                 at org.apache.log4j.Category.error(Category.java:322)
                 at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:146)
                 at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                 at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                 at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                 at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                 at java.lang.Thread.run(Thread.java:595)



                • 5. Re: ClassCastException when casting ObjectMessage object
                  gaohoward

                  Did you make sure you also update your client that sends the message?

                  • 6. Re: ClassCastException when casting ObjectMessage object

                    yes, I did, both sender and receiver side use the same CustomMessageBean class

                    • 7. Re: ClassCastException when casting ObjectMessage object
                      mvecera

                      I'd suspect some class loaders issue.
                      First you can check where are the classes loaded from and what class loaders were used in your onMessage method:

                      System.out.println(CustomMessageBean.class.getClassLoader().toString() + " " + CustomMessageBean.class.getProtectionDomain().getCodeSource().getLocation().toString());
                      System.out.println(message.class.getClassLoader().toString() + " " + message.class.getProtectionDomain().getCodeSource().getLocation().toString());


                      • 8. Re: ClassCastException when casting ObjectMessage object

                        after I added these line of codes in the Catch clause:

                        System.out.println(CustomMessageBean.class.getClassLoader().toString() + " - " + CustomMessageBean.class.getProtectionDomain().getCodeSource().getLocation().toString());
                         System.out.println(mailBeanJMS.getClass().getClassLoader().toString() + " - " + mailBeanJMS.getClass().getProtectionDomain().getCodeSource().getLocation().toString());


                        mailBeanJMS is an object of CustomMessageBean type
                        I get new error:
                        17:59:05,623 INFO [STDOUT] ERROR. : java.lang.ClassCastException: mail.CustomMessageBean
                        17:59:05,624 INFO [WebappClassLoader] Illegal access: this web application instance has been stopped already. Could not load java.security.ProtectionDomain. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
                        java.lang.IllegalStateException
                         at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1244)
                         at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
                         at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                         at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:148)
                         at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                         at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                         at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                         at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                         at java.lang.Thread.run(Thread.java:595)
                        
                        17:59:05,631 INFO [STDOUT] WebappClassLoader
                         delegate: false
                         repositories:
                         - file:/export/home/JBoss/jboss-eap-4.3/jboss-as/server/default/deploy/webapps.war/WEB-INF/classes/mail/CustomMessageBean.class
                        17:59:05,633 ERROR [ClientConsumer] RuntimeException was thrown from onMessage, 139265 will be redelivered
                        java.lang.NullPointerException
                         at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:149)
                         at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                         at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                         at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                         at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                         at java.lang.Thread.run(Thread.java:595)
                        
                        17:59:05,645 INFO [STDOUT] ERROR. : java.lang.ClassCastException: mail.CustomMessageBean
                        17:59:05,646 INFO [STDOUT] WebappClassLoader
                         delegate: false
                         repositories:
                         - file:/export/home/JBoss/jboss-eap-4.3/jboss-as/server/default/deploy/webapps.war/WEB-INF/classes/mail/CustomMessageBean.class
                        17:59:05,647 ERROR [ClientConsumer] RuntimeException was thrown from onMessage, 139265 will be redelivered
                        java.lang.NullPointerException
                         at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:149)
                         at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                         at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                         at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                         at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                         at java.lang.Thread.run(Thread.java:595)
                        17:59:05,649 INFO [STDOUT] onMessage at JMS Receiver
                        17:59:05,652 INFO [STDOUT] ERROR. : java.lang.ClassCastException: mail.CustomMessageBean
                        17:59:05,653 INFO [STDOUT] WebappClassLoader
                         delegate: false
                         repositories:
                         - file:/export/home/JBoss/jboss-eap-4.3/jboss-as/server/default/deploy/webapps.war/WEB-INF/classes/mail/CustomMessageBean.class
                        17:59:05,654 ERROR [ClientConsumer] RuntimeException was thrown from onMessage, 139265 will be redelivered
                        java.lang.NullPointerException
                         at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:149)
                         at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                         at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                         at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                         at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                         at java.lang.Thread.run(Thread.java:595)
                        17:59:05,656 INFO [STDOUT] onMessage at JMS Receiver


                        do you know the cause of problem? I did understand the new API clearly

                        Thank for your help!


                        • 9. Re: ClassCastException when casting ObjectMessage object
                          mvecera

                          Put that code before any typecasting and as I wrote - do not change message in the second line for anything else. The code can be placed immediately after

                          if (message instanceof ObjectMessage)...


                          • 10. Re: ClassCastException when casting ObjectMessage object

                            sorry, I could not add exactly your code because
                            "message.class" will have an compile error "message cannot be resolved to a type"

                            • 11. Re: ClassCastException when casting ObjectMessage object
                              mvecera

                              Oh, I'm sorry, you have to write

                              message.getClass()



                              • 12. Re: ClassCastException when casting ObjectMessage object

                                I change the two lines to

                                if (message instanceof ObjectMessage) {
                                 System.out.println(CustomMessageBean.class.getClassLoader().toString() + " - " + CustomMessageBean.class.getProtectionDomain().getCodeSource().getLocation().toString());
                                 System.out.println(message.getClass().getClassLoader().toString() + " - " + message.getClass().getProtectionDomain().getCodeSource().getLocation().toString());
                                


                                - Out put before the application is reloaded:

                                09:57:31,957 INFO [STDOUT] onMessage at JMS Receiver
                                09:57:31,965 INFO [STDOUT] WebappClassLoader
                                 delegate: false
                                 repositories:
                                 /WEB-INF/classes/
                                ----------> Parent Classloader:
                                java.net.FactoryURLClassLoader@1e5ed7b
                                 - file:/export/home/JBoss/jboss-eap-4.3/jboss-as/server/default/deploy/webapps.war/WEB-INF/classes/mail/CustomMessageBean.class
                                09:57:31,967 INFO [STDOUT] org.jboss.mx.loading.UnifiedClassLoader3@12bcd4b{ url=null ,addedOrder=2} - file:/export/home/JBoss/jboss-eap-4.3/jboss-as/server/default/lib/jboss-messaging-client.jar
                                


                                - Out put after reloading application:

                                10:01:38,617 INFO [STDOUT] onMessage at JMS Receiver
                                10:01:38,629 INFO [STDOUT] WebappClassLoader
                                 delegate: false
                                 repositories:
                                 - file:/export/home/JBoss/jboss-eap-4.3/jboss-as/server/default/deploy/webapps.war/WEB-INF/classes/mail/CustomMessageBean.class
                                10:01:38,630 INFO [STDOUT] org.jboss.mx.loading.UnifiedClassLoader3@12bcd4b{ url=null ,addedOrder=2} - file:/export/home/JBoss/jboss-eap-4.3/jboss-as/server/default/lib/jboss-messaging-client.jar
                                10:01:38,634 INFO [STDOUT] ERROR: java.lang.ClassCastException: mail.CustomMessageBean
                                10:01:38,635 INFO [WebappClassLoader] Illegal access: this web application instance has been stopped already. Could not load org.apache.log4j.spi.ThrowableInformation. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
                                java.lang.IllegalStateException
                                 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1244)
                                 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
                                 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                                 at org.apache.log4j.spi.LoggingEvent.<init>(LoggingEvent.java:159)
                                 at org.apache.log4j.Category.forcedLog(Category.java:391)
                                 at org.apache.log4j.Category.error(Category.java:322)
                                 at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:150)
                                 at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                                 at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                                 at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                                 at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                                 at java.lang.Thread.run(Thread.java:595)
                                10:01:38,640 ERROR [ClientConsumer] Failed to deliver message
                                java.lang.NoClassDefFoundError: org/apache/log4j/spi/ThrowableInformation
                                 at org.apache.log4j.spi.LoggingEvent.<init>(LoggingEvent.java:159)
                                 at org.apache.log4j.Category.forcedLog(Category.java:391)
                                 at org.apache.log4j.Category.error(Category.java:322)
                                 at mail.AsyncQueueReceiver.onMessage(AsyncQueueReceiver.java:150)
                                 at org.jboss.jms.client.container.ClientConsumer.callOnMessage(ClientConsumer.java:159)
                                 at org.jboss.jms.client.container.ClientConsumer$ListenerRunner.run(ClientConsumer.java:976)
                                 at org.jboss.messaging.util.JBMExecutor$TCLExecutor.run(JBMExecutor.java:99)
                                 at EDU.oswego.cs.dl.util.concurrent.QueuedExecutor$RunLoop.run(QueuedExecutor.java:89)
                                 at java.lang.Thread.run(Thread.java:595)
                                


                                Beside the old error, I also get the error with Log4J :(
                                It is so strange!

                                • 13. Re: ClassCastException when casting ObjectMessage object
                                  mvecera

                                  How are you redeploying the application? Do you have it unpacked or in a archive? Nevertheless, this is probably worth a JIRA bug...

                                  • 14. Re: ClassCastException when casting ObjectMessage object

                                    I deploy my applicaiton in the webapps.war folder:
                                    server/default/deploy/webapps.war
                                    it is unpbacked.
                                    my Application run on Solaris 9.
                                    to redeploy my application:
                                    - I compile and copy new classes to
                                    server/default/deploy/webapps.war/WEB-INF/classes by Ant program.
                                    - then I use VI editor to modify the web.xml
                                    server/default/deploy/webapps.war/WEB-INF/web.xml
                                    (only insert a bank line, and delete the blank line and quit: "wq")
                                    when the web.xml is modified, my application is reload

                                    Perhaps it is a bad way to reload an application, but I don't know the better way, I am new with JBoss :)

                                    Thanks for your patience