Date Created: Fri 10-Jun-2011

Get my WebSphere MQ course here >> http://www.themiddlewareshop.com/products/


//Note: Add project reference to amqmdnet.dll (Comes with Local install of WMQ for Windows)
//the code is not complete, but will show how to connect which is often the first problem you face when first //creating a client to connect to a remote queue manager.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using IBM.WMQ;

using System.Collections;



namespace MQSeriesTest
{

class Program
{

static void Main(string[] args)
{

MQ mq = new MQ();

Console.Read();

}

}



public class MQ
{

private MQQueue queue;

private MQMessage queueMessage;

private MQQueueManager queueManager;



private string queueManagerName;

private string host;

private int port;

private string channel;

private string username;

private string password;

private string queueName;

private Hashtable queueProperties;



public MQ()
{

setupTestData();

queueManager = createQueueManager();

}



public void setupTestData()
{

// Setup connection information

queueManagerName = "MYTESTMQGR";

host = "mymqserverhostname";

port = 1421;

channel = "CLIENT.MYTESTMQGR";

username = "dev";

password = "dev";

queueName = "TEST.LQUEUE";



queueProperties = new Hashtable();

queueProperties[MQC.HOST_NAME_PROPERTY] = host;

queueProperties[MQC.PORT_PROPERTY] = port;

queueProperties[MQC.CHANNEL_PROPERTY] = channel;

queueProperties[MQC.USER_ID_PROPERTY] = username;

queueProperties[MQC.PASSWORD_PROPERTY] = password;

}



public MQQueueManager createQueueManager()
{

MQQueueManager queueManager = null;



try
{

// Attempt the connection

queueManager = new MQQueueManager(queueManagerName, queueProperties);


Console.WriteLine("Connected Successfully");

}

catch (MQException mqexp)
{

Console.WriteLine("MQSeries Exception: " + mqexp.Message);

Console.WriteLine("MQSeries Exception: " + mqexp.Reason);

Console.WriteLine("MQSeries Exception: " + mqexp.StackTrace);

//see list of Reason Codes at http://russsutton.com/developer/?p=45

}



return queueManager;

}



public void PutMessageOnQueue(string message)
{

try
{

queue = queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);

queueMessage = new MQMessage();

queueMessage.WriteString(message);

queueMessage.Format = MQC.MQFMT_STRING;



queue.Put(queueMessage);

}

catch (MQException mqexp)
{

Console.WriteLine("MQSeries Exception: " + mqexp.Message);

Console.WriteLine("MQSeries Exception: " + mqexp.Reason);

Console.WriteLine("MQSeries Exception: " + mqexp.StackTrace);

}

}



public string GetMessageOffQueue()
{

string message = "";



queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

queueMessage = new MQMessage();

queueMessage.Format = MQC.MQFMT_STRING;



try
{

queue.Get(queueMessage);

message = queueMessage.ReadString(queueMessage.MessageLength);

}

catch (MQException MQExp)
{

Console.WriteLine("MQQueue::Get ended with " + MQExp.Message);

}



return message;

}

}

}

===================

MQ Commands to create QMGR and TEST Queue


crtmqm -q MYTESTMQGR
strmqm MYTESTMQGR

runmqsc commands

* DEFINE QMGRDEVTEST Queue Manager configuration
********************************************************************
* Create Listener
********************************************************************
DEFINE LISTENER ('MYTESTMQGR.LISTENER') +
TRPTYPE (TCP) +
PORT (1421) +
*QMGR sets the channel to start with the Queue Manager
CONTROL(QMGR) +
REPLACE

********************************************************************
* Start Listener
********************************************************************
start listener ('MYTESTMQGR.LISTENER')

********************************************************************
* Create Queues
********************************************************************
*Define the local DEV Queue

DEFINE QLOCAL('TEST.LQUEUE') +
DESCR('Development Test Queue') +
* Persistent messages OK
DEFPSIST(YES) +
* Shareable
SHARE DEFSOPT(SHARED) +
* Maximum queue depth
MAXDEPTH(100000) +
REPLACE


********************************************************************
* Create Client/Server Channel for clients and server's to connect
********************************************************************
*server channel
*Allow MQ Explorer administration
DEFINE CHANNEL(SYSTEM.ADMIN.SVRCONN) +
CHLTYPE(SVRCONN) +
REPLACE


*DEV Channels for c# client
*Client - Channel
DEFINE CHANNEL('CLIENT.MYTESTMQGRE') +
CHLTYPE(CLNTCONN) +
CONNAME('mymqserverhostname(1421)') +
DESCR('WebSphere MQ client connection to server hosting queue manager') +
TRPTYPE(TCP) +
QMNAME('MYTESTMQGR') +
USERID('dev') +
REPLACE

*Server - Channel
DEFINE CHANNEL('CLIENT.TO.TEST.QUEUE') +
CHLTYPE(SVRCONN) +
TRPTYPE(TCP) +
DESCR('Server connection for a WebSphere MQ client application') +
MCAUSER('dev') +
REPLACE

Get my WebSphere MQ course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many leading brands around the world. Steve specialises in JEE, DevOps and Thought Leadership.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


  • Linked In
  • Twitter
  • About Me
  • Contact Me

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

Courses

  • WebSphere Courses

Articles

WebSphere Categories

Other Categories