[rabbitmq-discuss] Creating a job only once (using Java Client API)
kamauallan at gmail.com
Sat Apr 16 13:03:20 BST 2011
I have potentially many clients what may attempt create a given type
of job at the same time and more than once. And I would like the given
job (maybe identified by a job_name) for the given queue to be created
only once. And for the given job to be or have been executed
successfully only once in the future, present or in the past.
This is the long format of the situation.
I have several clients for whom a particular initialization step needs
to happen only once. Each such client represents an instance of an
application I am executing in parallel, for a particular node I would
like some data initialization procedure to be run only once triggered
by a single instance of these processes even though there will be many
such instances (processes) started at the same time. After the task
has been created a note that such a task as already been created
(already run or not) should be evident for all instances of this
application present and even for the instances to be created in the
future accessing a given queue_name.
Below is what I have tried without much success.
public void runHelper()throws IOException,java.lang.InterruptedException
ConnectionFactory factory=new ConnectionFactory();
QueueingConsumer consumer=new QueueingConsumer(channel);
//this means no message has been send received for the given
channel and queue.
//so let's send one.
message="dothis, from \""+_id__producer+"\", id:"+i;
//consume the message
String message2=new String(delivery.getBody());
_id__producer:\""+_id__producer+"\", got the job to be executed only
//do not acknowledge the receiption of the message.
System.out.println(new java.util.Date()+" -
[x]Received:'"+message2+"'. "+new java.util.Date());
System.out.println("[x] Thread:\""+_id__producer+"\" completed
sending:"+number_of_messages+", messages in:"+duration+"ms.");
More information about the rabbitmq-discuss