简体   繁体   中英

How to get Message without creating Queue in Activemq

i am new to activemq. i created one queue and produce one message to that queue from one client. i want to consume that message from another client. for consume the message the code follows

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

    // Create a Connection
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // Create a Session
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);


    // Create the destination (Topic or Queue)
    Destination destination = (Destination) session.createQueue("StaticQueueName");

     MessageConsumer consumer= session.createConsumer(destination);
     Message message = (Message) consumer.receive();
     System.out.println(message.getStringProperty("status"));
      if (message instanceof TextMessage) {
         TextMessage textMessage = (TextMessage) message;
         Text = textMessage.getText();
         System.out.println("Received: " + Text);
     } else {
         System.out.println("Received: " + message);
     }

if we have already knew the queue name then no need to create.i metioning below code.

 Destination destination = (Destination) session.createQueue("StaticQueueName");

so is there any method available if we know the queue name before.so using that queue name we can access the message like getqueue("queue name") instead of create the queue.

If such a queue already exists, then no other queue will be created, the same will be given to you. It will not get 'overridden'.

QueueReceiver receiver= session.createReceiver(queueName);
receiver.setMessageListener(this);

If you know the queueName than just create a receiver it will get the message from the queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM