2

我有一个有 3 个状态的传奇;初始、接收行、已完成 -

    public static State Initial { get; set; }
    public static State ReceivingRows { get; set; }
    public static State Completed { get; set; }

当它收到 BofMessage(其中 Bof = 文件开头)时,它从 Initial 转换为 ReceivingRows。在 BofMessage 之后,它会接收大量 RowMessage,其中每个都描述平面文件中的一行。发送完所有 RowMessage 后,将发送 EofMessage 并且状态更改为 Completed。观察 -

    static void DefineSagaBehavior()
    {
        Initially(When(ReceivedBof)
            .Then((saga, message) => saga.BeginFile(message))
            .TransitionTo(ReceivingRows));

        During(ReceivingRows, When(ReceivedRow)
            .Then((saga, message) => saga.AddRow(message)));

        During(ReceivingRows, When(ReceivedRowError)
            .Then((saga, message) => saga.RowError(message)));

        During(ReceivingRows, When(ReceivedEof)
            .Then((saga, message) => saga.EndFile(message))
            .TransitionTo(Completed));
    }

这可行,除了有时在 BofMessage之前收到几个RowMessage!这与我发送给他们的顺序无关。这意味着消息将被接收并最终计为错误,导致它们从我最终将它们写入的数据库或文件中丢失。

作为一个临时修复,我在这个方法中添加了一个小睡眠定时器技巧来完成所有的发布——</p>

    public static void Publish(
        [NotNull] IServiceBus serviceBus,
        [NotNull] string publisherName,
        Guid correlationId,
        [NotNull] Tuple<string, string> inputFileDescriptor,
        [NotNull] string outputFileName)
    {
        // attempt to load offsets
        var offsetsResult = OffsetParser.Parse(inputFileDescriptor.Item1);
        if (offsetsResult.Result != ParseOffsetsResult.Success)
        {
            // publish an offsets invalid message
            serviceBus.Publish<TErrorMessage>(CombGuid.Generate(), publisherName, inputFileDescriptor.Item2);
            return;
        }

        // publish beginning of file
        var fullInputFilePath = Path.GetFullPath(inputFileDescriptor.Item2);
        serviceBus.Publish<TBofMessage>(correlationId, publisherName, fullInputFilePath);

        // HACK: make sure bof message happens before row messages, or else some row messages won't be received
        Thread.Sleep(5000);

        // publish rows from feed
        var feedResult = FeedParser.Parse(inputFileDescriptor.Item2, offsetsResult.Offsets);
        foreach (var row in feedResult)
        {
            // publish row message, unaligned if applicable
            if (row.Result != ParseRowResult.Success)
                serviceBus.Publish<TRowErrorMessage>(correlationId, publisherName, row.Fields);
            else
                serviceBus.Publish<TRowMessage>(correlationId, publisherName, row.Fields);
        }

        // publish end of file
        serviceBus.Publish<TEofMessage>(correlationId, publisherName, outputFileName);
    }

这是一个 5 秒的睡眠定时器,而且是一个非常丑陋的 hack。谁能告诉我为什么我没有按照发送顺序收到消息?如果默认情况下这些消息是无序的,我能否确保这些消息以正确的顺序发送?

谢谢!

请注意,为了方便起见,这是从http://groups.google.com/group/masstransit-discuss/browse_thread/thread/7bd9518a690db4bb交叉发布的。

4

1 回答 1

6

You cannot ensure messages get delivered in any order. You can get close in MT by ensuring there's only one concurrent consumer on the consumer side, I still wouldn't depend on this behaviour (http://docs.masstransit-project.com/en/latest/overview/keyideas.html#handlers). This would effectively make your consumer single threaded.

于 2012-05-23T11:26:36.107 回答