我需要使用来自MQ队列的消息,然后在最终确定队列使用时关闭控制台应用程序。我不知道消息何时在队列中。我能够成功完成读取,但是我不知道何时结束队列消耗就离开线程了。我需要知道如何识别这一刻。
this.connection = cf.CreateConnection();
this.connection.ExceptionListener = new ExceptionListener(this.OnException);
this.session = this.connection.CreateSession(true, AcknowledgeMode.AutoAcknowledge); // sessão com controle de transação
IDestination destQueue = this.session.CreateQueue(filaMensagem);
this.consumer = this.session.CreateConsumer(destQueue);
// Create an AutoResetEvent for signalling between threads
using (this.receiveCompleteEvent = new AutoResetEvent(false))
{
// Create and register the listener
this.consumer.MessageListener = new MessageListener(this.OnNewMessageCallback); // Callback for message handling
// Start the connection
this.connection.Start();
// Block and wait for the receiveCompleteEvent event to be signalled.
this.receiveCompleteEvent.WaitOne();
}
// dispose receiveCompleteEvent
this.consumer.Close();
this.session.Close();
this.connection.Close();