My application sends messages to a server, which performs the processing of them. Eventually, I get a java.net.SocketTimeoutException: Read timed out, when the server receives the message but the application down. When the application returns, it does not know that the message was received and sends it back, generating a duplicate message on the server. Is there an algorithm to work around this? I would like an example in java that would handle this case
Asked
Active
Viewed 174 times
0
-
Possible duplicate of [What is an idempotent operation?](https://stackoverflow.com/questions/1077412/what-is-an-idempotent-operation) – Matthew Kerian Jun 24 '19 at 20:43
-
@MatthewKerian The question explains what idempotency is but does not show an example algorithm that I can use in my case. – ewerton Jun 24 '19 at 20:54
-
1There are various methods you can use to stop it, now that you know what it's called it should be much easier to find more information related to it. – Matthew Kerian Jun 24 '19 at 20:56
-
Yes, but I think that does not characterize my question as duplicate. – ewerton Jun 24 '19 at 20:59
-
Eh, your question is asking more for how to avoid it sure. But unveiling the real issue people with this question are having is the purpose of the duplicate question. It's essentially the same thing just with different words. In addition to that, there's a lot of different aspects of Idempotence, for example java's streams close function is required to be idempotent. So there's no true one size fits all solution – Matthew Kerian Jun 24 '19 at 21:04
-
1But something that can help you if you just want a simple example, add a header containing a random integer. On your server side you can store X amount of the previous message's random int. If there are any duplicates then it's a duplicate request. – Matthew Kerian Jun 24 '19 at 21:06
-
1You'll need to implement some kind of acknowledgement protocol at the message level, something similar to what TCP does at the transport level. See [retransmission](https://en.wikipedia.org/wiki/Retransmission_(data_networks)) for some discussion and perhaps some ideas. – President James K. Polk Jun 24 '19 at 22:59
-
JMS transactions would solve my problem? – ewerton Jun 25 '19 at 17:50