You can consume this class from a main method or for a unit test.
using System;
using System.Messaging;
public class QClass
{
private string qPath;
MessageQueue mq;
public QClass(string queue_Path)
{
qPath = queue_Path;
}
MessageQueue queue = null;
private bool LoadQueue()
{
bool b=false;
if (!MessageQueue.Exists(qPath))
{
try
{
mq = MessageQueue.Create(qPath, true);
b = true;
}
catch (Exception ex)
{
b = true;
}
}
return b;
}
public bool SendMessageToQueue(string msg)
{
int msgCount = 0;
MessageQueueTransaction mtx = new MessageQueueTransaction(); // Use for transactional q's
string labelMessage = "Queue Loaded ";
Message mesg = new Message();
mesg.Body = "Hello world";
mesg.Label = "Label 1";
bool b = false;
b = LoadQueue();
if (b == false)
{
mq = new MessageQueue(qPath);
try
{
mq = new MessageQueue(qPath);
if (mq.CanWrite)
{
// mtx.Begin();
try
{
mq.Send(mesg, MessageQueueTransactionType.None);
// mtx.Commit();
b = true;
}
catch (Exception ec)
{
//mtx.Abort();
}
}
}
catch (Exception ex)
{
b = false;
}
}
return b;
}
public Message readReceive()
{
Message m1=mq.Receive();
return m1;
}
}
using System;
using System.Messaging;
public class QClass
{
private string qPath;
MessageQueue mq;
public QClass(string queue_Path)
{
qPath = queue_Path;
}
MessageQueue queue = null;
private bool LoadQueue()
{
bool b=false;
if (!MessageQueue.Exists(qPath))
{
try
{
mq = MessageQueue.Create(qPath, true);
b = true;
}
catch (Exception ex)
{
b = true;
}
}
return b;
}
public bool SendMessageToQueue(string msg)
{
int msgCount = 0;
MessageQueueTransaction mtx = new MessageQueueTransaction(); // Use for transactional q's
string labelMessage = "Queue Loaded ";
Message mesg = new Message();
mesg.Body = "Hello world";
mesg.Label = "Label 1";
bool b = false;
b = LoadQueue();
if (b == false)
{
mq = new MessageQueue(qPath);
try
{
mq = new MessageQueue(qPath);
if (mq.CanWrite)
{
// mtx.Begin();
try
{
mq.Send(mesg, MessageQueueTransactionType.None);
// mtx.Commit();
b = true;
}
catch (Exception ec)
{
//mtx.Abort();
}
}
}
catch (Exception ex)
{
b = false;
}
}
return b;
}
public Message readReceive()
{
Message m1=mq.Receive();
return m1;
}
}
No comments:
Post a Comment