开发者

Looking for some Java good practice advice regarding distributed applications and message passing

开发者 https://www.devze.com 2023-02-01 00:51 出处:网络
Some background information. This is a distributed application with multiples nodes. A \'communication\' thread sends and receives all messages sent between these nodes. This cannot be changed.

Some background information. This is a distributed application with multiples nodes. A 'communication' thread sends and receives all messages sent between these nodes. This cannot be changed.

A 'doStuff' thread asks the 'communication' thread to send a message to a node. It then needs to wait for a response from the other node. The 'communication' thread will receive this response messag开发者_StackOverflow中文版e. It then needs to deliver this message to the correct 'doStuff' thread.

I am unsure what sort of information needs to be stored at the node or within the message to ensure that the correct thread always receives the response message.

Looking for some advice upon how to achieve this. Thanks for reading :)


You probably want to attach some sort of message id to the outgoing message to be included in the response. A sequential number or a UUID would probably do the job. Your communicator thread can then keep track (a map?) of what "doStuff" thread was waiting for a given response and pass it back.

You could also keep track of when the request was sent so that if a response isn't received the communicator thread can notify the doStuff thread that the response wasn't received.


It doesn't seem very difficult

Use either a thread id, or other opaque token invented by the doStuff thread which is stored by the Communication thread (mapped to the calling thread id) and returned with the response The communication thread looks it up in the Map to identify the corresponding thread.

Or am I missing something?


Is using JMS an option at all? If so, you might find that more practical than doing "roll your own." Asynchronous message passing, in general, is a solved problem. If JMS is an option, look at TopicRequestor and/or QueueRequestor in particular.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号