使用 Active MQ 的 JMS 示例
我想学习使用 Active MQ 的 JMS 应用程序。
所以下载了apache-activemq-5.5.1并启动了服务器
并找到了示例代码但不起作用。为什么会出现以下异常?
注意:我将 activemq 库添加到我的项目中,该库包含 org.slf4j 模块
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.MessageListener;
//JMS Producer
public class JMSProducer {
public void produce() {
String url = "tcp://localhost:61616";
ConnectionFactory factory = new ActiveMQConnectionFactory(url);
try {
Connection connection = factory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("TestTopic");
MessageProducer producer = session.createProducer(topic);
TextMessage msg = session.createTextMessage();
msg.setText("Hello JMS World");
producer.send(msg);
}
catch(JMSException exp) {
}
}
}
//JMS Consumer
public class JMSConsumer {
public void consume() {
String url = "tcp://localhost:61616";
ConnectionFactory factory = new ActiveMQConnectionFactory(url);
try {
Connection connection = factory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("TestTopic");
MessageConsumer consumer = session.createConsumer(topic);
JMSMessageListener listener = new JMSMessageListener();
consumer.setMessageListener(listener);
connection.start();
}
catch(JMSException exp) {
}
}
}
//JMS Message Listener
public class JMSMessageListener implements MessageListener {
@Override
public void onMessage(javax.jms.Message msg) {
System.out.println(msg.toString());
}
}
I want to learn JMS application using Active MQ.
So downloaded apache-activemq-5.5.1 and started server
and found sample code but not working. Why following exception happens?
Note: I Added activemq library to my project and the library contains org.slf4j module
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.MessageListener;
//JMS Producer
public class JMSProducer {
public void produce() {
String url = "tcp://localhost:61616";
ConnectionFactory factory = new ActiveMQConnectionFactory(url);
try {
Connection connection = factory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("TestTopic");
MessageProducer producer = session.createProducer(topic);
TextMessage msg = session.createTextMessage();
msg.setText("Hello JMS World");
producer.send(msg);
}
catch(JMSException exp) {
}
}
}
//JMS Consumer
public class JMSConsumer {
public void consume() {
String url = "tcp://localhost:61616";
ConnectionFactory factory = new ActiveMQConnectionFactory(url);
try {
Connection connection = factory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("TestTopic");
MessageConsumer consumer = session.createConsumer(topic);
JMSMessageListener listener = new JMSMessageListener();
consumer.setMessageListener(listener);
connection.start();
}
catch(JMSException exp) {
}
}
}
//JMS Message Listener
public class JMSMessageListener implements MessageListener {
@Override
public void onMessage(javax.jms.Message msg) {
System.out.println(msg.toString());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.slf4j.org/codes.html#StaticLoggerBinder
http://www.slf4j.org/codes.html#StaticLoggerBinder