2011-12-09 4 views
1

私はC#でActiveMQを使用している初心者です。私は1つのボタンと1つのラベルで簡単なウィンドウフォームを作成しました。ボタンをクリックすると、キューにメッセージが送信され、ラベルは、送信したばかりのメッセージで初期化されます。もちろん、ラベルを直接初期化することはできますが、私のラベルを更新するために、フォームからキューからメッセージを消費するようにしてください。ActiveMQとCで送信したばかりのメッセージを処理してください

問題は、私のラベルを更新するために同じ形式でメッセージを処理できないということです。私のコンシューマーコードは全く呼び出されていませんが、私のフォームのLoadイベントで初期化されています。私は私のWindowsフォームを閉じて再起動した場合 は、ここでは、コード

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の この

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

別のリソース:「ActiveMQ in .NET(サンプルチャットアプリケーション)」https://code.msdn.microsoft.com/windowsapps/ActiveMQ-in-NET-Sample-9406441a –

関連する問題