activemq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ajsri77 <ajsr...@gmail.com>
Subject Writing a Listener Windows Service .NET for Active MQ
Date Fri, 21 Nov 2008 14:10:56 GMT

hi all.,

I'm trying to write a Windows Service in C#, which can create a consumer and
listen for the specified Active Message Queue.

I'm using the SimpleMessageListenerContainer to create the connection to the
activemq. 

All i'm getting is the Listener Class (Consumer) is getting created
successfully and displayed in the Topics of my activeMQ admin web console. 

But the OnMessage function is fired when i receive any message. I dont know
where i'm doing wrong.

Can you please help me sort out this problem.

the coding is as below.

private void InitListener()
        {
            try
            {
                ConnectionFactory connectionFactory = new
ConnectionFactory(URI);

                using (SimpleMessageListenerContainer listenerContainer =
new SimpleMessageListenerContainer())
                {                    
                    listenerContainer.ConnectionFactory = connectionFactory;
                    listenerContainer.DestinationName = DESTINATION;                    
                    listenerContainer.MessageListener = new Listener();                  
 
                    listenerContainer.AfterPropertiesSet();        
                    DAL.WriteToLog("Listener Properties set at " +
DateTime.Now.ToString() + Environment.NewLine);                    
                }
            }
            catch (Exception ex)
            {
                DAL.WriteToLog("Error occured at " + DateTime.Now.ToString()
+ Environment.NewLine);
                DAL.WriteToLog(ex.Message + " " + DateTime.Now.ToString() +
Environment.NewLine);
            }
        }

This function i'm calling in OnStartup Method of the Service.

And my Listener class is as follows.

class Listener : IMessageListener
    {
        public Listener()
        {
            DAL.WriteToLog("Listener created at " + DateTime.Now.ToString()
+ Environment.NewLine);
        }

        #region IMessageListener Members

        public void OnMessage(Apache.NMS.IMessage message)
        {
            DAL.WriteToLog("Message Received " + DateTime.Now.ToString() +
Environment.NewLine);
            
            LogMessage mq;

            IObjectMessage objMessage = message as IObjectMessage;

            // Deserialize the Log Message Object

            MemoryStream myStream = (MemoryStream)objMessage.Body;

            BinaryFormatter formatter = new BinaryFormatter();
                        
            myStream.Position = 0;

            mq = (LogMessage)formatter.Deserialize(myStream);

            DAL.WriteToLog("Message Deserialized at " +
DateTime.Now.ToString() + Environment.NewLine);

            myStream.Close();

            DAL.WriteToLog("Inserting Log : " + mq.ErrorLevel + " - " +
mq.ErrorDate + Environment.NewLine);

            DAL.Insert(mq);

            DAL.WriteToLog("Done" + Environment.NewLine);
        }

        #endregion
    }

Can anyone point out me where and what i'm doing wrong. it is driving me
crazy. Because the same code if i put in a console application is working
very well... i donna what to do?

Thanks in advance for helping.
-- 
View this message in context: http://www.nabble.com/Writing-a-Listener-Windows-Service-.NET-for-Active-MQ-tp20621820p20621820.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.


Mime
View raw message