java – 使用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());
           }
       }
最佳答案

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

转载注明原文:java – 使用Active MQ的示例JMS示例 - 代码日志