title: DeferredResult in spring mvc
body: <p>I have one class that extends DeferredResults and extends Runnable as shown below</p>
  
  <pre><code>public class EventDeferredObject&lt;T&gt; extends DeferredResult&lt;Boolean&gt; implements Runnable {
  
      private Long customerId;
  
      private String email;
  
      @Override
      public void run() {
  
          RestTemplate restTemplate=new RestTemplate();
  
          EmailMessageDTO emailMessageDTO=new EmailMessageDTO("dineshshe@gmail.com", "Hi There");
  
          Boolean result=restTemplate.postForObject("http://localhost:9080/asycn/sendEmail", emailMessageDTO, Boolean.class);
  
          this.setResult(result);
      }
  
  //Constructor and getter and setters
  }
  </code></pre>
  
  <p>Now I have controller that return the object of the above class,whenever new request comes to controller we check if that request is present in HashMap(That stores unprocessed request at that instance).If not present then we are creating object of EventDeferredObject class can store that in HashMap and call start() method on it.If this type request is already present then we will return that from HashMap.On completion on request we will delete that request from HashMap.</p>
  
  <pre><code>@RequestMapping(value="/sendVerificationDetails")
  
  public class SendVerificationDetailsController {
  
  
  private ConcurrentMap&lt;String , EventDeferredObject&lt;Boolean&gt;&gt; requestMap=new  ConcurrentHashMap&lt;String , EventDeferredObject&lt;Boolean&gt;&gt;(); 
  
      @RequestMapping(value="/sendEmail",method=RequestMethod.POST)
      public EventDeferredObject&lt;Boolean&gt; sendEmail(@RequestBody EmailDTO emailDTO)
      {
          EventDeferredObject&lt;Boolean&gt; eventDeferredObject = null;
  
          System.out.println("Size:"+requestMap.size());
  
          if(!requestMap.containsKey(emailDTO.getEmail()))
          {
              eventDeferredObject=new EventDeferredObject&lt;Boolean&gt;(emailDTO.getCustomerId(), emailDTO.getEmail());
              requestMap.put(emailDTO.getEmail(), eventDeferredObject);
  
              Thread t1=new Thread(eventDeferredObject);
              t1.start();
  
          }
          else
          {
              eventDeferredObject=requestMap.get(emailDTO.getEmail());
  
          }
          eventDeferredObject.onCompletion(new Runnable() {
  
              @Override
              public void run() {
                  if(requestMap.containsKey(emailDTO.getEmail()))
                  {   
                      requestMap.remove(emailDTO.getEmail());
                  }
              }
          });
  
          return eventDeferredObject;
      }
  
  }
  </code></pre>
  
  <p>Now this code works fine if there no identical request comes to that stored in HashMap. If we give number of different request at same time code works fine.</p>