Blocking send operation
Is there a way to invoke the send method in a blocking way? I would like to wait for the ack/nack/timeout, and continue only after I have the result.
My current solution is a wrapper of the original send method, that keeps track of the sent messages and waits for the event from the event broker.
Best Answer
-
Hi @Adyc, have you seen the SendBlocking session property? It doesn't do what you want (it blocks only until the message is accepted by the transport), but in the description it does say:
The API does not support a synchronous (send and wait for acknowledgement operation), however such an operation can be easily implemented in an application by waiting for a Acknowledgement
Sorry!
1
Answers
-
Hi @Adyc, welcome to the Community!
Which language, API, protocol are you asking about? We have many. Some of our newer APIs already have this built-in. JMS 1.1 also has this built-in by spec. Coding this yourself for JCSMP is pretty simple, just need some threading locking/coordinating code. Other answers for other languages. Let us know.
1 -
Hi @Adyc, have you seen the SendBlocking session property? It doesn't do what you want (it blocks only until the message is accepted by the transport), but in the description it does say:
The API does not support a synchronous (send and wait for acknowledgement operation), however such an operation can be easily implemented in an application by waiting for a Acknowledgement
Sorry!
1 -
Yeah, I'm not a C# dev, but in Java the code to implement this behaviour is not that hard. Essentially make your own
send()
method that does the actual API send, and then block on an object / thread coordinator (e.g. a CountDownLatch of size 1). The latch (or whatever) should probably be keyed on the MessageID that the API assigns to the message after the send() call returns... or make sure the CorrelationKey is set to the message or something unique; this is in case you have multiple threads publishing in parallel. Then in the callback where publish ACKs are received from the broker, you can check the CorrelationKey assigned to the ACK, find the Latch (or whatever) that is for that specific message and unblock it. This will release your other thread to continue. Obviously, if you receive a NACK (negative acknowledgement instead) you'd want to throw an exception to the latch so that your publishing thread knows it has a problem.Note that in C# the API has only one thread (Context thread) for doing everything, so you CANNOT publish from this thread and block as it would block up other processes the API needs to run. So make sure you're publishing from an application-owned thread.
0