0
votes

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 message. 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 :)

3
What are you really trying to achieve here? How is the application intended to be used?K Erlandsson
Basic overview is a system controlling storage and retrieval requests of files. Each storage or retrieval request will spawn its own thread which will terminate once the request has been completed. A library I'm using necessitates the use of the 'communication' thread. Though this could all be handled within this one "main" thread ideally this can all happen in parallel. I might reconsider to have just 3 threads: communication, storage and retrieval.jcbown

3 Answers

0
votes

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.

0
votes

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?

0
votes

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.