I'm making a project with 5 packages. The packages can communicate with each other by sending Messages to a central MessageQueue in the Main package and that MessageQueue takes care of all the messages. (cfr.: http://en.wikipedia.org/wiki/Java_Message_Service)
Provided this image:
the packages would be the clients (both publisher and subscriber) and the MessageQueue would be the topic.
I'm now implementing the MessageQueue (a.k.a. the Topic in the image above) and packages can already subscribe to Messages and packages can also publish a Message.
Publishing a Message is just adding the Message to a queue 'inside' the MessageQueue.
So at a given time during execution there can be 0, 1 or more messages in the MessageQueue and now i must make sure the MessageQueue processes all these messages correctly (and in order).
A way of doing this could look like this:
public class MessageQueue {
public MessageQueue(){
//initialising etc.
processMessages();
}
private void processMessages(){
while(/*program is running*/){
if(queue.isNotEmpty()){
/*process first message in queue*/
}
}
}
However, i believe this is not a great design and is sometimes referred to as busy waiting?
How could i approach this in a better way (without using any standard APIs ofcourse)?