Return-Path: Delivered-To: apmail-activemq-users-archive@www.apache.org Received: (qmail 52341 invoked from network); 14 Feb 2011 12:44:26 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 14 Feb 2011 12:44:26 -0000 Received: (qmail 57969 invoked by uid 500); 14 Feb 2011 12:44:26 -0000 Delivered-To: apmail-activemq-users-archive@activemq.apache.org Received: (qmail 57675 invoked by uid 500); 14 Feb 2011 12:44:23 -0000 Mailing-List: contact users-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@activemq.apache.org Delivered-To: mailing list users@activemq.apache.org Received: (qmail 57666 invoked by uid 99); 14 Feb 2011 12:44:22 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Feb 2011 12:44:22 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of michael_mcdermott@brown.edu designates 209.85.161.171 as permitted sender) Received: from [209.85.161.171] (HELO mail-gx0-f171.google.com) (209.85.161.171) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Feb 2011 12:44:15 +0000 Received: by gxk8 with SMTP id 8so2440454gxk.2 for ; Mon, 14 Feb 2011 04:43:54 -0800 (PST) MIME-Version: 1.0 Received: by 10.236.103.132 with SMTP id f4mr1214442yhg.70.1297687434436; Mon, 14 Feb 2011 04:43:54 -0800 (PST) Received: by 10.236.95.37 with HTTP; Mon, 14 Feb 2011 04:43:54 -0800 (PST) Date: Mon, 14 Feb 2011 07:43:54 -0500 Message-ID: Subject: Asynchronously emptying a queue From: "McDermott, Michael" To: users@activemq.apache.org Content-Type: multipart/alternative; boundary=0023547c90678f253b049c3d6668 --0023547c90678f253b049c3d6668 Content-Type: text/plain; charset=ISO-8859-1 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 --0023547c90678f253b049c3d6668--