gpt4 book ai didi

c# - 处理我刚刚用 ActiveMQ 和 C# 发送的消息

转载 作者:太空宇宙 更新时间:2023-11-03 19:26:14 26 4
gpt4 key购买 nike

我是将 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);
}
}
}
}
}

如果我关闭我的 Windows 窗体并重新启动它,那么我的标签已更新,但我不想关闭它并重新打开它。

你们有什么想法吗?

最佳答案

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

订户类

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 的小文章.免责声明:这是我的网站,这篇文章是我写的。抱歉 self 宣传。但我觉得这与主题相关。

关于c# - 处理我刚刚用 ActiveMQ 和 C# 发送的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8452323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com