MyChannels.Nirvana API
MyChannels.Nirvana Queue Publisher
This example shows how to create a Queue Publisher using the MyChannels.Nirvana API.
Application Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using MyChannels.Nirvana;
namespace MyChannels.Nirvana.Samples
{
class EXQueuePush
{
private int messagesSent = 0;
private string rName = null;
private string qName = "All";
private int count = 10;
private int size = 100;
public EXQueuePush(string[] args)
{
// Check arguments: <rname> [qName] [count] [size]
// Need a min of 2: rname
if (args.Length < 1)
{
Console.WriteLine("Realm name required");
Environment.Exit(2);
}
rName = args[0];
if (args.Length > 1)
qName = args[1];
if (args.Length > 2)
count = Convert.ToInt32(args[2]);
if (args.Length > 3)
size = Convert.ToInt32(args[3]);
start();
}
private void start()
{
var session = new Session(rName);
// Initialize the session
session.Initialize();
Console.WriteLine("Initialized...");
session.AsynchronousExceptionRaised +=
new EventHandler<AsyncExceptionEventArgs>(session_AsynchronousExceptionRaised);
session.ConnectionStatusChanged +=
new EventHandler<ConnectionStatusEventArgs>(session_ConnectionStatusChanged);
var queues = session.Queues;
//Create a byte array filled with characters equal to
// the message size specified. This could be a result
//of String.getBytes() call in a real world scenario.
byte[] buffer = new byte[size];
for (int x = 0; x < size; x++)
{
buffer[x] = (byte)((x % 90) + 32);
}
//Construct a sample Properties object
//Instantiate the message to be published with the specified Properties and buffer
Properties props = new Properties();
Message msg = new Message(props, buffer);
//Inform the user that publishing is about to start
Console.WriteLine("Starting publish of " + count + " events with a size of " + size + " bytes each");
if (qName == "All")
{
//IProducer producer = queues.CreateProducer(qName);
//for (int x = 0; x < count; x++)
//{
// //Publish the event
// producer.Send(msg);
//}
Console.WriteLine("Publishing to all Queues is not supported at present.");
}
else
{
IProducer producer = queues.CreateProducer(qName);
for (messagesSent = 0; messagesSent < count; messagesSent++)
{
//Publish the event
producer.Send(msg);
}
}
session.Dispose();
}
void session_ConnectionStatusChanged(object sender, ConnectionStatusEventArgs e)
{
while (!e.IsConnected)
{
Console.WriteLine("Disconnected from Universal Messaging, Sleeping for 1 second...");
try
{
Thread.Sleep(1000);
}
catch (Exception) { }
}
// decrement the sent messages loop, so that the message sending while the disconnect
//occurred is sent again
--messagesSent;
}
void session_AsynchronousExceptionRaised(object sender, AsyncExceptionEventArgs e)
{
Console.WriteLine("Exception raised: {0}", e.Error.GetType());
Console.WriteLine("Exception message: {0}", e.Error.Message);
Environment.Exit(2);
}
public static void Main(string[] args)
{
new EXQueuePush(args);
}
}
}