Hello folks,
I'm an ActiveMQ noob and I'm trying to figure out how to write a program to
loop through the contents of the queue and having a devil of a time.  I've
bought the Manning book and tried my best on google but have been unable to
find out how.

>From what I'm reading, it seems like I should set up an asynchronous
connection and implement the MessageListener interface.  But when I do, I
don't usually seem to get back anything, let alone loop through the entire
queue.  I was able to achieve the results in a pretty straight forward
manner using the Stomp client, so I think it is my Java.  Any suggestions on
where I've went awry:

import javax.jms.*;
import javax.naming.*
import org.apache.activemq.*;
import java.util.Date;

public class Consumera implements MessageListener {

ActiveMQConnectionFactory connectionFactory;
Context ctx;
Connection connection;
Session session;
MessageConsumer consumer;
boolean useTransaction = false;
Queue queue;
Message message;
 public void createConsumer() throws JMSException, NamingException {
try {

ctx = new InitialContext();
 //this section is for local create
        // Create a ConnectionFactory

// ditrect connection factory
//connectionFactory = new ActiveMQConnectionFactory(username, password,
brokerURL);
//jndi connection factory
connectionFactory =
(ActiveMQConnectionFactory) ctx.lookup("remoteConsumerConnectionFactory");

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


        // Create a Session
        session = connection.createSession(useTransaction,
Session.AUTO_ACKNOWLEDGE);
        // Create the destination (Topic or Queue)
        queue= (Queue) ctx.lookup("GoogleGroupQueue");


        consumer = session.createConsumer(queue);
        consumer.setMessageListener(this);
        connection.start();

        // Create a MessageProducer from the Session to the Topic or Queue
    }

    catch (Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}

public void onMessage(Message message) {
System.out.println("Received message: " );
//if (message instanceof TextMessage) {
// System.out.println("Received message: " + message);
//}
}
 public void closePublisher() throws JMSException {
try {
        // Clean up
     consumer.close();
        session.close();
        connection.close();
        System.out.println("Closed connection");
    }
    catch (Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}

public static void main(String[] args) throws JMSException {

     Consumera consumer = new Consumera();
     try{
     consumer.createConsumer();
     } catch (NamingException e) {
  e.printStackTrace();
  } finally {
     consumer.closePublisher();
     }
}
}




Mike

Reply via email to