JMS中的ClassCastException在JNDI中查找ConnectionFactory

时间:2012-04-28 03:59:57

标签: spring-mvc jms jndi jboss5.x

我是Queues的新手。我已经在JBOSS 5.1 EAP中设置了我的队列,现在我在尝试注入队列时得到ClassCastException(查找JNDI),我在前端使用flex并使用Spring框架进行flex。当使用石英作业注入队列时,它运行良好且良好。石英工作是一个单独的项目,与弹性和弹簧无关。

我读过一些可能是罐子问题的地方,我春天有JMS罐子,我尝试去掉,更换所有类型的罐子,没有运气。

        // Step 1. Create an initial context to perform the JNDI lookup.

        InputStream in = this.getClass().getResourceAsStream(
                "/clv2.properties");
        Properties configProp = new Properties();
        configProp.load(in);
        IpAddress = configProp.getProperty("ipaddress");
        port = configProp.getProperty("port");
        inQueueName = configProp.getProperty("inQueueName");
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial",
                "org.jnp.interfaces.NamingContextFactory");
        props.setProperty("java.naming.factory.url.pkgs",
                "org.jboss.naming");
        // props.setProperty("java.naming.provider.url", host + ":" + port);
        props.setProperty("java.naming.provider.url", "localhost" + ":"
                + 1099);
        // props.setProperty("java.naming.provider.url",
        // "16.181.233.61:1399");
        initialContext = new InitialContext(props);

        // Step 3. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext
                .lookup("/ConnectionFactory");
        Queue queue = (Queue) initialContext
                .lookup(/* "/queue/CLVInboundQueue" */inQueueName);
        // Step 4.Create a JMS Connection
        connection = (QueueConnection) cf.createConnection();

        // Step 5. Create a JMS Session
        session = (QueueSession) connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

        // Step 6. Create a JMS Message Producer
        // MessageProducer producer = session.createProducer(queue);
        QueueSender queueSender = session.createSender(queue);
        TextMessage textMessage = session.createTextMessage(message);
        textMessage.setLongProperty("Rejected_Message_ID",
                rejected_Message_Id);
        /*
         * BufferedReader reader = new BufferedReader(new
         * InputStreamReader(ClassLoader.class.getResourceAsStream(file)));
         * StringBuilder sb = new StringBuilder(); String line = null;
         * 
         * 
         * while ((line = reader.readLine()) != null) { sb.append(line +
         * "\n"); } String announcementmsg = sb.toString();
         */
        commonlogger.info(textMessage);
        connection.start();
        // producer.send(session.createTextMessage(announcementmsg));
        queueSender.send(textMessage);

这是我要编入队列的编码。我在“//步骤3.在连接工厂上执行查找”中获得了异常,这是我的堆栈跟踪。

09:22:20,730 ERROR [STDERR] java.lang.ClassCastException: org.jboss.jms.client.JBossConnectionFactory cannot be cast to javax.jms.QueueConnectionFactory
09:22:20,731 ERROR [STDERR]     at com.cat.clv.util.InQueueReinjectMessage.sendMessage(InQueueReinjectMessage.java:63)
09:22:20,731 ERROR [STDERR]     at com.cat.clv.util.RejectedMessageReinject.reProcessedMessage(RejectedMessageReinject.java:65)
09:22:20,731 ERROR [STDERR]     at com.cat.clv.service.ReinjectMessagesServiceImpl.reinjectRejectedMessages(ReinjectMessagesServiceImpl.java:106)
09:22:20,731 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
09:22:20,731 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
09:22:20,732 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
09:22:20,732 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Unknown Source)
09:22:20,732 ERROR [STDERR]     at flex.messaging.services.remoting.adapters.JavaAdapter.invoke(JavaAdapter.java:421)
09:22:20,732 ERROR [STDERR]     at flex.messaging.services.RemotingService.serviceMessage(RemotingService.java:183)
09:22:20,732 ERROR [STDERR]     at flex.messaging.MessageBroker.routeMessageToService(MessageBroker.java:1503)

2 个答案:

答案 0 :(得分:2)

如果您查看JBossConnectionFactory的来源,那么您会看到它实现了QueueConnectionFactory

所以这个类强制转换异常可能是因为:

  1. JBoss的错误安装
  2. 客户端和服务器中的不同jms.jar
  3. 首先可以尝试选项2并查看它是否有效,如果没有,您可以检查这些示例是否适用于您的jboss安装。

答案 1 :(得分:0)

在查找时获取classCast异常: classcastexception:weblogic.jms.client.JMSconnectionFactory无法强制转换为javax.jms.QueueConnectionFactory

initialContext = new InitialContext(props);

    // Step 3. Perform a lookup on the Connection Factory
    QueueConnectionFactory cf = (QueueConnectionFactory) initialContext
            .lookup("/ConnectionFactory");
    Queue queue = (Queue) initialContext
            .lookup(/* "/queue/CLVInboundQueue" */inQueueName);
    // Step 4.Create a JMS Connection
    connection = (QueueConnection) cf.createConnection();

    // Step 5. Create a JMS Session
    session = (QueueSession) connection.createSession(false,
            Session.AUTO_ACKNOWLEDGE);

    // Step 6. Create a JMS Message Producer
    // MessageProducer producer = session.createProducer(queue);
    QueueSender queueSender = session.createSender(queue);
    TextMessage textMessage = session.createTextMessage(message);
    textMessage.setLongProperty("Rejected_Message_ID",
            rejected_Message_Id);
    /*
     * BufferedReader reader = new BufferedReader(new
     * InputStreamReader(ClassLoader.class.getResourceAsStream(file)));
     * StringBuilder sb = new StringBuilder(); String line = null;
     * 
     * 
     * while ((line = reader.readLine()) != null) { sb.append(line +
     * "\n"); } String announcementmsg = sb.toString();
     */
    commonlogger.info(textMessage);
    connection.start();
    // producer.send(session.createTextMessage(announcementmsg));
    queueSender.send(textMessage);