Java 我将如何向 Activemq 发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18124790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How will I send messages to Activemq
提问by Hanumath
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.
我从不工作JMS
。最近我下载Activemq
和改变端口号61616
,以61617
在所有conf/activemq-*.xml
files.I从命令提示符并打开控制台页面上浏览器中运行以下命令。
C:\Users\Infratab Bangalore\Desktop\Queueing\apache-activemq-5.8.0\bin>activemq
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
现在我想使用JMS
to从java代码发送消息Activemq
。为此,我编写了以下代码。并使用 Apache Tomcat 服务器运行我的代码。它不起作用
This code is implemented in Eclipse.
此代码在 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
}
}
}
I am getting the following error
我收到以下错误
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.
你能建议我,我写错的地方。
Thanks.
谢谢。
采纳答案by Ashish Aggarwal
Download common-logging.ja r file from
从以下位置下载 common-logging.jar 文件
http://grepcode.com/snapshot/repo1.maven.org/maven2/commons-logging/commons-logging/1.1.3/
http://grepcode.com/snapshot/repo1.maven.org/maven2/commons-logging/commons-logging/1.1.3/
and place this into your classpath and run again.
并将其放入您的类路径并再次运行。
回答by lcestari
The message is from the line 38 of ActiveMQPrefetchPolicy which occurs during the initialization of the class (due it is an static field) (example of the line in 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) . You will need common-logging.jar as it is a dependency in the classpath of your application to run. You might get other errors. I would recommend you to follow some sample in the internet, for example the example of the Chapter 8 of the ActiveMQ in Action -> http://code.google.com/p/activemq-in-action/source/browse/#svn%2Ftrunk%2Fexamples%2Fchapter8%2Fjms-webapp-jboss%253Fstate%253Dclosed
该消息来自 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 中的第 8 章示例 -> http://code.google.com/p/activemq-in-action/source/browse/# svn%2Ftrunk%2Fexamples%2Fchapter8%2Fjms-webapp-jboss%253Fstate%253Dclosed
Regards,
Luan
问候,
栾