activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chenq <chenq.m...@gmail.com>
Subject why queue consumer threads only get half messages ?
Date Tue, 21 Oct 2008 12:11:31 GMT

 I'm testing the activemq, and when I make my cosumers receive messages from
a queue in multi-threds , it only get half messages sent from producers.
would somebody can tell me why?


here is my source code:

public class Recv extends Thread {
    private static final String url = "tcp://localhost:61616";
    private static final String QUEUE_NAME = "TestQue";
    private static final String TOPIC_NAME = "TestTopic";
    public static int mode = 0;

    public void revceMessage() throws JMSException {
        Connection connection = null;
        TextMessage ms = null;
        StringBuffer str = null;
        int i = 0; 
        try {
            ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory(url);
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
            Destination destination = null;
            if(mode == 0)
            	destination = session.createQueue(QUEUE_NAME);
            else
            	destination = session.createTopic(TOPIC_NAME);
            MessageConsumer consu = session.createConsumer(destination,
null);
            while (true) {
                ms = (TextMessage) consu.receive();
                i++;
                if (ms == null) {
                    break;
                }
                str = new StringBuffer();
                str.append("thread:");
                str.append(this.getId());
                str.append(",receive:");
                str.append(i);
                System.out.println(str);
                str = null;
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
    
    public void run() {
    	try {
    		this.revceMessage();
    	} catch (JMSException e) {
    		e.printStackTrace();
    	}
    }

    public static void main(String[] args) {
    	Recv s = null;
    	if ((args.length >0) && (!args[0].trim().equals("0")))
        	mode = 1;
        System.out.println("mode is: " + mode + ", url:" + url);
        for(int i=0;i<2;i++) {
        	s = new Recv();
        	s.start();
        }
    }
}


-- 
View this message in context: http://www.nabble.com/why-queue-consumer-threads-only-get-half-messages---tp20088895p20088895.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message