如何在ActiveMQ中出列所有消息?

时间:2017-08-22 09:04:21

标签: java activemq

我有这个发送者类,它正在向myQueue发送消息。

   Sender.java

   import javax.jms.Connection;
   import javax.jms.ConnectionFactory;
   import javax.jms.Destination;
   import javax.jms.JMSException;
   import javax.jms.MessageProducer;
   import javax.jms.Session;
   import javax.jms.TextMessage;
   import org.apache.activemq.ActiveMQConnection;
   import org.apache.activemq.ActiveMQConnectionFactory;
   import org.apache.commons.collections.Factory;

   public class Sender {

private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageProducer producer = null;

String [] messages = new String[] {"Hello ...My name is vaniiiiiiiiiii tanejaaaaaaaaaaaaaa",
                                    "Hello ...My name is priyanka tanejaaaaaaaaaaaaaa",
                                    "Hello ...My name is rahul tanejaaaaaaaaaaaaaa",
                                    "Hello ...My name is popo tanejaaaaaaaaaaaaaa"};
   public void sendMessage(){
factory = new ActiveMQConnectionFactory(
        "tcp://localhost:61619");
try {
    connection= factory.createConnection();
    connection.start();
    session= connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    destination=session.createQueue("Parul");
    producer = session.createProducer(destination);
    TextMessage message = session.createTextMessage();
    for(int i=0;i<messages.length;i++){
    message.setText(messages[i]);
    System.out.println("Sent: " + message.getText());
    producer.send(message);
    }



} catch (JMSException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   
}

public static void main(String[] args) {
    Sender sender = new Sender();
    sender.sendMessage();

}

 }

我的消息现已发布到activemq,现在我们要读取来自activemq的消息。所以我写了消费类。

  Consumer.java

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
 import javax.jms.Destination;
 import javax.jms.JMSException;
 import javax.jms.Message;
   import javax.jms.MessageConsumer;
    import javax.jms.MessageListener;
   import javax.jms.Session;
     import javax.jms.TextMessage;

   import org.apache.activemq.ActiveMQConnectionFactory;
   import org.apache.camel.Main;

     public class Reciever implements MessageListener{

private ConnectionFactory factory;
private Connection connection;
private Session session;
private Destination destination;

private MessageConsumer consumer = null;
Reciever(){



}
void recieveMessage(){
    factory= new ActiveMQConnectionFactory("tcp://localhost:61619");
    try {
        connection=factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        System.out.println("session started");
        destination=session.createQueue("Parul");
        consumer = session.createConsumer(destination);
        System.out.println("Consumer created");

          while (true) {     
              System.out.println("About to read");
                Message msg = consumer.receive(5000); 
                if (msg instanceof TextMessage) {
                    TextMessage tm = (TextMessage) msg;
                    System.out.println(tm.getText());       
                }
                else{
                    System.out.println("Queue Empty"); 
                    connection.stop();
                    break;
                }
            }


    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
       public static void main(String[] args) {
      Reciever reciever = new Reciever();
    reciever.recieveMessage();
      }

   }

但我的消费者无法从队列中读取消息。问题是什么你能帮我解决这个问题。我想同时阅读所有4个消息。

0 个答案:

没有答案