Nirvana C# .NET: Creating a Channel

This example demonstrates how to create a Nirvana channel programmatically.

Usage

nmakechan <rname> <channel name> [time to live] [capacity] [type] [cluster wide] [start eid] 

<Required Arguments> 

<rname> - the rname of the server to connect to
<channel name> - Channel name parameter for the channel to be created

[Optional Arguments] 

[time to live] - The Time To Live parameter for the new channel (default: 0)
[capacity] - The Capacity parameter for the new channel (default: 0)
[type] - The type parameter for the new channel (default: S)
R - For a reliable (stored in memory) channel with persistent eids
P - For a persistent (stored on disk) channel
S - For a simple (stored in memory) channel with non-persistent eids
T - For a transient (no server based storage)
M - For a Mixed (allows both memory and persistent events) channel

[cluster wide] - Whether the channel is cluster wide. Will only work if the realm is part of a cluster
[start eid] - The initial start event id for the new channel (default: 0)

Application Source Code

See also:

using System;
using com.pcbsys.nirvana.client;
namespace com.pcbsys.nirvana.apps
{
    class makechan : nSampleApp
    {
        /**
         * Creates a nirvana channel
         */
        private static makechan mySelf = null;
        /**
         * This method demonstrates the Nirvana API calls necessary to create
         * a channel.
         * It is called after all command line arguments have been received and
         * validated
         *
         * @param realmDetails a String[] containing the possible RNAME values
         * @param achannelName the channel name to create
         */
        private void doit(String[] realmDetails, String achannelName, int age, int cap, String type, bool cluster, long eid)
        {
            mySelf.constructSession(realmDetails);
            //Creates the specified channel
            try
            {
                //Create a channel attributes object
                nChannelAttributes nca = createChannelAttributes(achannelName, age, cap, type, cluster);
                //Create the channel
                nChannel myChannel = mySession.createChannel(nca, eid);
            }
            //Handle errors
            catch (nChannelNotFoundException cnfe)
            {
                Console.WriteLine("The channel specified could not be found.");
                Console.WriteLine("Please ensure that the channel exists in the REALM you connect to.");
                Environment.Exit(1);
            }
            catch (nSecurityException se)
            {
                Console.WriteLine("Unsufficient permissions for the requested operation.");
                Console.WriteLine("Please check the ACL settings on the server.");
                Environment.Exit(1);
            }
            catch (nSessionNotConnectedException snce)
            {
                Console.WriteLine("The session object used is not physically connected to the Nirvana realm.");
                Console.WriteLine("Please ensure the realm is up and check your RNAME value.");
                Environment.Exit(1);
            }
            catch (nUnexpectedResponseException ure)
            {
                Console.WriteLine("The Nirvana REALM has returned an unexpected response.");
                Console.WriteLine("Please ensure the Nirvana REALM and client API used are compatible.");
                Environment.Exit(1);
            }
            catch (nUnknownRemoteRealmException urre)
            {
                Console.WriteLine("The channel specified resided in a remote realm which could not be found.");
                Console.WriteLine("Please ensure the channel name specified is correct.");
                Environment.Exit(1);
            }
            catch (nRequestTimedOutException rtoe)
            {
                Console.WriteLine("The requested operation has timed out waiting for a response from the REALM.");
                Console.WriteLine("If this is a very busy REALM ask your administrator to increase the client timeout values.");
                Environment.Exit(1);
            }
            catch (nChannelAlreadyExistsException caee)
            {
                Console.WriteLine("The channel specified already exists on the REALM.");
                Environment.Exit(1);
            }
            catch (nBaseClientException nbce)
            {
                Console.WriteLine("An error occured while creating the Channel Attributes object.");
                Environment.Exit(1);
            }
            //Close the session we opened
            try
            {
                nSessionFactory.close(mySession);
            }
            catch (Exception ex) { }
            //Close any other sessions within this JVM so that we can exit
            nSessionFactory.shutdown();
        }
        private nChannelAttributes createChannelAttributes(String achannelName, int age, int cap, String type, bool cluster)
        {
            //Create a channel attributes object
            nChannelAttributes nca = new nChannelAttributes();
            //Set the channel name
            nca.setName(achannelName);
            //Set the channel type parameter (reliable or guaranteed)
            if (type != null)
            {
                if (type.Equals("R"))
                {
                    nca.setType(nChannelAttributes.RELIABLE_TYPE);
                }
                else if (type.Equals("P"))
                {
                    nca.setType(nChannelAttributes.PERSISTENT_TYPE);
                }
                else if (type.Equals("M"))
                {
                    nca.setType(nChannelAttributes.MIXED_TYPE);
                }
                else if (type.Equals("S"))
                {
                    nca.setType(nChannelAttributes.SIMPLE_TYPE);
                }
                else if (type.Equals("T"))
                {
                    nca.setType(nChannelAttributes.TRANSIENT_TYPE);
                }
            }
            else
            {
                nca.setType(nChannelAttributes.SIMPLE_TYPE);
            }
            //Set the channel capacity parameter
            nca.setMaxEvents(cap);
            //Set the channel Time to Live parameter
            nca.setTTL(age);
            // set cluster wide flag
            nca.setClusterWide(cluster);

            return nca;
        }
        /**
         * Prints the usage message for this class
         */
        private static void Usage()
        {
            Console.WriteLine("Usage ...\n");
            Console.WriteLine("nmakechan <rname> <channel name> [time to live] [capacity] [type] [cluster wide] [start eid] \n");
            Console.WriteLine(
              "<Required Arguments> \n");
            Console.WriteLine(
              "<rname> - the rname of the server to connect to");
            Console.WriteLine(
              "<channel name> - Channel name parameter for the channel to be created");
            Console.WriteLine(
              "\n[Optional Arguments] \n");
            Console.WriteLine(
              "[time to live] - The Time To Live parameter for the new channel (default: 0)");
            Console.WriteLine(
              "[capacity] - The Capacity parameter for the new channel (default: 0)");
            Console.WriteLine(
              "[type] - The type parameter for the new channel (default: S)");
            Console.WriteLine(
              "   R - For a reliable (stored in memory) channel with persistent eids");
            Console.WriteLine(
              "   P - For a persistent (stored on disk) channel");
            Console.WriteLine(
              "   S - For a simple (stored in memory) channel with non-persistent eids");
            Console.WriteLine(
              "   T - For a transient (no server based storage)");
            Console.WriteLine(
              "   M - For a Mixed (allows both memory and persistent events) channel\n");
            Console.WriteLine(
              "[cluster wide] - Whether the channel is cluster wide. Will only work if the realm is part of a cluster");
            Console.WriteLine(
              "[start eid] - The initial start event id for the new channel (default: 0)");
        }
        protected override void processArgs(String[] args)
        {
            //
            // Need a min of 2, rname, channel name, capacity, ttl, type
            if (args.Length < 2)
            {
                Usage();
                UsageEnv();
                Environment.Exit(2);
            }
            String RNAME = args[1]; ;
            var channelName = args[2];
            int age = 0;
            int cap = 0;
            String type = "S";
            bool cluster = false;
            long eid = 0;
            //Check the channel name specified
            if (args.Length > 3)
            {
                age = Convert.ToInt32(args[3]);
            }
            if (args.Length > 4)
            {
                cap = Convert.ToInt32(args[4]);
            }
            if (args.Length > 5)
            {
                type = args[5];
            }
            if (args.Length > 6)
            {
                cluster = Convert.ToBoolean(args[6]);
            }
            if (args.Length > 7)
            {
                eid = Convert.ToInt32(args[8]);
            }
            //
            // Run the sample app
            //
            mySelf.doit(parseRealmProperties(RNAME), channelName, age, cap, type, cluster, eid);
        }
        static void Main(string[] args)
        {
            //Create an instance for this class
            mySelf = new makechan();
            mySelf.processArgs(Environment.GetCommandLineArgs());
        }
    }
}
EXAMPLE_SOURCE_END
Share this page with others:
Tell Your Tweets Facebook It! Add to Delicious Reddit! Digg It! Stumble Upon Add to Your Faves Mixx it
Follow Us:
Keep up with my-Channels on Twitter Become a fan on Facebook LinkedIn Profile Recent Highlights RSS Feed