2

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.

nichu09
  • 882
  • 2
  • 15
  • 44

2 Answers2

2

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'.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • please check this link.http://stackoverflow.com/questions/19705413/how-to-get-the-message-from-temporary-queue-in-different-session/19708727?noredirect=1#19708727 – nichu09 Nov 05 '13 at 08:34
1
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.

Trying
  • 14,004
  • 9
  • 70
  • 110