public csMSDTCConsumer(string[] args)
    {
        ParseArgs(args);

        try
        {
            tibemsUtilities.initSSLParams(serverUrl, args);
        }
        catch (Exception e)
        {
            System.Console.WriteLine("Exception: " + e.Message);
            System.Console.WriteLine(e.StackTrace);
            System.Environment.Exit(-1);
        }

        Console.WriteLine("\n------------------------------------------------------------------------");
        Console.WriteLine("csMSDTConsumer");
        Console.WriteLine("------------------------------------------------------------------------");
        Console.WriteLine("Server....................... " + ((serverUrl != null)?serverUrl:"localhost"));
        Console.WriteLine("User......................... " + ((userName != null)?userName:"******"));
        Console.WriteLine("Queue........................ " + queueName);
        Console.WriteLine("Count........................ " + count);
        Console.WriteLine("Number of Txns............... " + numTxns);
        Console.WriteLine("Client ID.................... " + ((clientID != null)?clientID:"(null)"));
        Console.WriteLine("------------------------------------------------------------------------\n");

        try {
            factory = new TIBCO.EMS.EMSDTCConnectionFactory(serverUrl);
            // set the clientID before creating the connection, since clientID is required
            factory.SetClientID(clientID);

            connection = factory.CreateEMSDTCConnection(userName, password);
            session    = connection.CreateEMSDTCSession();

            // start the connection
            connection.Start();

            Console.WriteLine("Receive Msgs from queue: " + queueName);

            // Use createQueue to create the queue
            Queue queue = session.CreateQueue(queueName);

            // create the consumer
            msgConsumer = session.CreateConsumer(queue);

            for (int i = 0; i < numTxns;)
            {
                try
                {
                    // demonstrates the use of Transaction scope.
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        // receive msgs
                        this.receiveMsgs(msgConsumer);

                        // the application can do some other work on another resource
                        // manager as part of this msdtc txn. When the transaction is
                        // commited the DTC will coordinate with the resource manager's
                        // and run a two phase commit protocol (if there is more than
                        // one resource) and based on the outcome from other RM's
                        // involved, commit or rollback the txn.
                        // For example one can create another connection to another
                        // EMS server another session and send msgs to another queue
                        // on this new EMS server as part of the same MSDTC
                        // distributed txn.
                        //
                        // In this example, since there is only one RM, it will result
                        // is single phase commit.

                        // commit the transaction.
                        scope.Complete();
                    }

                    i++;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.StackTrace);
                    break;
                }
            }

            Console.WriteLine("Closing connection ..");
            // close the connection
            connection.Close();
        }

        catch (EMSException e)
        {
            Console.Error.WriteLine("Exception caught: " + e.Message);
            Console.Error.WriteLine(e.StackTrace);
            if (e.LinkedException != null)
            {
                Console.Error.WriteLine(e.LinkedException);
            }

            Environment.Exit(0);
        }
    }
Beispiel #2
0
    public csMSDTCProducer(string[] args)
    {
        ParseArgs(args);

        try
        {
            tibemsUtilities.initSSLParams(serverUrl, args);
        }
        catch (Exception e)
        {
            System.Console.WriteLine("Exception: " + e.Message);
            System.Console.WriteLine(e.StackTrace);
            System.Environment.Exit(-1);
        }

        Console.WriteLine("\n------------------------------------------------------------------------");
        Console.WriteLine("csMSDTCProducer");
        Console.WriteLine("------------------------------------------------------------------------");
        Console.WriteLine("Server....................... " + ((serverUrl != null)?serverUrl:"localhost"));
        Console.WriteLine("User......................... " + ((userName != null)?userName:"******"));
        Console.WriteLine("Queue........................ " + queueName);
        Console.WriteLine("Count........................ " + count);
        Console.WriteLine("Number of Txns............... " + numTxns);
        Console.WriteLine("Client ID.................... " + ((clientID != null)?clientID:"(null)"));
        Console.WriteLine("------------------------------------------------------------------------\n");

        try {
            factory = new TIBCO.EMS.EMSDTCConnectionFactory(serverUrl);
            // set the clientID before creating the connection, since clientID is required
            factory.SetClientID(clientID);

            connection = factory.CreateEMSDTCConnection(userName, password);
            session    = connection.CreateEMSDTCSession();

            Console.WriteLine("Sending Msgs to queue: " + queueName);

            // Use createQueue to create the queue
            Queue queue = session.CreateQueue(queueName);

            // create the producer and set delivery mode to peristent
            msgProducer = session.CreateProducer(queue);
            msgProducer.DeliveryMode = DeliveryMode.PERSISTENT;

            for (int i = 0; i < numTxns;)
            {
                try
                {
                    // create a commitable transaction.
                    CommittableTransaction txn = new CommittableTransaction();

                    // set the ambient transaction and a completion handler.
                    System.Transactions.Transaction.Current = txn;

                    // send msgs
                    this.sendMsgs(msgProducer);

                    // the application can do some other work on another resource
                    // manager as part of this msdtc txn. When the transaction is
                    // commited the DTC will coordinate with the resource manager's
                    // and run a two phase commit protocol (if there is more than
                    // one resource) and based on the outcome
                    // from other RM's involved commit or rollback the txn.
                    // In this example, since there is only one RM, it will result
                    // is single phase commit.

                    // commit the transaction.
                    txn.Commit();

                    // next txn.
                    i++;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.StackTrace);
                    break;
                }
            }

            Console.WriteLine("Closing connection ..");
            // close the connection
            connection.Close();
        }
        catch (EMSException e)
        {
            Console.Error.WriteLine("Exception caught: " + e.Message);
            Console.Error.WriteLine(e.StackTrace);
            if (e.LinkedException != null)
            {
                Console.Error.WriteLine(e.LinkedException);
            }

            Environment.Exit(0);
        }
    }