Return from one method when another method is called

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • magne

    Return from one method when another method is called

    I have an http module (not made by me):

    To connect to a server and send a request, there is a method like
    this:

    public void sendRequest(Str ing query){
    ....
    }

    When the server responds, it calls another method in the same class:

    public void respons(String results){
    ....
    }


    I want to combine these two methods so that my application can use one
    method looking something like this:

    public String sendRequest(Str ing xml){
    ....
    }

    which returns the results. Is this possible?

    I think it must be to include the second method someway in the first
    method and wait for the respons, but I don't know how to do so.

    To summarize, the problem is to make one method return something when
    another method is called. I guess there are some smart ways around
    this problem, but I haven't found any yet.
  • Joost van Stuijvenberg

    #2
    Re: Return from one method when another method is called

    [color=blue]
    > I want to combine these two methods so that my application can use one
    > method looking something like this:
    > public String sendRequest(Str ing xml){
    > ...
    > }
    > which returns the results. Is this possible?[/color]
    [color=blue]
    > I think it must be to include the second method someway in the first
    > method and wait for the respons, but I don't know how to do so.
    >
    > To summarize, the problem is to make one method return something when
    > another method is called. I guess there are some smart ways around
    > this problem, but I haven't found any yet.[/color]

    You'll need some object to lock on, so you can wait for the response.
    However, in your current setup I cannot figure any way to wait for the
    response. Please take a look at Mark Grand's "Future" design pattern at:



    This is an approach that worked for me; I needed to send multiple
    requests to a site and await each response 'synchronously' in its own
    thread and return the response to the caller. This design pattern
    encapsulates sync/async issues (a bit more than you actually need) and
    provides locking.

    This all means you can call a method and receive its return variable as
    if the communication went synchronously, even though the actual
    communication was asynchronously.

    Regards,

    Joost van Stuijvenberg

    Comment

    Working...