Quickstart: Accessing local IBM WebSphere MQ with .NET C# (Part 2)

In the last post about IBM WebSphere MQ, I described how to install the current developer edition (v9.05) on your local Windows 10 Environment. In this blog post I will show, how to code a simple .NET C# Client to send and receive messages over the created message queue. This tutorial is based on the given code samples from IBM you get after installing the developer edition of the IBM Websphere MQ Server. In my installation they were located at “E:\Programme\ibmmq\Tools\dotnet\samples\cs\base”.

Go to part 1 of this series

Creating the new project

I used Visual Studio 2017, Community Edition, to code this sample.

You can download the full sample solution at: https://github.com/Kingviech/mq-cisharp-sample

To start, create a new C# Console Project start with file new project and choose the “Console-App (.NET Framework)” project template and name it “MqTest”.

After hitting the OK-Button your project is created and the Program.cs file is opened for you.

Adding references to the MQ-Libraries

Next up we will add the necessary references for the example application. To start this, right click on the references in the solution explorer of your created example project and click on the “Add reference…” menu entry.

Next click on the section “Explore” to search in the file explorer for the DLL needed for implementing the MQ-Client.

The DLL needed for this project is located inside the installation folder of your local IBM Websphere MQ installation, in my case it was located at the following file path: E:\Programme\ibmmq\Tools\Lib\amqmdnet.dll

After successfully adding the reference, it appears in the list of project references in the solution explorer.

Connecting to the Queue Manager

In the last part of this tutorial we created the queue manager QMA which we will connect to in this part of the tutorial.

Before starting out, add the following usings at the top of your Program.cs file:

After that we create an instance of Type MQQueueManager with the queue manager name as a constructor parameter:

Connecting to the queue

After successfully connecting to the queue manager we are ready to connect to the simple local queue we created in the last part of the tutorial which was named “QMA1”. We can create a connection to this queue in a similar way as we created the queue manager. We create an instance of type MQQueue and pass the queue name as constructor parameter. Additionally, we add flags to the second constructor parameter to tell the MQQueue instance, that allow to read the output of the queue and allows us to create messages in the queue. Additionally, the flat “MQOO_INQUIRE” was added, to be able to read attributes from the queue object itself.

Sending a simple “Hello, World!” to your queue

For sending a simple text message to the queue, we must prepare an instance of the MQMessage class and put it on the queue. The following code snippet shows how to do this.

 

After executing our code, we can look at the messages of our queue and we can see the sent message from our client (you can look at the messages of the queue in your IBM MQ Explorer installation, as described in the last part of this tutorial)!

Receiving messages from the queue

For receiving a message, the GET method of the queue object can be used. This method requires a target message object to write any received message to and message options for defining parameters like wait timeout. In the following code a simple get method is executed (you may want to execute this code in a loop, to keep receiving new messages from your queue).

When we run this program now, we receive the message sent previously by our program.

Cleaning up

After all your receiving is done, you may want to close the connections to the queue and disconnect from the queue manager. This is done by calling the corresponding methods of the MQQueue and MQQueueManager instances.

Conclusion

In this series I have shown how to connect to a local developer edition of IBM Websphere MQ. The last part, connecting to a remote MQ-Server, will be available after I have implemented the customer request in the next 2 months, stay tuned!

I hope this was helpful to anyone, any feedback is greatly appreciated!

 

Quickstart: Accessing local IBM WebSphere MQ with .NET C# (Part 1)

I was recently tasked to implement an integration between two systems of a customer. The task was to receive a XML-payload via a IBM WebSphere MQ Server hosted in their infrastructure, so my job was to implement a simple proof of concept.

We decided to give it a go with a .NET client, as almost all of our team has expertise in the C# programming language, so I started examining the .NET ressources for this API.

Go to part 2 of this series

Installing the MQ-Server locally

After reading through the documentation, I decided to install the developer edition of the MQ-Server on my local machine (win10) to give it a try. You can download the developer edition here (Current version as of this blog post was 9.05).

Unzip the installation files into a directory of your choice and start the Setup.exe.

After starting the installation wizard, you will be prompted to check if the system requirements are met by your computer.

In the tab “Network Configuration” choose the second option “No” if you don’t wish to run the MQ-Server with an AD-User.

In the last step you can choose the language of your local installation and then start the installation on your system.

For my first installation I chose the installation type “typical”, but you can change the installation location by choosing the installation type “custom”. You can also change the data folder and installation name if you choose the custom type. You can leave all other options at their default values for the time being.

After the server finished the installation, a second wizard opens for the configuration of the service instance. You can leave the default values if you install the server on a machine that is not in a network with a domain controller. After the configuration finished, you can start the IBM MQ Explorer.

Create a new Queue Manager

After starting the IBM MQ Explorer you will be greeted with the welcome page of the explorer. To understand the components of the IBM MQ Infrastructure, I recommend to read the redpaper “Websphere MQ Primer: An Introduction to Messaging and WebSphere MQ”.

To give a brief explanation: The MQ-Server consists of several queue managers which may contain several queues that can be used for asynchronous communication between applications.

We start off by creating a new Queue Manager in the MQ Explorer. To start this off, you can right click on the left navigation node named “Queue managers”:

Choose a name for your new queue manager and hit the finish button to create the queue manager.

After a short waiting time your new queue manager will be visible in the left navigation panel of your MQ-Explorer.

Create a new Queue

After creating the queue manager you are set for message delivery. Your last step before sending your first message is to create a queue for your queue manager.

To create a new queue, expand the navigation node of your queue manager, right click on “Queues”, expand “New” and choose “Locale queue”.

Enter a name for your new queue in the opened dialog and hit the finish button to create your new queue.

If the creation was successful, your new queue will be shown in the list of queues of your queue manager.

Sending test messages to your queue

To send a test message to your queue, right click on the queue in the list view of queues of your queue manager and click on “Queue test message…”.

You can now enter any message you like to send to your queue. I went with the famous “Hello, World!” string.

After hitting the send button, you can send more messages of close the dialog if you are done with sending messages to the queue. To view the messages in the queue you can right click on the choose the option “Show messages” in the context menu.

In the opened dialog you can see all messages, that are currently in your queue and waiting for processing. This view may come in handy in the next part of this series, when developing the C# client.

Conclusion

In this blog post I have shown how to install the developer edition ofIBM WebSphere MQ Server locally. In the next part of this series I will show you how to write a simple C# client that can send and receive messages from your created queue.