Ankur Raiyani
Ankur Raiyani

Reputation: 1709

Make asynchronous web service call as synchronous in Java

I am beginner in web service development. We are building REST web application in java using Spring 3.

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.

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.

Please help, how should i design/implement to call login service as synchronous call? Thanks.

EDIT: Below is the listener/controller of postback message which extract the token and set into a object.

@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 "<Confirm/>";
    }
}

Below is a thread which call the login service and wait for 10 sec. Every 2 sec it checks for that status of object.

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 < 6; i++) {
            if(loginRS.getToken().length > 0))  {
                //Got the response exit from thread
                break;
            }
            else {
                //Wait for 2 second
                Thread.sleep(2000)
            }
        }
        return loginRS;
    }
}

Please let me know if you need more information. Thanks

Upvotes: 0

Views: 4974

Answers (1)

Narendra Pathai
Narendra Pathai

Reputation: 41945

Some Pseudo code to give you the idea

Login request sender Thread{

    acknowledgement = sendLoginRequest

    sleep() or wait on some lock

}


ListenerThread{

    response received = listenForResponse

    lock.notifyAll() or interrupt Login Thread

}

Doing this makes this synchronous.

UPDATE:

public class PostbackController {

        @Autowried
        public LoginResponse loginRS;
        //instance 1 injected by Spring where you set the token
}



public class LoginService extends Thread {

    @Autowried
    public LoginResponse loginRS;

    //a NEW instance will be created here also by Spring which will not have that token you set, as this is a new instance. So Thread will keep sleeping always.
}

Make PostbackController nested class of LoginService and use the same instance of PostbackController

Upvotes: 1

Related Questions