You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by lrklx <lr...@163.com> on 2013/05/22 15:43:18 UTC

I do not receive the first message in my second consumer!

hi everyone!

i write a test program for consumer study, but i find i canot  receive the
first message in my second consumer!

i create two MessageProducers (producer1 and producer2) and two
MessageConsumers (consumer1 and consumer2) from two different Sessions. 

 i want run the code as producer1.send -> consumer1.receive ->
producer2.send -> consumer2.receive -> producer1.send -> .... ,but consumer2
can not receive at the first time, and second is ok.

here is my code:

public class TestConsumer
{
	public static void main(String[] args) throws JMSException
	{
		String url = "tcp://localhost:61616";
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

		Connection connection0 = connectionFactory.createConnection();
		connection0.start();
		final Session session0 = connection0.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		Destination destination0 = session0.createQueue("test-in");
		// listen queue "test-in", and reply to queue "test-out"
		MessageConsumer consumer0 = session0.createConsumer(destination0);
		consumer0.setMessageListener(new MessageListener()
		{
			public void onMessage(Message message)
			{
				try
				{
					// System.out.println("receive ok!");
					Destination destination00 = session0
							.createQueue("test-out");
					MessageProducer producer0 = session0
							.createProducer(destination00);
					producer0.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
					Message replyMessage = session0
							.createTextMessage("replyOK");
					producer0.send(replyMessage);
					// System.out.println("send ok!");
					producer0.close();
				}
				catch (Exception e)
				{
					// ingore
				}
			}
		});

		Connection connection = connectionFactory.createConnection();
		connection.start();

		Session session1 = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		Destination destination1 = session1.createQueue("test-in");
		MessageProducer producer1 = session1.createProducer(destination1);
		producer1.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		TextMessage message = session1.createTextMessage("hello");
		// # producer1 send 1
		producer1.send(message);

		Destination destination11 = session1.createQueue("test-out");
		MessageConsumer consumer1 = session1.createConsumer(destination11);
		// # consumer1 receive 1
		System.out.println(consumer1.receive(3000));

		Session session2 = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		Destination destination2 = session2.createQueue("test-in");
		MessageProducer producer2 = session2.createProducer(destination2);
		producer2.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		TextMessage message2 = session1.createTextMessage("hello");
		// # producer2 send 1
		producer2.send(message2);

		Destination destination22 = session2.createQueue("test-out");
		MessageConsumer consumer2 = session2.createConsumer(destination22);
		*// # consumer2 receive 1 >>>>>> here, can not receive message, 
		//although i can see "test-out" queue has one message via web console!
		System.out.println(consumer2.receive(3000));*

		// # producer1 send 2
		producer1.send(message);
		// # consumer1 receive 2
		System.out.println(consumer1.receive(3000));

		// # producer2 send 2
		producer2.send(message2);
		// # consumer2 receive 2
		System.out.println(consumer2.receive(3000));

		connection.close();
		connection0.close();
	}
}

i had already read the page "I do not receive messages in my second
consumer"
http://activemq.apache.org/i-do-not-receive-messages-in-my-second-consumer.html
and changed the connection url as:
String url = "tcp://localhost:61616?jms.prefetchPolicy.all=1";

but it does't work.


can anybody tell me why?



--
View this message in context: http://activemq.2283324.n4.nabble.com/I-do-not-receive-the-first-message-in-my-second-consumer-tp4667344.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: I do not receive the first message in my second consumer!

Posted by lrklx <lr...@163.com>.
thank you tabish.

i changed the code  
Destination destination11 = session1.createQueue("test-out"); 
as
Destination destination11 = new
ActiveMQQueue("test-out?consumer.prefetchSize=0");

and it works good.

thanks again.



--
View this message in context: http://activemq.2283324.n4.nabble.com/I-do-not-receive-the-first-message-in-my-second-consumer-tp4667344p4667434.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: I do not receive the first message in my second consumer!

Posted by Timothy Bish <ta...@gmail.com>.
On 05/22/2013 09:43 AM, lrklx wrote:
> hi everyone!
>
> i write a test program for consumer study, but i find i canot  receive the
> first message in my second consumer!
>
> i create two MessageProducers (producer1 and producer2) and two
> MessageConsumers (consumer1 and consumer2) from two different Sessions.
>
>   i want run the code as producer1.send -> consumer1.receive ->
> producer2.send -> consumer2.receive -> producer1.send -> .... ,but consumer2
> can not receive at the first time, and second is ok.
>
> here is my code:
>
> public class TestConsumer
> {
> 	public static void main(String[] args) throws JMSException
> 	{
> 		String url = "tcp://localhost:61616";
> 		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
>
> 		Connection connection0 = connectionFactory.createConnection();
> 		connection0.start();
> 		final Session session0 = connection0.createSession(false,
> 				Session.AUTO_ACKNOWLEDGE);
> 		Destination destination0 = session0.createQueue("test-in");
> 		// listen queue "test-in", and reply to queue "test-out"
> 		MessageConsumer consumer0 = session0.createConsumer(destination0);
> 		consumer0.setMessageListener(new MessageListener()
> 		{
> 			public void onMessage(Message message)
> 			{
> 				try
> 				{
> 					// System.out.println("receive ok!");
> 					Destination destination00 = session0
> 							.createQueue("test-out");
> 					MessageProducer producer0 = session0
> 							.createProducer(destination00);
> 					producer0.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
> 					Message replyMessage = session0
> 							.createTextMessage("replyOK");
> 					producer0.send(replyMessage);
> 					// System.out.println("send ok!");
> 					producer0.close();
> 				}
> 				catch (Exception e)
> 				{
> 					// ingore
> 				}
> 			}
> 		});
>
> 		Connection connection = connectionFactory.createConnection();
> 		connection.start();
>
> 		Session session1 = connection.createSession(false,
> 				Session.AUTO_ACKNOWLEDGE);
> 		Destination destination1 = session1.createQueue("test-in");
> 		MessageProducer producer1 = session1.createProducer(destination1);
> 		producer1.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
> 		TextMessage message = session1.createTextMessage("hello");
> 		// # producer1 send 1
> 		producer1.send(message);
>
> 		Destination destination11 = session1.createQueue("test-out");
> 		MessageConsumer consumer1 = session1.createConsumer(destination11);
> 		// # consumer1 receive 1
> 		System.out.println(consumer1.receive(3000));
>
> 		Session session2 = connection.createSession(false,
> 				Session.AUTO_ACKNOWLEDGE);
> 		Destination destination2 = session2.createQueue("test-in");
> 		MessageProducer producer2 = session2.createProducer(destination2);
> 		producer2.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
> 		TextMessage message2 = session1.createTextMessage("hello");
> 		// # producer2 send 1
> 		producer2.send(message2);
>
> 		Destination destination22 = session2.createQueue("test-out");
> 		MessageConsumer consumer2 = session2.createConsumer(destination22);
> 		*// # consumer2 receive 1 >>>>>> here, can not receive message,
> 		//although i can see "test-out" queue has one message via web console!
> 		System.out.println(consumer2.receive(3000));*
>
> 		// # producer1 send 2
> 		producer1.send(message);
> 		// # consumer1 receive 2
> 		System.out.println(consumer1.receive(3000));
>
> 		// # producer2 send 2
> 		producer2.send(message2);
> 		// # consumer2 receive 2
> 		System.out.println(consumer2.receive(3000));
>
> 		connection.close();
> 		connection0.close();
> 	}
> }
>
> i had already read the page "I do not receive messages in my second
> consumer"
> http://activemq.apache.org/i-do-not-receive-messages-in-my-second-consumer.html
> and changed the connection url as:
> String url = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
>
> but it does't work.
>
>
> can anybody tell me why?
>
>
>
> --
> View this message in context: http://activemq.2283324.n4.nabble.com/I-do-not-receive-the-first-message-in-my-second-consumer-tp4667344.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>
You create the second consumer after sending the second message however 
you already have a consumer (1) so that one is having the message sent 
to its prefetch buffer.  You either need to create the consumer (2) 
earlier or set a prefetch of zero so they are pull consumers.

-- 
Tim Bish
Sr Software Engineer | RedHat Inc.
tim.bish@redhat.com | www.fusesource.com | www.redhat.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/

www.camelone.org : The open source integration conference: