.NET - 使用.NET反应的多线程生产者消费者

时间:2017-06-21 14:03:23

标签: .net multithreading system.reactive producer-consumer

我正在使用.NET反应来构建一个producer.consumer模式。制作人从Kafka消息总线读取消息。一旦读取消息,就需要将消息移交给消费者以处理消息。

我能够使用.NET反应来做到这一点。但是,我注意到消费者正在与生产者在同一个线程上处理消息。请参阅下面的代码。目标是:拥有一个从总线读取消息的生产者线程。然后,在一个单独的线程上将其交给消费者来处理消息。我的代码是:

 // Producer Code
 private Subject<LGMessage> _onMessageSubject = new Subject<LGMessage>();

 private IObserver<LGMessage> messageBusObserver;

 protected IObservable<LGMessage> _onMessageObservable
    {
        get
        {
            return _onMessageSubject.AsObservable();
        }
    }


public void AddObserver(IObserver<LGMessage> observer)
    {
       _onMessageObservable.ObserveOn(NewThreadScheduler.Default).Subscribe(observer);


    }


// Read is called when the message is read from the bus
public bool Read(Message<string, string> msg)
    {

            // add the message to the observable
            _onMessageSubject.OnNext(msg.Value);


    }

// Consumer Code
public virtual void OnNext(string value)
    {
        Console.WriteLine("Thread {0} Consuming",          

        Thread.CurrentThread.ManagedThreadId);

        Console.WriteLine("{1}: Message I got from bus: {0}", value.Key, 
         this.Name);
        // Take Action
    }

1 个答案:

答案 0 :(得分:2)

您的代码很难说清楚,但看起来您并没有暴露出可观察的内容。这否定了Rx运营商的下游使用。在您的情况下,您希望使用the threading operators

在制作人中,我不会暴露AddObserver(IObserver<string> observer),而是暴露出类似这样的东西:

public IObservable<string> Messages => _onMessageSubject.AsObservable();

消费者可以做类似

的事情
Messages
    .ObserveOn(NewThreadScheduler.Default)
    .Subscribe(consumerObserver);

修改

以下代码对我有用:

var subject = new Subject<int>();

var observer1 = new AnonymousObserver<int>(i => Console.WriteLine($"Observer1: Observed {i} on thread {Thread.CurrentThread.ManagedThreadId}."));
var observer2 = new AnonymousObserver<int>(i => Console.WriteLine($"Observer2: Observed {i} on thread {Thread.CurrentThread.ManagedThreadId}."));
var observer3 = new AnonymousObserver<int>(i => Console.WriteLine($"Observer3: Observed {i} on thread {Thread.CurrentThread.ManagedThreadId}."));

subject.AsObservable().ObserveOn(NewThreadScheduler.Default).Subscribe(observer1);
subject.AsObservable().ObserveOn(NewThreadScheduler.Default).Subscribe(observer2);
subject.AsObservable().ObserveOn(NewThreadScheduler.Default).Subscribe(observer3);

subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
subject.OnCompleted();

这里是输出(Observer1获得了线程14,Observer2获得了线程15,Observer3获得了线程16):

Observer1: Observed 1 on thread 14.
Observer2: Observed 1 on thread 15.
Observer1: Observed 2 on thread 14.
Observer1: Observed 3 on thread 14.
Observer2: Observed 2 on thread 15.
Observer2: Observed 3 on thread 15.
Observer3: Observed 1 on thread 16.
Observer3: Observed 2 on thread 16.
Observer3: Observed 3 on thread 16.