C# LogoWhile sitting at the airport this morning waiting for my flight, I noticed the guy sitting beside me kept peeking at my laptop screen. I wasn’t working on anything important. So, I didn’t mind it too much. After a few minutes though, it began getting a little uncomfortable. At that point, the guys said he was a beginning programmer and was curious as to what I was working on. We got into a lengthy conversation about programming and technology in general. He went into a fairly lengthy story about how he is just now learning how to program and his employer has asked that he write an application that can talk to a queue. Unfortunately, the guy had never worked with queues before and didn’t fully understand what they were and how they are used. I did my best to explain to him how queues operate, but he still wasn’t getting it. To help him better understand, I decided to throw together a quick queue server that would hopefully help him learn enough about queues that he could fulfill the task at hand.

I began coding the example while we were at the airport in Atlanta, Georgia, finished the example on the airplane, and shared it with him once we arrived in Austin, Texas. It is a very simple queue server that uses a simple HTTP server to listen for incoming messages and puts the messages on a queue. If the queue doesn’t exist, it gets built on-the-fly and the queue gets added to a dictionary where the queue name is the dictionary key. That way, we could support multiple queues with the same server with no extra coding. I chose to use an HTTP server to listen for incoming messages because I could quickly put together an HTML page that could be used to submit messages to the queue using the POST method and could retrieve messages from a queue using the GET method. Both methods require that “QUEUE_NAME” is passed either via a form or in the URL and the POST method also requires a “MESSAGE”.

After showing the fellow programmer my super simple queue server in C#, I feel like he finally grasped the concept (even though I thought it was pretty easy to grasp the first time I heard it, but that’s for another story). He asked if I would share the code with him to study and play with at a later time. So, I agreed and gave him the code on a thumbdrive. Since he found the code useful, I decided I would share it with anyone else that might be interested. I admit that the code is not clean and is nothing more than a hack. But, it does work and does a good job of getting the point across. Here is a screenshot of the application in action.

Queue Server in C#

When queues are created and messages are added to the queues, those queues will appear in the left-side list. The number of messages on each queue is also shown in the list. Click on any queue name to see a list of messages on the right. Click on any message to display the full message in the field at the bottom-right. When you launch the app, the HTTP server will begin listening for connections on port 8080. So, to test all of this, I have included an HTML file called “queue_server_test.html” which can be found in the download below. Launch that page in any browser, enter a queue name & message, and click the “Submit” button. This will post your queue name and message to “http://localhost:8080”. If successful, a message will be displayed on the screen. This is what the submit form looks like:

Queue Server Form

To retrieve the messages, point your browser to “http://localhost:8080/?QUEUE_NAME=[QUEUE NAME HERE]”. Just make sure you enter the same queue name you used in the form you submitted. Every time you refresh this page, the next message in the queue will be returned until there are no more messages in the queue. Also, as you refresh the screen to retrieve messages, you can return to the queue server window to see the message count drop next to the queues. So, make sure you submit several messages to test this thoroughly.

If you are interested in this super simple queue server in C#, you can download the source code (Visual Studio 2010 solution) from http://www.prodigyproductionsllc.com/downloads/QueueServer.zip.

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Tagged with:  

Leave a Reply