我将如何向Activemq发送消息 [英] How will I send messages to Activemq
问题描述
我从不从事JMS
的工作.最近我下载了Activemq
,并将所有conf/activemq-*.xml
文件中的端口号从61616
更改为61617
.我从命令提示符处运行以下命令,并在浏览器上打开控制台页面.
I never work on JMS
. Recently I downloaded Activemq
and changed port no from 61616
to 61617
in all conf/activemq-*.xml
files.I run the following command from command prompt and open console page on browser.
C:\Users\Infratab Bangalore\Desktop\Queueing\apache-activemq-5.8.0\bin>activemq
现在我想使用JMS
从Java代码向Activemq
发送消息.为此,我编写了以下代码.并使用Apache Tomcat服务器运行我的代码.它不起作用
Now I want to send messages from java code using JMS
to Activemq
.For this I wrote the following code. And run my code using Apache Tomcat server.it's not working
此代码在Eclipse中实现.
This code is implemented in 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)
你能建议我,我在哪里写错了.
can you suggest me, where I wrote wrong.
谢谢.
推荐答案
从以下位置下载common-logging.ja r文件:
Download common-logging.ja r file from
http://grepcode .com/snapshot/repo1.maven.org/maven2/commons-logging/commons-logging/1.1.3/
并将其放入您的类路径中,然后再次运行.
and place this into your classpath and run again.
这篇关于我将如何向Activemq发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!