title: Guava EventBus: How to return result from event handler
body: <p>I have a web service which receives xml events from another system, process them using specific workflow and sends a list of potentials errors back as HTTP response.</p>
  
  <p>Event processing workflow consists of several handlers (let's say: <em>Preprocessor</em>, <em>Persister</em> and <em>Validator</em>) implemented using <a href="http://code.google.com/p/guava-libraries/wiki/EventBusExplained" rel="nofollow">Guava's EventBus</a>. Handlers send events to each other. Something like this: </p>
  
  <pre><code>public class RequestHandler {
  
      @RequestMapping
      public Errors handleRequest(String xmlData) {
          eventBus.post(new XmlReceivedEvent(xmlData));
          ...
          return errors; // how to get errors object from the last handler in chain ? 
      }
  }
  
  public class Preprocessor {
  
      @Subscribe
      public void onXmlReceived(XmlReceivedEvent event) {
         // do some pre-processing
         ...  
         eventBus.post(new PreprocessingCompleteEvent(preprocessingResult)); 
      }
  }
  
  public class Persister {
  
      @Subscribe
      public void onPreprocessingComplete(PreprocessingCompleteEvent event) {
         // do some persistence stuff
         ...    
         eventBus.post(new PersistenceCompleteEvent(persistenceResult)); 
      }
  }
  
  public class Validator {
  
      @Subscribe
      public void onPersistenceComplete(PersistenceCompleteEvent event) {
         // do validation
         ...    
         eventBus.post(new ValidationCompleteEvent(errors)); // errors object created, should be returned back to the RequestHandler 
      }
  }
  </code></pre>
  
  <p>The problem is: <strong>how to return the processing result</strong> deeply from <em>Validator</em> handler back to the starting point (<em>RequestHandler</em>), so user can receive HTTP response ?</p>
  
  <p>I consider two options:</p>
  
  <ol>
  <li><p>Set errors object to the initial XmlReceivedEvent and retrieve it after processing complete:</p>
  
  <pre><code>public class RequestHandler {
  
      @RequestMapping
      public Errors handleRequest(String xmlData) {
          XmlReceivedEvent event = new XmlReceivedEvent(xmlData);
          eventBus.post(event);
          ...
          return event.getErrors(); 
      }
  }
  </code></pre></li>
  </ol>
  
  <p>However, in that case I will have to pass errors object to each event in the chain to make it available for <em>Validator</em> to fill it with real data.</p>
  
  <ol>
  <li><p>Subscribe <em>RequestHandler</em> to <em>ValidationCompleteEvent</em> from <em>Validator</em> with populated errors object inside.</p>
  
  <pre><code>public class RequestHandler {
  
      private Errors errors;
  
      @RequestMapping
      public Errors handleRequest(String xmlData) {
          XmlReceivedEvent event = new XmlReceivedEvent(xmlData);
          eventBus.post(event);
          ...
          return this.errors; // ??? 
      }
  
      @Subscribe
      public void onValidationComplete(ValidationCompleteEvent event) {
          this.errors = event.getErrors();
      }
  }
  </code></pre></li>
  </ol>
  
  <p>But, unfortunately, <em>RequestHandler</em> is a Spring stateless service (singleton), so I would like to avoid saving any data in class fields.</p>
  
  <p>Appreciate any ideas.</p>