使用Apache NMS STOMP连接到RabbitMQ

本文关键字:RabbitMQ 连接 STOMP Apache NMS 使用 | 更新日期: 2023-09-27 18:02:04

我正在尝试使用STOMP协议向Rabbit MQ(3.6.5)队列读写消息。我使用Apache NMS Stomp(1.5.4)作为客户端库。

当使用NMS发送消息时,我得到以下异常:
输入字符串格式不正确。

原因是NMS希望message-id文件在特定位置包含一个数字。
这是来自NMS库的代码:

public void SetValue( String messageKey )
{
    key = messageKey;
    // Parse off the sequenceId
    var p = messageKey.LastIndexOf( ":" );
    if ( p >= 0 )
    {
        ProducerSequenceId = Int64.Parse( messageKey.Substring( p + 1 ) );
        messageKey = messageKey.Substring( 0, p );
    }
    ProducerId = new ProducerId( messageKey );
}

Rabbit MQ Broker发送的message-id字段具有以下值: " T_ID:无花果- 52033 - 636066062974737556 - 1:0:1:1@@session lonny1wnmfotxeevqmlhgg@@1"
NMS试图将"1@@session-Bo6HXXTZFSh51Qy7X4wx9A@@1"转换为Int64.

这是我的客户代码:

var connecturi = new Uri( "stomp:tcp://localhost:61613?transport.useInactivityMonitor=false&trace=true" );
Console.WriteLine( "About to connect to " + connecturi );
IConnectionFactory factory = new NMSConnectionFactory( connecturi );
using ( var connection = factory.CreateConnection( "XXXX", "XXXX" ) )
    using ( var session = connection.CreateSession() )
    {
        connection.Start();
        var destination = SessionUtil.GetDestination( session, "queue://FOO.BAR" );
        Console.WriteLine( "Using destination: " + destination );
        // Create a consumer and producer
        using ( var consumer = session.CreateConsumer( destination ) )
            using ( var producer = session.CreateProducer( destination ) )
            {
                // Start the connection so that messages will be processed.
                producer.DeliveryMode = MsgDeliveryMode.Persistent;
                // Send a message
                var request = session.CreateTextMessage( "Hello World! FROM NMS" );
                producer.Send( request );
                // Consume a message
                var message = consumer.Receive() as ITextMessage;
                if ( message == null )
                {
                    Console.WriteLine( "No message received!" );
                }
                else
                {
                    Console.WriteLine( "Received message with ID:   " + message.NMSMessageId );
                    Console.WriteLine( "Received message with text: " + message.Text );
                }
            }
    }

这个问题有解决办法吗?

  • 一种配置NMS以不同方式处理id的方法?
  • 告诉Rabbit MQ生成其他消息id的方法?

使用Apache NMS STOMP连接到RabbitMQ

我找到问题了。
Apache NMS STOMP期望消息id字段采用特定的格式。它们尝试将消息id的特定部分解析为Int64变量。(似乎是Apache MQ特定的)
他们在 1.7.1版本中修复了这个问题,遗憾的是,这个版本没有正式发布…看起来这个项目不是很活跃/已经死了。

Apache JIRA

我用最新源代码的构建替换了nuget包。

源代码可以在这里找到:SVN repo