title: Composing multiple reactive Publisher/Flux/Mono objects into a blocking request
body: <p>I'm very new to the reactive world and struggling to understand how to accomplish a task.  I'm working on a legacy project where I must implement an interface that has many methods to query various objects from redis.  Sometimes the queries are as simple as querying a hash by ID, so it's a single call to redis to get the hash.  Other times, I may need to lookup the ID from a redis set first based some parameters, then get the hash with the resultant ID.  I'm using Reactor 3.1.0.M3 with Lettuce 5.0.0.RC1 in a Spring Boot application.</p>
  
  <p>My existing code to for these two example methods look like this:</p>
  
  <pre><code>    public &lt;T extends CatalogInfo&gt; T get(String id, Class&lt;T&gt; clazz) {
          String result = (String)repository.getRedisHashRepository().getHashById(CatalogUtils.root(clazz).getSimpleName(), id);
          if (null != result) {
              return serializer.fromData(result, clazz);
          }
          return null;
  
      }
  
      public &lt;T extends CatalogInfo&gt; T get(String attName, String attValue, Class&lt;T&gt; clazz) {
  
          String attKey = CatalogUtils.buildKey(CatalogUtils.root(clazz), attName, attValue);
  
          String id = CatalogUtils.getIdFromSet(repository.getRedisSetRepository().getSetMembers(attKey));
          if (id == null) {
              return null;
          }
          return get(id, clazz);
      }
  </code></pre>
  
  <p>There are some utility functions in there that just help me build the keys I want to use for redis from the Class name and to make sure the ID stored in the redis set is a single value.  As you can see, in the second get method, I call the first get method with the result from the set.</p>
  
  <p>Implementing the first method with Lettuce/Reactor was easy enough:</p>
  
  <pre><code>public &lt;T extends CatalogInfo&gt; Mono&lt;String&gt; getReactive(String id, Class&lt;T&gt; clazz, Publisher&lt;String&gt;... publisher) {
      Mono&lt;String&gt; mono = Mono.just(id).flatMap(new Function&lt;String, Mono&lt;String&gt;&gt;() {
          @Override
          public Mono&lt;String&gt; apply(String id) {
              return hashCommands.hget(CatalogUtils.root(clazz).getSimpleName(), id);
          }
      });
      return mono;
  }
  </code></pre>
  
  <p>At that point, I can call mono.block() to get the resulting value.  I can fairly easily get the functionality of the second get method working by chaining together two flatMaps/functions:</p>
  
  <pre><code>public &lt;T extends CatalogInfo&gt; Flux&lt;String&gt; getIdFromSetReactive(String attName, String attValue, Class&lt;T&gt; clazz) {
      String attKey = CatalogUtils.buildKey(CatalogUtils.root(clazz), attName, attValue);
  
      Flux&lt;String&gt; flux = Flux.just(attKey).flatMap(new Function&lt;String, Flux&lt;String&gt;&gt;() {
          @Override
          public Flux&lt;String&gt; apply(String attKey) {
              return commands.smembers(attKey);
          }
      }).flatMap(new Function&lt;String, Publisher&lt;String&gt;&gt;() {
          @Override
          public Publisher&lt;String&gt; apply(String id) {
              return hashCommands.hget(CatalogUtils.root(clazz).getSimpleName(), id);
          }
      });
      return flux;
  } 
  </code></pre>
  
  <p>I have many different types of methods that may require up to 8 calls to redis to complete.  In my original code, I could re-use each method and call one method from another, but I can't figure out how to do this with reactor.</p>
  
  <p>I would like to be able to call a method that builds a Flux to get the ID from a redis set (let's call it fluxA), then call another method that builds a Flux to query a redis hash based on ID (fluxB), etc.</p>
  
  <p>I <em>think</em> I might need to define each function I might need as member variables like this:</p>
  
  <pre><code>private Function&lt;String, Flux&lt;String&gt;&gt; getIdFromSetFunction = new Function&lt;String, Flux&lt;String&gt;&gt;() {
          @Override
          public Flux&lt;String&gt; apply(String attKey) {
              return commands.smembers(attKey);
          }
      }; 
  </code></pre>
  
  <p>and then make calls like</p>
  
  <pre><code>return Flux.just(attKey).flatMap(getIdFromSetFunction).flatMap(getHash);
  </code></pre>
  
  <p>The only issue is the code that gets executed in those functions need the Class information that is currently made available in my method calls.  But I'm not sure that is the right approach.</p>
  
  <p>Any advice would be greatly appreciated!</p>