2011-12-09 45 views
1

我是使用ActiveMQ和C#的初学者。我创建了一个带有一个按钮和一个标签的简单Windows窗体。当我点击按钮时,我向队列发送一条消息,标签用我刚刚发送的消息进行初始化。当然,我可以直接初始化我的标签,但我希望我的表单宁愿使用队列中的消息来更新我的标签。处理我刚刚用ActiveMQ和C发送的消息#

问题是我没有设法处理消息以相同的形式更新我的标签。我的消费者代码根本没有被调用,但是它在我的表单的Load事件中被初始化。 下面的代码

protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 
     InitializeHandlerAMQ(); 
    } 

    private void InitializeHandlerAMQ() 
    { 
     Tchat tchat = null; 
     IDestination dest = _session.GetQueue(QUEUE_DESTINATION); 
     using(IMessageConsumer consumer = _session.CreateConsumer(dest)) 
     { 
      IMessage message; 
      while((message = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null) 
      { 
       var objectMessage = message as IObjectMessage; 
       if(objectMessage != null) 
       { 
        tchat = objectMessage.Body as Tchat; 
        if (tchat != null) 
        { 
         textBox2.Text += string.Format("{0}{1}", tchat.Message, Environment.NewLine); 
        } 
       } 
      } 
     } 
    } 

如果我闭上窗口的形式和重新启动它,然后我的标签以及更新,但我不希望将其关闭并重新打开它。

你有什么想法的家伙?

回答

5

尝试用这样的事件委托来创建一个类。

订户类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Apache.NMS; 
using Apache.NMS.ActiveMQ; 
using Apache.NMS.ActiveMQ.Commands; 

namespace Utilities 
{ 
    public delegate void QMessageReceivedDelegate(string message); 
    public class MyQueueSubscriber : IDisposable 
    { 
     private readonly string topicName = null; 
     private readonly IConnectionFactory connectionFactory; 
     private readonly IConnection connection; 
     private readonly ISession session; 
     private readonly IMessageConsumer consumer; 
     private bool isDisposed = false; 
     public event QMessageReceivedDelegate OnMessageReceived; 

     public MyQueueSubscriber(string queueName, string brokerUri, string clientId) 
     { 
      this.topicName = queueName; 
      this.connectionFactory = new ConnectionFactory(brokerUri); 
      this.connection = this.connectionFactory.CreateConnection(); 
      this.connection.ClientId = clientId; 
      this.connection.Start(); 
      this.session = connection.CreateSession(); 
      ActiveMQQueue topic = new ActiveMQQueue(queueName); 
      //this.consumer = this.session.CreateDurableConsumer(topic, consumerId, "2 > 1", false); 
      this.consumer = this.session.CreateConsumer(topic, "2 > 1"); 
      this.consumer.Listener += new MessageListener(OnMessage); 

     } 

     public void OnMessage(IMessage message) 
     { 
      ITextMessage textMessage = message as ITextMessage; 
      if (this.OnMessageReceived != null) 
      { 
       this.OnMessageReceived(textMessage.Text); 
      } 
     } 

     #region IDisposable Members 

     public void Dispose() 
     { 
      if (!this.isDisposed) 
      { 
       this.consumer.Dispose(); 
       this.session.Dispose(); 
       this.connection.Dispose(); 
       this.isDisposed = true; 
      } 
     } 

     #endregion 

    } 
} 

的WinForms 在Windows形成订阅队列这样

MyQueueSubscriber QueueSubscriber = new MyQueueSubscriber(QueueName, ActiveMQHost, QueueClientId); 
    QueueSubscriber.OnMessageReceived += new QMessageReceivedDelegate(QueueSubscriber_OnMessageReceived); 

static void QueueSubscriber_OnMessageReceived(string message) 
{ 
     SetText(message); 
} 

    private void SetText(string text) 
    { 
     // InvokeRequired required compares the thread ID of the 
     // calling thread to the thread ID of the creating thread. 
     // If these threads are different, it returns true. 
     if (this.textBox1.InvokeRequired) 
     { 
      SetTextCallback d = new SetTextCallback(SetText); 
      this.Invoke(d, new object[] { text }); 
     } 
     else 
     { 
      this.labelname.value = text; 
     } 
    } 

资源: 不幸的是没有那么多的资源,教C#& ActiveMQ。尝试使用http://activemq.apache.org/nms/,因为这非常好。

试试看http://www.codersource.net/MicrosoftNet/CAdvanced/PublishSubscribeinCusingActiveMQ.aspx的小文章。免责声明:这是我的网站,这篇文章是我写的。对不起,我自己宣传。但我觉得这与这个话题有关。

+0

另一个资源:“.NET中的ActiveMQ(示例聊天应用程序)”https://code.msdn.microsoft.com/windowsapps/ActiveMQ-in-NET-Sample-9406441a –

相关问题