public Producer()
        {
            string serverUrl = ConfigurationManager.AppSettings["serverUrl"];
            string userName = ConfigurationManager.AppSettings["userName"];
            string password = ConfigurationManager.AppSettings["password"];
            string topicName = ConfigurationManager.AppSettings["avnTopicName"];

            string inputQueueName = ConfigurationManager.AppSettings["inputQueueName"];

            try
            {
                ConnectionFactory factory = new TIBCO.EMS.ConnectionFactory(serverUrl);
                connection = factory.CreateConnection(userName, password);
                // create the session
                session = connection.CreateSession(false, Session.NO_ACKNOWLEDGE);
                // create the destination
                destination = session.CreateQueue(inputQueueName);

                //destination = session.CreateTopic(topicName);

                // create the producer
                msgProducer = session.CreateProducer(null);
                msg = session.CreateTextMessage();
            }
            catch (Exception ex)
            {
                Console.WriteLine("EMS Error: {0} {1}", ex.Message, ex.InnerException.Message);
            }
        }
        public void Reconnect()
        {
            if (Session != null && !Session.IsClosed)
                Session.Close();

            Provider.Connect();
            Session = Provider.Connection.CreateSession(false, SessionMode.AutoAcknowledge);
            Destination = Session.CreateQueue(QueueName);
            AfterReconnect();
        }
        public AsyncMessageConsumerUsingMessageListener(String QueueName, int NoOfSessions)
        {
            EMSQueueConnection.connection.Stop(); /* Iffy if multiple clients are using the conn.  */
            this.QueueName    = QueueName;
            this.NoOfSessions = NoOfSessions;

            TIBCO.EMS.Session         session     = null;
            TIBCO.EMS.MessageConsumer msgConsumer = null;
            TIBCO.EMS.Destination     destination = null;

            try
            {
                for (int i = 0; i < NoOfSessions; i++)
                {
                    /*
                     * Create a new session which in turn creates a thread interanlly .
                     * Ack mode is hard coded for now , see no possibulty for  it being other than CLient_ack*/
                    session = EMSQueueConnection.connection.CreateSession(false, Session.CLIENT_ACKNOWLEDGE);

                    // create the consumer
                    if (destination == null)
                    {
                        destination = session.CreateQueue(QueueName);
                    }
                    msgConsumer = session.CreateConsumer(destination);

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

                    /*
                     * Console.WriteLine("\n Subscribing to destination: " + QueueName);Console.WriteLine("************************************************\n ThreadName:"
                     + System.Threading.Thread.CurrentThread.Name + "\n  Session:" + session.ToString()
                     +
                     + "\n  SessionID:" + session.SessID + "\n  Connection:" + session.Connection.ToString() +
                     + "\n  MessageConsumer:" + msgConsumer.ToString()  );*/
                }

                EMSQueueConnection.connection.Start();
                // Start pub-sub messages
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Exception in AsyncMsgConsumer: " + e.Message);
                Console.Error.WriteLine(e.StackTrace);
            }
        }
 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 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;
    }
    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);
        }
    }