public bool Connect()
    {
        if (emsSession == null || emsSession.IsClosed)
        {
            try
            {
                ConnectionFactory factory = new TIBCO.EMS.ConnectionFactory(emsServerUrl);

                // create the emsConnection
                emsConnection = factory.CreateConnection(emsUserName, emsUserPassword);

                Utilities.WriteLog(String.Format(@"Подсоединено к : {0};", emsServerUrl));

                // create the emsSession
                emsSession = emsConnection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);
                Utilities.WriteLog(String.Format(@"Создана сессия;"));

                // set the exception listener
                emsConnection.ExceptionListener = this;

                msgProducer = emsSession.CreateProducer(destination);
                Utilities.WriteLog(String.Format(@"Создан продюсер;"));

                completionListener = new EMSCompletionListener(entitiesModel);

                // create the emsDestination
                if (useTopic)
                    emsDestination = emsSession.CreateTopic(_emsInputQueueName);
                else
                    emsDestination = emsSession.CreateQueue(_emsInputQueueName);

                if (useTopic)
                    destination = emsSession.CreateTopic(_emsOutputQueueName);
                else
                    destination = emsSession.CreateQueue(_emsOutputQueueName);

                var message = String.Format(@"Подписано на события очереди: {0}",_emsInputQueueName);
                Utilities.WriteLog(message);

                // create the consumer
                msgConsumer = emsSession.CreateConsumer(emsDestination);
                Utilities.WriteLog(String.Format(@"Создан консюмер: {0}", emsDestination.ToString()));

                // set the message listener
                msgConsumer.MessageListener = this;

                xmlHelper = new XMLHelper();
                // start the emsConnection
                emsConnection.Start();

                // Note: when message callback is used, the emsSession
                // creates the dispatcher thread which is not a daemon
                // thread by default. Thus we can quit this method however
                // the application will keep running. It is possible to
                // specify that all emsSession dispatchers are daemon threads.
                return true;
            }
            catch (Exception ex)
            {
                Utilities.WriteExceptionMessageToLog(ex, String.Format(@"Ошибка подключения к очереди {0}", _emsInputQueueName));
                return false;
            }
        }
        return true;
    }
 private static Destination CreateDestination(Session sess, string name, QueueType type)
 {
     Destination dest;
     switch (type)
     {
         case QueueType.Queue:
             dest = sess.CreateQueue(name);
             break;
         case QueueType.Topic:
             dest = sess.CreateTopic(name);
             break;
         default:
             throw new ApplicationException("Internal error");
     }
     return dest;
 }
    public csMsgProducer(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("csMsgProducer SAMPLE");
        Console.WriteLine("------------------------------------------------------------------------");
        Console.WriteLine("Server....................... " + ((serverUrl != null)?serverUrl:"localhost"));
        Console.WriteLine("User......................... " + ((userName != null)?userName:"******"));
        Console.WriteLine("Destination.................. " + name);
        Console.WriteLine("Send Asynchronously.......... " + useAsync);
        Console.WriteLine("Message Text................. ");

        for (int i = 0; i < data.Count; i++)
        {
            Console.WriteLine(data[i]);
        }
        Console.WriteLine("------------------------------------------------------------------------\n");

        try
        {
            TextMessage msg;
            int i;

            if (data.Count == 0)
            {
                Console.Error.WriteLine("Error: must specify at least one message text\n");
                Usage();
            }

            Console.WriteLine("Publishing to destination '" + name + "'\n");

            ConnectionFactory factory = new TIBCO.EMS.ConnectionFactory(serverUrl);

            connection = factory.CreateConnection(userName, password);

            // create the session
            session = connection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);

            // create the destination
            if (useTopic)
                destination = session.CreateTopic(name);
            else
                destination = session.CreateQueue(name);

            // create the producer
            msgProducer = session.CreateProducer(null);

            if (useAsync)
                completionListener = new EMSCompletionListener();

            // publish messages
            for (i = 0; i < data.Count; i++)
            {
                // create text message
                msg = session.CreateTextMessage();

                // set message text
                msg.Text = (String) data[i];

                // publish message
                if (useAsync)
                    msgProducer.Send(destination, msg, completionListener);
                else
                    msgProducer.Send(destination, msg);

                Console.WriteLine("Published message: " + data[i]);
            }

            // close the connection
            connection.Close();
        }
        catch (EMSException e)
        {
            Console.Error.WriteLine("Exception in csMsgProducer: " + e.Message);
            Console.Error.WriteLine(e.StackTrace);
            Environment.Exit(-1);
        }
    }