lundi 23 février 2015

How do I use Spring Reactor reactive streams to compose an aggregated set of responses?

Apologies for not making the question more specific, but I am struggling to make progress with Spring Reactor beyond the most trivial examples. Despite reading a lot about the Reactive programming approach, I don't think I have a handle on it yet.


Let's assume I have a service that receives HTTP requests. Each individual HTTP request is an XML document that contains a large number of items. Lets call them Apples and Oranges. All my service needs to do is to convert my HTTP request into a series of 'Apple', and a series of 'Orange', and send them on to the appropriate downstream service for handling that type of item. Once all of the downstream services have returned all of their responses, I need to aggregate them all and return a result to the caller.


So, my starting point is a:



Stream<FullHttpRequest> request = input.in();


Which actually just represents a single HTTP request containing my XML document. I gather I turn it into a Stream of something else using the '.map(Function)' function. But because 'Apple' and 'Orange' have no super class in common, I have to invent a wrapper that contains both, and a function to create instances of the wrapper. So I get something like:



Stream<AppleOrOrange> items = input.in().map(func);


Where 'func' is a function that takes a FullHttpRequest and returns a.. wait. This is where I start hitting problems. It returns a "List< AppleOrOrange >", but I don't want a "Stream< List< AppleOrOrange > >", I want a "Stream< AppleOrOrange >" and I don't know how to get it.


Let's say I get as far as creating a "Stream< AppleOrOrange >" - I can then attach a Consumer that routes the Apple or Orange to the right service. Let's assume that I get back an AppleResponse or OrangeResponse, depending on the service.



Stream<AppleOrOrange> items = input.in().map(func);
items.consume(sendDownstreamConsumer);


But then how do I respond aggregate the responses once they are returned?


Aucun commentaire:

Enregistrer un commentaire