Esempio n. 1
0
        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);
            }
        }
        /// <summary>
        /// Opens if is not initialized yet, otherwise just returns with no action.
        /// </summary>
        public void Open()
        {
            if (!initialized)
            {

                ValidateQueueConfiguration();

                try
                {
                    factory = new ConnectionFactory(ServerConfig.Url, ServerConfig.ClientId);
                }
                catch (EMSException e)
                {
                    Log.TraceData(Log.Source, TraceEventType.Error, 15000, "URL/Client ID is wrong. " + e.ToString());
                    throw;
                }

                IConfigurationValueProvider configProvider = new SingleTagSectionConfigurationProvider(this.ServerConfig.AuthenticationSectionName);

                try
                {
                    connection = factory.CreateConnection(configProvider["userName"], configProvider["password"]);
                }
                catch (EMSException e)
                {
                    Log.TraceData(Log.Source, TraceEventType.Error, 15001, "Connection to ems server failed! " + e.ToString());
                    throw;
                }

                try
                {
                    session = connection.CreateSession(this.sessionConfig.IsTransactional, sessionConfig.Mode);

                }
                catch (EMSException e)
                {
                    Log.TraceData(Log.Source, TraceEventType.Error, 15002, "Error during session creation. " + e.ToString());
                    throw;
                }

                try
                {
                    destination =
                        CreateDestination(session, queueConfig.Name, queueConfig.Type);

                    consumer =
                        session.CreateConsumer(destination, queueConfig.MessageSelector,
                                               queueConfig.NoLocal);

                    connection.Start();
                }
                catch (EMSException e)
                {
                    Log.TraceData(Log.Source, TraceEventType.Error, 15003, "Queue initialization error. " + e);
                    throw;
                }
                initialized = true;
            }
        }
    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;
    }