4

我从不工作JMS。最近我在所有文件中下载Activemq并更改了端口号。我从61616命令提示符运行以下命令并在浏览器上打开控制台页面。61617conf/activemq-*.xml

  C:\Users\Infratab Bangalore\Desktop\Queueing\apache-activemq-5.8.0\bin>activemq

在此处输入图像描述

现在我想使用JMSto从 java 代码发送消息Activemq。为此,我编写了以下代码。并使用 Apache Tomcat 服务器运行我的代码。它不起作用

此代码在 Eclipse 中实现。

package PackageName;

import java.io.IOException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MessageProducer extends HttpServlet {
    @Override
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        try {
            //created ConnectionFactory object for creating connection 
            ConnectionFactory factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61617");
            //Establish the connection
            Connection connection = factory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("Test");
            //Added as a producer
            javax.jms.MessageProducer producer = session.createProducer(queue);
            // Create and send the message
            TextMessage msg = session.createTextMessage();
            msg.setText("TestMessage");
            producer.send(msg);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

我收到以下错误

 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
org.apache.activemq.ActiveMQPrefetchPolicy.<clinit>(ActiveMQPrefetchPolicy.java:30)
org.apache.activemq.ActiveMQConnectionFactory.<init>(ActiveMQConnectionFactory.java:88)
PackageName.MessageProducer.service(MessageProducer.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

你能建议我吗,我在哪里写错了。

谢谢。

4

2 回答 2

5

从下载 common-logging.jar 文件

http://grepcode.com/snapshot/repo1.maven.org/maven2/commons-logging/commons-logging/1.1.3/

并将其放入您的类路径并再次运行。

于 2013-08-08T11:39:59.823 回答
2

该消息来自 ActiveMQPrefetchPolicy 的第 38 行,该行发生在类的初始化期间(因为它是一个静态字段)(grepcode http://grepcode.com/file/repository.springsource.com/org 中的行示例。 apache.activemq/com.springsource.org.apache.activemq/5.3.0/org/apache/activemq/ActiveMQPrefetchPolicy.java#38)。您将需要 common-logging.jar,因为它是运行应用程序类路径中的一个依赖项。您可能会遇到其他错误。我建议您在互联网上关注一些示例,例如 ActiveMQ in Action 第 8 章的示例 -> http://code.google.com/p/activemq-in-action/source/browse/# svn%2Ftrunk%2Fexamples%2Fhapter8%2Fjms-webapp-jboss%253Fstate%253Dclose

问候,

于 2013-08-08T13:11:14.873 回答