activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Tully <gary.tu...@gmail.com>
Subject Re: Asynchronously emptying a queue
Date Thu, 17 Feb 2011 17:24:57 GMT
have a look at the source to the ConsumerTool in the examples in the
activemq binary distribution. That can do both sync and async consumption.

http://svn.apache.org/viewvc/activemq/trunk/assembly/src/release/example/src/ConsumerTool.java?view=markup
http://activemq.apache.org/examples.html


On 17 February 2011 16:34, McDermott, Michael
<michael_mcdermott@brown.edu>wrote:

> Nag,
> Thanks for the reply.  I missed the link to the forum on the ActiveMQ site.
> I'll go back and find it.  Thanks for the heads up.
>
> I'm just writing a client that reads a queue.  The code for posting to the
> queue works just fine, and I can pull messages of synchronously and with
> the
> stomp client.  I'm just missing something in my understanding and code that
> is preventing my asynchronous consumer from looping through a queue until
> it
> is empty.
>
> Mike
>
>
>
> On Thu, Feb 17, 2011 at 11:25 AM, Nakarikanti, Nageswara <
> NNakarikanti@dwd.in.gov> wrote:
>
> > For fast replys post it to Forum
> >
> > I did not see any code in your program that is responsible for producing
> > the messages into Queue you created?
> >
> >
> > Nag.
> >
> > -----Original Message-----
> > From: McDermott, Michael [mailto:michael_mcdermott@brown.edu]
> > Sent: Monday, February 14, 2011 7:44 AM
> > To: users@activemq.apache.org
> > Subject: Asynchronously emptying a queue
> >
> > 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
> >
>
>
>
> --
> Michael J. McDermott
> Lead Developer, Identity and Access Management
> Brown University
>
> Sent from someone else's iphone.
>



-- 
http://blog.garytully.com
http://fusesource.com

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message