using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using IBM.XMS;
namespace XMSTest
{
class Program
{
static void Main(string[] args)
{
XMSFactoryFactory factoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
Console.WriteLine("Before create connectionfactory");
// Use the connection factories factory to create a connection factory
IBM.XMS.IConnectionFactory connfactory = factoryFactory.CreateConnectionFactory();
Console.WriteLine("created connectionfactory");
// Set the properties
connfactory.SetStringProperty(XMSC.WMQ_HOST_NAME, "MyHostNameOrIPAddressHere");
connfactory.SetIntProperty(XMSC.WMQ_PORT, 1414);
connfactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);//managed
connfactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "MainQM");
connfactory.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V1);
Console.WriteLine("Before create connection");
using (IBM.XMS.IConnection conn = connfactory.CreateConnection())
{
Console.WriteLine("created connection");
using (IBM.XMS.ISession session = conn.CreateSession(false, AcknowledgeMode.AutoAcknowledge))
{
Console.WriteLine("Before create session with topic");
using (IBM.XMS.IDestination dest = session.CreateTopic("topic://MyOwn.TopicHere")) // Create a topic
{
Console.WriteLine("created session with topic");
dest.SetIntProperty(XMSC.DELIVERY_MODE, XMSC.DELIVERY_NOT_PERSISTENT);
Console.WriteLine("before create consumer");
using (IBM.XMS.IMessageConsumer consumer = session.CreateConsumer(dest))
{
Console.WriteLine("created consumer");
// Start the connection
conn.Start();
IMessage recvMsg = null;
int messagesReceived = 0;
// Receive specified number of messages
while (
messagesReceived < Convert.ToInt32(10))
{
recvMsg = null;
// Receive the message
try
{
recvMsg = consumer.Receive();
}
catch (System.Xml.XmlException xmlException)
{
// we had cases where XMS.NET throws this
// exception because the received message
// had a tag-name put in an encoding that
// resulted in invalid XML
Console.WriteLine(xmlException.ToString());
}
catch (Exception otherException)
{
Console.WriteLine(otherException.ToString());
}
// Increment the counter
messagesReceived++;
if (recvMsg != null)
{
// Display that message was received.
Console.WriteLine("Received message "+messagesReceived.ToString());
}
else
{
Console.WriteLine("Message received was null or an error occurred.");
}
} // end of while
}
}
}
}
Console.WriteLine("End. Press Enter");
Console.ReadLine();
}
}
}