4 Replies Latest reply on May 16, 2008 1:15 PM by adrian.brock

    Class Cast Exception -- By HelloDr

    hellodr

      public void createQueue() throws Exception{
      InitialContext ctx = new InitialContext();
      System.out.println("\n\nFactory is : " + ctx.lookup("ConnectionFactory") + "\n\n");
      SpyConnectionFactory factory = (SpyConnectionFactory) ctx.lookup("ConnectionFactory");
      qCon = factory.createQueueConnection();
      ctx = new InitialContext();
      System.out.println("\n\nQueue is : " + (Object)ctx.lookup("queue/echnQueue") + "\n\n");
      queue = (Queue) ctx.lookup("queue/echnQueue");
      qSess = qCon.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
      qCon.start();
      }

      public void sendMessageToQueue(String message) throws Exception{
      qSender = qSess.createSender(queue);
      TextMessage tm = qSess.createTextMessage(message);
      qSender.send(tm);
      qSender.close();
      System.out.println("\n\n\n\n*****************\n\nMessage Send to Queue And QSender is Closed\n\n************\n\n\n");
      pw.println("***************** Message Send to Queue And QSender is Closed ************");
      }


      I am getting ClassCastException in the red marked line. I am trying to type cast to javax.jms.Queue.Please let me know what to to solve this and also please let me know what changes should I need to do in jbossmq-destination-service.xml and jms-ds.xml
      All the reqd libraries are set in the classpath.

      Regards,
      Dharma

        • 1. Re: Class Cast Exception -- By HelloDr
          genman


          What's the type of the object? E.g.

          Object o = ...
          System.out.println("class is " + o.getClass());

          • 2. Re: Class Cast Exception -- By HelloDr
            hellodr

            Thanks genman..

            The type of the Object is org.jboss.mq.SpyQueue. But I dont need to use SpyQueue or SpyConnectionFactory. I prefer to use QueueConnectionFactory and Queue Connection in my program. But When do a JNDI lookup like
            SpyConnectionFactory factory = (SpyConnectionFactory) ctx.lookup("ConnectionFactory");
            It returs Object of SpyConnectionFactory. What should I do in my Application Server Settings to return QueueConnectionFactory. Also please let me know the JNDI name for it.
            My full Program Code is (Which Throws Exception)


            package com.echain.contacts.web.action;
            import java.io.PrintWriter;

            import javax.jms.QueueConnection;
            import javax.jms.QueueSender;
            import javax.jms.QueueSession;
            import javax.jms.TextMessage;
            import javax.naming.InitialContext;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;

            import org.apache.struts.action.Action;
            import org.apache.struts.action.ActionForm;
            import org.apache.struts.action.ActionForward;
            import org.apache.struts.action.ActionMapping;
            import org.jboss.mq.SpyConnectionFactory;
            import org.jboss.mq.SpyQueue;

            public class ServerAction extends Action {

            QueueConnection qCon;
            QueueSession qSess;
            SpyQueue queue;
            QueueSender qSender;
            PrintWriter pw;

            public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
            try {
            pw = res.getWriter();
            System.out.println("\n\n*********** Creating Queue ***********\n\n");
            pw.println("*********** Creating Queue ***********");
            createQueue();
            System.out.println("\n\n*********** Created Queue ***********\n\n");
            pw.println("*********** Created Queue ***********");
            System.out.println("\n\n*********** Sending Message to Queue ***********\n\n");
            pw.println("*********** Sending Message to Queue ***********");
            sendMessageToQueue("THis is the Message");
            System.out.println("\n\n*********** Message Sent to Queue ***********\n\n");
            pw.println("*********** Message Sent to Queue ***********");
            } catch (Exception e) {
            e.printStackTrace();
            }

            return mapping.findForward("forward.success");
            }

            public void createQueue() throws Exception{
            InitialContext ctx = new InitialContext();
            System.out.println("\n\nFactory is : " + ctx.lookup("ConnectionFactory") + "\n\n");
            SpyConnectionFactory factory = (SpyConnectionFactory) ctx.lookup("ConnectionFactory");
            qCon = factory.createQueueConnection();
            ctx = new InitialContext();
            Object o = ctx.lookup("queue/echnQueue");
            System.out.println("\n\nQueue is : " + o.getClass() + "\n\n");
            queue = (SpyQueue) ctx.lookup("queue/echnQueue");
            qSess = qCon.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            qCon.start();
            }

            public void sendMessageToQueue(String message) throws Exception{
            qSender = qSess.createSender(queue);
            TextMessage tm = qSess.createTextMessage(message);
            qSender.send(tm);
            qSender.close();
            System.out.println("\n\n\n\n*****************\n\nMessage Send to Queue And QSender is Closed\n\n************\n\n\n");
            pw.println("***************** Message Send to Queue And QSender is Closed ************");
            }
            }

            In the above marked redline I am getting an exception
            java.lang.LinkageError: Class javax/jms/QueueConnection violates loader constraints

            Please help me out

            Regards,
            Dharma

            • 3. Re: Class Cast Exception -- By HelloDr
              thameema

              I am using jboss 4.2.2 and facing the same issue. When I try to typecast the SpyConnectionFactory to TopicConnectionFacotry, I am getting ClassCastException.. Anyhelp on this?

              Thanks.

              • 4. Re: Class Cast Exception -- By HelloDr

                This is a basic java question.

                System.out.println(TopicConnectionFactory.class.getClassLoader());
                Class[] interfaces = SpyConnectionFactory.class.getInterfaces();
                for (int i = 0; i < interfaces.length; ++i)
                {
                 Class intf = interfaces
                 System.out.println(intf.getName() + " " + intf.getClassLoader();
                 }
                


                Printing the code source will tell you where it is loading the duplicate classes from.