我正在尝试使用c#使用stomp协议从activeMQ的队列接收/发送消息。因为我不太了解activemq和stomp。所以我正在寻找一些适当的文档或示例代码,我可以逐步学习。
static void Main(string[] args)
{
Apache.NMS.Stomp.ConnectionFactory factory = new Apache.NMS.Stomp.ConnectionFactory(new Uri("stomp:tcp://localhost:61613"));
IConnection connection = factory.CreateConnection();
ISession session = connection.CreateSession();
IDestination destination = session.GetDestination("/queue/notification");
IMessageConsumer consumer = session.CreateConsumer(destination);
connection.Start();
consumer.Listener += new MessageListener(OnMessage);
Console.WriteLine("Consumer started, waiting for messages... (Press ENTER to stop.)");
Console.ReadLine();
connection.Close();
}
private static void OnMessage(IMessage message)
{
try
{
Console.WriteLine("Median-Server (.NET): Message received");
ITextMessage msg = (ITextMessage)message;
message.Acknowledge();
Console.WriteLine(msg.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException.Message);
}
}
}
} 我试过这个。这是进行stomp连接的正确方法。
答案 0 :(得分:2)
有各种语言的STOMP客户端库,对于.NET,Apache.NMS.Stomp库将JMS类型的外观置于STOMP语义之外。如果您想获得更多技术并了解STOMP协议的真正含义,那么STOMP规范非常清晰且易于理解。当然,ActiveMQ自己的网站上有一些你应该阅读的STOMP suppor文件。使用NMS.Stomp库与ActiveMQ进行交互时,一些网页搜索也会很快给你一些不错的blog posts。