ACTIVEMQ Topic消息 生产者 发布消息后 消费者收不到消息

 

生产者

public class Producer {
     public static void main(String[] args) throws JMSException {  
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");  
            Connection connection = factory.createConnection();  
            connection.start();  
              
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
            ActiveMQTopic topic= new ActiveMQTopic("testTopic");  
      
            MessageProducer producer = session.createProducer(topic);  
           producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
      
    
            for(int i=0; i<10; i++){
                TextMessage message = session.createTextMessage();  
               message.setText("message_" + System.currentTimeMillis());  
               producer.send(message);  
               System.out.println("Sent message: " + message.getText());  
            }
              
           
      
//        session.close();  
//        connection.stop();  
//        connection.close();  
        }  
}

发布消息的结果
Sent message: message_1341915173083
Sent message: message_1341915173085
Sent message: message_1341915173085
Sent message: message_1341915173086
Sent message: message_1341915173086
Sent message: message_1341915173086
Sent message: message_1341915173087
Sent message: message_1341915173087
Sent message: message_1341915173088
Sent message: message_1341915173088






消费者
public class Consumer {
    public static void main(String[] args) throws JMSException {  
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");  
        Connection connection = factory.createConnection();  
        connection.start();  
          
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
        ActiveMQTopic topic= new ActiveMQTopic("testTopic");
       // javax.jms.Topic topic =  session.createTopic("myTopic.messages");  
  
        MessageConsumer consumer = session.createConsumer( topic);  
        consumer.setMessageListener(new MessageListener() {  
            public void onMessage(Message message) {  
                TextMessage tm = (TextMessage) message;  
                try {  
                    System.out.println("Received message: " + tm.getText());  
                } catch (JMSException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
        
        
        
        MessageConsumer comsumer2 = session.createConsumer(topic);
        comsumer2.setMessageListener(new MessageListener(){
             public void onMessage(Message message) {  
                 TextMessage tm = (TextMessage) message;  
                 try {  
                     System.out.println("Received message: " + tm.getText());  
                 } catch (JMSException e) {  
                     e.printStackTrace();  
                 }  
             }  
        });
//      session.close();  
//      connection.stop();  
//      connection.close();  
    }  
}
消费者运行程序后后获取不到生产者发布的消息,初识 ActiveMQ不太熟悉,  求解答

 

 

1个回答

亲测 搞了个小例子 我的例子代码来自:
http://www.open-open.com/lib/view/open1328079945062.html
然后跑了一下 消费者能接到消息

ActiveMQTopic topic= new ActiveMQTopic("testTopic"); 怀疑你这个代码原因
觉得应该用这种方式
Topic topic = session.createTopic("testTopic");

另外我测试过程我写了个小博客 我的博客地址:[url]http://babydeed.iteye.com/blog/1584561[/url] 里面含有我的代码

works001
works001 说道这里 你应该明白了
大约 8 年之前 回复
works001
works001 ActiveMQTopic topic= new ActiveMQTopic("testTopic"); 这种方式 消费者和生产者未必是同一topic
大约 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问