activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bai Shen <nab...@cyrrin.com>
Subject Re: Creating simple producer and consumer
Date Thu, 03 May 2007 12:06:22 GMT



jlim wrote:
> 
> Hi,
> 
> Can you check if you're calling the connection's start method 
> (connection.start()) before starting your consumers.
> 
> You could also try running the example consumer that is included in the 
> distribution to check if the problem is with your consumer.
> 
> Regards,
> Jonas
> 

Tried calling connection.start().  That didn't help.  Here's the modified
consumer code that I created.

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SimpleConsumer {
    
    private static final org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory
            .getLog(SimpleConsumer.class);

    public static void main(String[] args) {
        Context jndiContext = null;
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        Topic t = null;
        MessageConsumer consumer = null;
        String destinationName = null;
        final int NUM_MSGS;

        if ((args.length < 1) || (args.length > 2)) {
            log.info("Usage: java SimpleConsumer <destination-name>
[<number-of-messages>]");
            System.exit(1);
        }
        destinationName = args[0];
        log.info("Destination name is " + destinationName);
        if (args.length == 2) {
            NUM_MSGS = (new Integer(args[1])).intValue();
        }
        else {
            NUM_MSGS = 1;
        }

        try {
            jndiContext = new InitialContext();
        }
        catch (NamingException e) {
            log.info("Could not create JNDI API context: " + e.toString());
            System.exit(1);
        }

        try {
            connectionFactory = (ConnectionFactory)
jndiContext.lookup("ConnectionFactory");
            destination = (Destination) jndiContext.lookup(destinationName);
            t = (Topic) jndiContext.lookup(destinationName);
        }
        catch (NamingException e) {
            log.info("JNDI API lookup failed: " + e);
            System.exit(1);
        }

        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
            consumer = session.createConsumer(destination);
            TextMessage message = null;
            for (int i = 0; i < NUM_MSGS; i++) {
            	message = (TextMessage)consumer.receive(10000);
            	log.info("Receiving message: " + message.getText());
            }
        }
        catch (JMSException e) {
            log.info("Exception occurred: " + e);
        }
        finally {
            if (connection != null) {
                try {
                    connection.close();
                }
                catch (JMSException e) {
                }
            }
        }
    }
}
-- 
View this message in context: http://www.nabble.com/Creating-simple-producer-and-consumer-tf3683279s2354.html#a10303400
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message