使用Active MQ的示例JMS示例

时间:2012-02-15 10:01:42

标签: java jms activemq

我想学习使用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());
           }
       }

1 个答案:

答案 0 :(得分:5)

  

org.slf4j.impl.StaticLoggerBinder时会报告此错误   class无法加载到内存中。没有时会发生这种情况   可以在类路径上找到适当的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