3 Replies Latest reply: Nov 29, 2010 9:23 AM by Nigeldeakin-Oracle RSS

    Strange ClassCastException

    812254
      Hi guys, I'm doing some prototypes with glassfish+openmq, and I'm getting a weird ClassCastException here. If I run openMQ embedded everything works fine, but if I run it separately (LOCAL or REMOTE configurations) I get the following exception

      SEVERE: java.lang.ClassCastException: com.sun.messaging.Queue cannot be cast to com.sun.messaging.Destination
           at com.sun.messaging.jmq.jmsclient.ProtocolHandler.createMessageProducer(ProtocolHandler.java:1231)
           at com.sun.messaging.jmq.jmsclient.MessageProducerImpl.<init>(MessageProducerImpl.java:115)
           at com.sun.messaging.jmq.jmsclient.QueueSenderImpl.<init>(QueueSenderImpl.java:64)
           at com.sun.messaging.jmq.jmsclient.UnifiedSessionImpl.createSender(UnifiedSessionImpl.java:164)
           at com.sun.messaging.jmq.jmsclient.UnifiedSessionImpl.createProducer(UnifiedSessionImpl.java:516)
           at com.sun.messaging.jms.ra.SessionAdapter.createProducer(SessionAdapter.java:234)

      More info: glassfish v3 and openmq v 4.2.2 (apparently)

      Any ideias?

      Edited by: 809251 on 26-Nov-2010 01:19
        • 1. Re: Strange ClassCastException
          Nigeldeakin-Oracle
          That's strange. When using LOCAL/REMOTE mode I would expect to see exactly that cast taking place. And since com.sun.messaging.Queue is a subclass of com.sun.messaging.Destination the cast should work.

          Please give more information about your application and how you built it. An "impossible" ClassCastException like that can mean a class loader problem. Are there any MQ jars in your application (there shouldn't be)? Did you copy any of the MQ jars to anywhere else?

          Nigel
          MQ team
          • 2. Re: Strange ClassCastException
            812254
            So, I'm using Eclipse and packaging/deploying the application with it's plugins while I'm in development.
            I didn't copy any libs to anywhere, but I did put jms.jar (glassfish3\mq\lib) in my application classpath
            @LocalBean
            @Stateless
            public class JMSService {
                 
                 @Resource(mappedName="jms/QueueFactory")
                 private ConnectionFactory queueFactory;
            
                 public void doSomething(String queue, String message) {
                      try {
                           Connection topicConnection = queueFactory.createConnection();
                           Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                           Destination myDest = new Queue(queue);
                           MessageProducer producer = session.createProducer(myDest);
                           TextMessage message = session.createTextMessage();
                           message.setText(message);
                           producer.send(message);
                           producer.close();
                           session.close();
                           topicConnection.close();               
                      } catch (Exception e) {
                           e.printStackTrace();
                      }
                 }
            
            }
            That's how I'm doing it.

            Thanks

            Edited by: 809251 on 29-Nov-2010 05:30
            • 3. Re: Strange ClassCastException
              Nigeldeakin-Oracle
              The only line of code that caught my eye is
              Destination myDest = new Queue(queue);
              I presume that's a com.sun.messaging.Queue you're creating there.

              Although I would expect that to work, it's not generally considered good practice because it hardwires MQ class names in your code and so makes it non-portable between JMS providers. It's better to use session.createQueue(queueName) instead.

              However the recommended JavaEE way to do this is to obtain your Queue object by JNDI lookup of a destination resource in GlassFish. Please try this and see if you still get the error. You can do this using annotation, just like you've done with the connection factory. This certainly works for me...

              Nigel