You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by wha <wi...@cgi.com> on 2008/02/11 23:30:16 UTC

Start/Stop Consumer and duplicate message is received.

Hi,

I'm using the following version apache-activemq-5.1-20080208.142256-20.zip

I have like 500 messages pending in the broker and I have the following
consumer:

	public void run()
	{
		//Create a connection
		Connection conn = null;
		
		String user = null;
		String passw = null;
		String brokerurl = "tcp://localhost:61616";
		
		try
		{
			ActiveMQConnectionFactory connFactory = new
ActiveMQConnectionFactory(user,passw,brokerurl);
			conn = connFactory.createConnection();
			conn.start();
			
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			
			Queue dest = session.createQueue("Q1");
			
			MessageConsumer msgConsumer = session.createConsumer(dest);
			
			msgConsumer.setMessageListener(this);
			
			System.out.println("Listening Q1...");
			
		}
		catch (JMSException jmse)
		{
			System.out.println(jmse);
		}
	}
	
	public void onMessage(Message mess)
	{	
		try
		{
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			java.util.Date date = new java.util.Date();
			
			System.out.println(dateFormat.format(date) + " - Received msg: " +
((TextMessage)mess).getText());
			mess.acknowledge();
			
			Thread.sleep(5 * 1000);
			
		}
		catch(InterruptedException ie)
		{
			System.out.println(ie);
		}
		catch (JMSException jmse)
		{
			System.out.println(jmse);
		}
			
	}

When I stop the consumer and then restart it. The last message that was
received is being resent again by the broker. Is that normal behavior or is
there something wrong with my code??

Could it be my Thread.sleep that is executed too fast before an
aknowledgement can be sent ?

Any help appreciated!

Thanks!

-- 
View this message in context: http://www.nabble.com/Start-Stop-Consumer-and-duplicate-message-is-received.-tp15422289s2354p15422289.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Start/Stop Consumer and duplicate message is received.

Posted by wha <wi...@cgi.com>.
Thanks for the info. I got it working.
-- 
View this message in context: http://www.nabble.com/Re%3A-Start-Stop-Consumer-and-duplicate-message-is-received.-tp15423159s2354p15461022.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Start/Stop Consumer and duplicate message is received.

Posted by Mario Siegenthaler <ms...@inventsoft.ch>.
I'd say your problem is due to the session running in auto-ack mode
(so after the onMessage method completes) and your manual ack. I think
ActiveMQ might just ignore your call to acknowledge() and wait for the
method to complete. Since the 5 second sleep is inside the
onMessage-method it'll assume that the last message was not delivered
successfully and tries to redeliver it. Try if a change to manual-ack
helps.

Mario


On 2/11/08, wha <wi...@cgi.com> wrote:
>
> Hi,
>
> I'm using the following version apache-activemq-5.1-20080208.142256-20.zip
>
> I have like 500 messages pending in the broker and I have the following
> consumer:
>
>         public void run()
>         {
>                 //Create a connection
>                 Connection conn = null;
>
>                 String user = null;
>                 String passw = null;
>                 String brokerurl = "tcp://localhost:61616";
>
>                 try
>                 {
>                         ActiveMQConnectionFactory connFactory = new
> ActiveMQConnectionFactory(user,passw,brokerurl);
>                         conn = connFactory.createConnection();
>                         conn.start();
>
>                         Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
>
>                         Queue dest = session.createQueue("Q1");
>
>                         MessageConsumer msgConsumer = session.createConsumer(dest);
>
>                         msgConsumer.setMessageListener(this);
>
>                         System.out.println("Listening Q1...");
>
>                 }
>                 catch (JMSException jmse)
>                 {
>                         System.out.println(jmse);
>                 }
>         }
>
>         public void onMessage(Message mess)
>         {
>                 try
>                 {
>                         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
>                         java.util.Date date = new java.util.Date();
>
>                         System.out.println(dateFormat.format(date) + " - Received msg: " +
> ((TextMessage)mess).getText());
>                         mess.acknowledge();
>
>                         Thread.sleep(5 * 1000);
>
>                 }
>                 catch(InterruptedException ie)
>                 {
>                         System.out.println(ie);
>                 }
>                 catch (JMSException jmse)
>                 {
>                         System.out.println(jmse);
>                 }
>
>         }
>
> When I stop the consumer and then restart it. The last message that was
> received is being resent again by the broker. Is that normal behavior or is
> there something wrong with my code??
>
> Could it be my Thread.sleep that is executed too fast before an
> aknowledgement can be sent ?
>
> Any help appreciated!
>
> Thanks!
>
> --
> View this message in context: http://www.nabble.com/Start-Stop-Consumer-and-duplicate-message-is-received.-tp15422289s2354p15422289.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>
>

Re: Start/Stop Consumer and duplicate message is received.

Posted by Rob Davies <ra...@gmail.com>.
On Feb 11, 2008, at 10:30 PM, wha wrote:

>
> Hi,
>
> I'm using the following version apache- 
> activemq-5.1-20080208.142256-20.zip
>
> I have like 500 messages pending in the broker and I have the  
> following
> consumer:
>
> 	public void run()
> 	{
> 		//Create a connection
> 		Connection conn = null;
> 		
> 		String user = null;
> 		String passw = null;
> 		String brokerurl = "tcp://localhost:61616";
> 		
> 		try
> 		{
> 			ActiveMQConnectionFactory connFactory = new
> ActiveMQConnectionFactory(user,passw,brokerurl);
> 			conn = connFactory.createConnection();
> 			conn.start();
> 			
> 			Session session = conn.createSession(false,  
> Session.AUTO_ACKNOWLEDGE);
> 			
> 			Queue dest = session.createQueue("Q1");
> 			
> 			MessageConsumer msgConsumer = session.createConsumer(dest);
> 			
> 			msgConsumer.setMessageListener(this);
> 			
> 			System.out.println("Listening Q1...");
> 			
> 		}
> 		catch (JMSException jmse)
> 		{
> 			System.out.println(jmse);
> 		}
> 	}
> 	
> 	public void onMessage(Message mess)
> 	{	
> 		try
> 		{
> 			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd  
> HH:mm:ss");
> 			java.util.Date date = new java.util.Date();
> 			
> 			System.out.println(dateFormat.format(date) + " - Received msg: " +
> ((TextMessage)mess).getText());
> 			mess.acknowledge();
> 			
> 			Thread.sleep(5 * 1000);
> 			
> 		}
> 		catch(InterruptedException ie)
> 		{
> 			System.out.println(ie);
> 		}
> 		catch (JMSException jmse)
> 		{
> 			System.out.println(jmse);
> 		}
> 			
> 	}
>
> When I stop the consumer and then restart it. The last message that  
> was
> received is being resent again by the broker. Is that normal  
> behavior or is
> there something wrong with my code??
>
> Could it be my Thread.sleep that is executed too fast before an
> aknowledgement can be sent ?
>
> Any help appreciated!
>
> Thanks!
>
> -- 
> View this message in context: http://www.nabble.com/Start-Stop-Consumer-and-duplicate-message-is-received.-tp15422289s2354p15422289.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>

You have auto acknowledgement set on your session - so the message  
won't actually be acknowledged until you return from onMessage()
Try client acknowledgement  instead




cheers,

Rob

http://open.iona.com/ -Enterprise Open Integration
http://rajdavies.blogspot.com/