Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:14:50+00:00 2026-06-07T05:14:50+00:00

There might be a stupid simple answer to this, but I’m trying to use

  • 0

There might be a stupid simple answer to this, but I’m trying to use ActiveMQ to pass messages between producers and consumers. I will have many producers and many consumers, but I want each message to be delivered only once among the consumers. This would seem to mean that I cannot use Topics, since they would deliver messages to all consumers who are listening, and I want only one Consumer to receive each message.

My problem is that I am able to receive messages, but the messages are not dequeued. So if I restart my consumer process, all of the messages are reprocessed. This answer seems pertinent but does not seem to apply since I can’t create durable queue consumers, only durable topic consumers (unless I’m missing something in the API docs).

My code is as follows.

TopicConnectionFactory factory = new ActiveMQConnectionFactory(props.getProperty("mq.url"));
Connection conn  = factory.createConnection();
Session session = conn.createSession(true, Session.CLIENT_ACKNOWLEDGE);
Queue queue = session.createQueue(props.getProperty("mq.source_queue"));
conn.start();
MessageConsumer consumer = session.createConsumer(queue);

Then later on

Message msg = consumer.receive();
msg.acknowledge();
if (!(msg instanceof TextMessage)) continue;
String msgStr = ((TextMessage)msg).getText();

This is my current code. I have tried with Session.AUTO_ACKNOWLEDGE and without msg.acknowledge(). Any working permutation of this code seems to retrieve the messages, but when I restart my consumer, all of the messages get received again, even if they have been received prior to the restart.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:14:52+00:00Added an answer on June 7, 2026 at 5:14 am

    You created the session as a transacted Session and therefore need to call, session.commit if you want to inform the broker that all messages are now consumed and don’t need to be redelivered. If you don’t set the first argument to createSession to true then the Ack mode is respected otherwise its ignored, one of the oddities of the JMS API I’m afraid. If you do this:

    ConnectionFactory factory = new ActiveMQConnectionFactory(props.getProperty("mq.url"));
    Connection conn  = factory.createConnection();
    Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = session.createQueue(props.getProperty("mq.source_queue"));
    conn.start();
    MessageConsumer consumer = session.createConsumer(queue);
    

    Then this would work:

    Message msg = consumer.receive();
    msg.acknowledge();
    

    Otherwise you need to do:

    Message msg = consumer.receive();
    session.commit(); 
    

    But keep in mind that for a single message transactions don’t really make sense to client ack with no transaction is a better option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might turn out to be a stupid typo but just incase there is
There might be a really simple solution to this question, but i just don't
hey guys, this might be really stupid, but hopefully someone can help. I'm trying
This might be a stupid question, but is there a rule that states that
This might be a stupid question but i would really appreciate any compact answer.
This might be a stupid question but if there's a better or proper way
This might be a stupid question, but is there any common practice for initializing
This might be a stupid question but is it possible to use javascript/jQuery in
This might seem as silly question but I am thinking there might be the
I'm more or less Java programmer, so this might be a stupid question, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.