2.22. Querying Data Asynchronously with Message Queuing
Problem
You want to asynchronously retrieve data from a system that is not always connected.
Solution
You must:
Use message queuing to construct and send a data request from the client.
Access and process the requesting message at the server.
Compose and send a response message containing the result set to the client.
Retrieve the response at the client and deserialize it into a
DataSet.
The sample code contains three event handlers:
- Send
Button.Click Checks if the
querymessage queue exists and creates it if necessary. AMessageQueueobject is created to access the queue. A message is sent to the queue containing theCustomerIDwhich the user wants information about.- Process Query
Button.Click Checks if the
querymessage queue exists and creates it if necessary. AMessageQueueobject is created to access the queue. An attempt is made to receive a message from the queue, waiting one second before giving up. If a message is received, theCustomerIDis extracted from the message and the message queue is closed. ADataSetis created and aDataAdapteris used to return the record for the requestedCustomerIDinto a CustomerDataTablein theDataSet. A result queue is created if necessary and a message labeled with theCustomerIDand containing theDataSetwith the asynchronous query results is sent to the queue.- Process Result
Button.Click Checks if the
resultmessage queue exists and creates it if necessary. AMessageQueueobject is created ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access