title: Make asynchronous web service call as synchronous in Java
body: <p>I am beginner in web service development.
  We are building REST web application in java using Spring 3.</p>
  
  <p>Web service which we are consuming has an asynchronous login method. We have provided them a call back listener URL where their service postback the response.</p>
  
  <p>So when we send a request for login we receive a blank response as an acknowledgment. And service send a response with actual data on the listener URL.</p>
  
  <p>Please help, how should i design/implement to call login service as synchronous call?
  Thanks.</p>
  
  <p><strong>EDIT:</strong>
  Below is the listener/controller of postback message which extract the token and set into a object.</p>
  
  <pre><code>@Controller
  public class PostbackController {
  
      @Autowried
      public LoginResponse loginRS;
  
      @RequestMapping(value = "/callbacklistener", method = RequestMethod.POST)
      @ResponseBody
      public String postbackHandler(@RequestParam("postback") String requestMessage) {
          //We extract the token value from requestMessage.
          String token = requestMessage;
          loginRS.setToken(token);
          return "&lt;Confirm/&gt;";
      }
  }
  </code></pre>
  
  <p>Below is a thread which call the login service and wait for 10 sec. Every 2 sec it checks for that status of object.</p>
  
  <pre><code>public class LoginService extends Thread {
  
      @Autowried
      public LoginResponse loginRS;
  
      public LoginService() {
          this.start();
      }
  
      @Override
      public void run() {
          try {
              System.out.println("Thread Start.");
              this.login();
              System.out.println("Thread Complete.");
          } catch (InterruptedException e) {}
      }
  
      public LoginResponse login() {
          String loginXML = "";
          String response = "";//by calling REST web service using RESTtemplate we get blank response.
  
          for(i=0; i &lt; 6; i++) {
              if(loginRS.getToken().length &gt; 0))  {
                  //Got the response exit from thread
                  break;
              }
              else {
                  //Wait for 2 second
                  Thread.sleep(2000)
              }
          }
          return loginRS;
      }
  }
  </code></pre>
  
  <p>Please let me know if you need more information. Thanks</p>