使用 Active MQ 的 JMS 示例

发布于 2025-01-05 11:32:11 字数 2382 浏览 2 评论 0原文

我想学习使用 Active MQ 的 JMS 应用程序。
所以下载了apache-activemq-5.5.1并启动了服务器
并找到了示例代码但不起作用。为什么会出现以下异常?
注意:我将 activemq 库添加到我的项目中,该库包含 org.slf4j 模块

exception

 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

exception

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2025-01-12 11:32:11

org.slf4j.impl.StaticLoggerBinder 时报此错误
类无法加载到内存中。当没有
可以在类路径上找到适当的 SLF4J 绑定。配售
slf4j-nop.jar、slf4j-simple.jar 之一(且仅有一个)、
类上的 slf4j-log4j12.jar、slf4j-jdk14.jar 或 logback-classic.jar
路径应该可以解决问题。

从 1.6.0 开始,从 SLF4J 版本 1.6 开始,在没有绑定的情况下,
SLF4J 将默认采用无操作 (NOP) 记录器实现。

您可以从项目下载页面下载 SLF4J 绑定。

http://www.slf4j.org/codes.html#StaticLoggerBinder

This error is reported when the org.slf4j.impl.StaticLoggerBinder
class could not be loaded into memory. This happens when no
appropriate SLF4J binding could be found on the class path. Placing
one (and only one) of slf4j-nop.jar, slf4j-simple.jar,
slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class
path should solve the problem.

since 1.6.0 As of SLF4J version 1.6, in the absence of a binding,
SLF4J will default to a no-operation (NOP) logger implementation.

You can download SLF4J bindings from the project download page.

http://www.slf4j.org/codes.html#StaticLoggerBinder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文