如何将PooledConnectionFactoryBean与ActiveMQ一起使用

时间:2013-08-12 00:59:06

标签: java jms activemq

首先,我是JSM的新手。 我正在尝试为JMS ActiveMQ创建一个连接池 使用连接工厂ActiveMQConnectionFactory我成功发送/接收消息,但是使用PooledConnectionFactoryBean它失败并出现此错误:

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/springframework/beans/factory/FactoryBean
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.FactoryBean
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 14 more
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/apache/activemq/pool/PooledConnectionFactoryBean
    at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
    at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-2" java.lang.NoClassDefFoundError: org/apache/activemq/pool/PooledConnectionFactoryBean
    at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
    at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-3" java.lang.NoClassDefFoundError: org/apache/activemq/pool/PooledConnectionFactoryBean
    at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
    at java

.lang.Thread.run(Thread.java:722)

以下是源代码:

package jmxproducer;

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactoryBean;

public class JMXProducerTest implements Runnable {

    private String message;

    public JMXProducerTest(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public void run() {
        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

            PooledConnectionFactoryBean pool = new PooledConnectionFactoryBean();
            pool.setConnectionFactory(connectionFactory);

            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue("MY.QUEUE");

            // Create a MessageProducer from the Session to the Topic or Queue
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // Create a messages
            TextMessage msg = session.createTextMessage(message);
            producer.send(msg);
            session.close();
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

我错过了什么?我在类路径中添加了这个jar:activemq-all-5.8.0.jar

谢谢, kdureidy

1 个答案:

答案 0 :(得分:0)

您是否将Spring库添加到项目中?

错误

java.lang.ClassNotFoundException: org.springframework.beans.factory.FactoryBean

似乎表明你的项目中缺少spring-beans-X.jar(其中X是你缺少的Spring版本)。