0

I am new to Activemq and Java,I read tutorials,somewhat I understand.can anyone help me to solve the following task.

Imagine we have a 10 messages in Queue/Topic of Activemq. we are getting messages from Database,we already did it.

I want to write 2 Java Applications (using JMS for receiving messages from activemq ) that will act as a consumer in Activemq. What i want to achieve out of this is that whenever Activemq get messages from Database, activemq should check if any consumer is free or not.If any consumer is free activemq should push message to consumer,once consumer receives it will do its own processing.

So how should my java code be inorder to achieve this?

Also how to set the prefetch limit?

can anyone help me.

Thanks.

Hanumath
  • 1,117
  • 9
  • 23
  • 41

1 Answers1

3

Ok good to know that there is Producer sitting between AMQ brokers and Database that is constantly pushing messages to AMQ.

To answer your question. There can be multiple consumers listening on any destination (queue/topic). ActiveMQ broker will push the message to any consumer (distribution of which can be customized). As I answered in my previous question that AMQ will use prefetch limit (let's say N) to send that many messages before starting to wait to get ack. If a consumer is busy then there can be only N messages waiting in its local buffer and AMQ broker will automatically choose the other consumer to push the messages.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • You are saying,Activemq able to push messages to any consumer,for that I need to write consumer code. For this I searched in Google,I found 3 or 4 examples all are explaining consumer fetching messages from activemq. But I want push messages from Activemq to consumers. If you know can you help me to write the consumer code. – Hanumath Sep 05 '13 at 11:41
  • You don't need to write special code for message distribution. Just write consumer code as standard consumer. For sample code see: https://github.com/egalli64/bitingcode/blob/master/amq/Hello.java – anubhava Sep 05 '13 at 11:47
  • ok @anubhava. Assume in my Activemq we have 2 queues names are A,B. A have 2 consumers names are 1,2. similarly B have 2 consumers names are 3,4. Know my doubt is, If activemq get message from DB into A.How Activemq knows whether A have 1,2 or not. For that am I need to configure something Activemq configuration files. – Hanumath Sep 05 '13 at 11:58
  • 1
    ActiveMQ very much knows that there are 2 live consumers for queueA consumer1 and consumer2. When consumer application starts up it establishes a durable connection with AMQ broker and tells AMQ on which queue it is listening to. That is only way AMQ broker can deliver messages from that queue to all active consumers. – anubhava Sep 05 '13 at 12:06
  • 1
    Here my consumer will be a java application. While creating the consumer function(in the github code posted above) how will i link my java application to the consumer? will it be via name/path/url? – Hanumath Sep 05 '13 at 12:18
  • is there anyway to contact you (over mails? or chat?) it would be of great help if could offer suggestion that will help setting up acitvemq. – Hanumath Sep 05 '13 at 13:12
  • Yes sure you can contact me on my email `anubhava[AT]aol[dot]com` – anubhava Sep 05 '13 at 13:51