我如何向Activemq发送消息

时间:2013-08-08 11:25:34

标签: java jms activemq java-ee-6

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

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

enter image description here

现在我想使用JMSActivemq发送来自java代码的消息。为此,我编写了以下代码。并使用Apache Tomcat server.it运行我的代码

此代码在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)
你可以建议我,我写错了。

感谢。

2 个答案:

答案 0 :(得分:5)

下载common-logging.ja r文件

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

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

答案 1 :(得分: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章的例子 - &gt; http://code.google.com/p/activemq-in-action/source/browse/#svn%2Ftrunk%2Fexamples%2Fchapter8%2Fjms-webapp-jboss%253Fstate%253Dclosed

的问候,