EDIT: This questions is not about building a message bus. That mechanism is already handled using a framework and communication between 2 sub systems is working properly. This question is more about, what to do when we receive a message and how to handle challenges presented down below, elegantly.
I have an interesting problem and tried to solve it myself but i think that my approach can be improved. Here is my scenario
I have an XML file with a list of messages. Some of them are messages of type "receive" and others of "transmit". As the names suggest, receive means that I will receive a message from some other system and transmit means that my system has to send a reply. Further in case of receive, I have to match the incoming message to the message in my XML file. For example
<Messages>
<Message name="1" type="transmit" ... />
<Message name="2" type="transmit" ... />
<Message name="3" type="receive" ... />
<Message name="4" type="receive" ... />
<Message name="5" type="transmit" ... />
...
</Messages>
The flow of code will be as follows
-
My code reads the XML file, sees that the first message is a transmit and sends it to other system. At the end, it checks the next message and if the next message is a trasmit, code starts processing it but if it is a receive type message then code goes to waiting state.
-
For a receive message, an event will be fired and the code will start processing the incoming message (verifying and matching the incoming message etc). At the end, we will check if the next message is a transmit or receive. In case of receive, it goes again to hibernation and in case of transmit it starts processing it.
Now, the format of XML file can also change. For example
<Messages>
<State name="S1">
<Message name="1" type="transmit" ... />
<Message name="2" type="transmit" ... />
</State>
<State name="S2">
<Message name="3" type="receive" ... />
<Message name="4" type="receive" ... />
<Message name="5" type="transmit" ... />
</State>
</Messages>
Even though the messages are the same but the way XML file is represented can change.
There is also another valid scenerio where sometimes an incoming message (receive) can come out of order. It should be handled properly. For example
<Messages>
<Message name="1" type="transmit" ... />
<Message name="3" type="receive" ... /> <--- Out of order
<Message name="2" type="transmit" ... />
<Message name="4" type="receive" ... />
<Message name="5" type="transmit" ... />
...
</Messages>
Here are my requirements:
-
The code which needs to read the messages should not be concerned how the XML file is read. It should only be concerned with the message. This means that there should be an abstraction on different XML file structures by providing an interface.
--> To achieve this, I have implemented Iterator pattern. Using this, I expose a few methods like GetNextMessage(), IncrementMessagePointer() etc
-
Handle out of order messages.
--> To handle this, I am currently using a pointer (say "ptr") which keeps track of out of order receive message. So, if a message is incoming out of order, I move the pointer to the next receive message in the original message sequence. Then while processing the messages, I check the ptr. If pointer is there then I just skip the waiting and ignore the receive message in my XML file and move ahead to the next transmit.
My approach to handle out of order messages does not seem to very elegant and will be prone to error in case some new scenario comes. So, how can we handle the above scenerio elegantly ?
Aucun commentaire:
Enregistrer un commentaire