4

I have looked everywhere for this and I can't find the answer. I even saw a copy of the Restlet book, which gives only a partial answer and even that partial answer is wrong.

What I am trying to do is very simple. I need to do a simple GET request to an HTTP URL. I know how to do this synchronously:

Engine.getInstance().getRegisteredClients().clear();
Engine.getInstance().getRegisteredClients().add(new HttpClientHelper(null));

ClientResource resource=new ClientResource(url);
Representation rep=resource.get();
String respText=rep.getText();
// handle the response in respText as you see fit

The problem of course is that this blocks on resource.get() until a response is received. What I really want to do is to do this asynchronously, ie set a callback (in using the resource.setOnResponse method?) and then firing off the request without blocking. I would also like to set a timeout value so that if I don't receive a timeout in a reasonable amount of time it fires off some sort of onTimeout or onError method.

One would think that this is a very common thing that someone might want to do with Restlet, yet I can find no documentation that discusses this. The only discussion I see is in the Restlet book, where it says in Listing 9.2 that the get() method does not block, when in fact it does. In other words, I tried this:

    Engine.getInstance().getRegisteredClients().clear();
    Engine.getInstance().getRegisteredClients().add(new HttpClientHelper(null));

    ClientResource resource=new ClientResource(url);
    resource.setOnResponse(new Uniform() {
            public void handle(Request request, Response response) {
                try {
                    int statusCode=response.getStatus().getCode();
                    // Print status code, should be 200
                    System.out.println("Status code is "+statusCode);
                    System.out.println("");
                    System.out.println("");
                    if (statusCode==200) {
                        onSuccess(response);  // this is my own success handler method
                    } else {
                        System.out.println("ERROR: Bad response from server");
                    }
                } catch (Exception ex) {
                    // handle exception
                }
            }
    });
    System.out.println("Before resource get");
    resource.get();  // This blocks!!

Can someone please show me how to do this? Thanks.

Marc
  • 3,386
  • 8
  • 44
  • 68
  • Well, it gets worse. I tried setting the timeout like this: Context context = new Context(); context.getParameters().set("socketConnectTimeoutMs", "2000"); ClientResource resource=new ClientResource(context,url); and what happens is that it does indeed timeout but it throws an uncatchable exception of type org.apache.http.conn.ConnectTimeoutException deep in the Restlet code. – Marc Aug 01 '13 at 03:26
  • My problem is that I need a timeout. But I don't get a uncatchable exception like you?! Where exactly do you get your exception? – Charmin Oct 29 '13 at 13:33

1 Answers1

-1

Take a look at the following discussion, it discusses two issues which might be causing the problem.

http://restlet-discuss.1400322.n2.nabble.com/Using-Asynch-setOnResponse-td7578750.html

Suresh Kumar
  • 11,241
  • 9
  • 44
  • 54
  • 1
    Thanks for pointing me to the mailing list, but the answers there don't seem to help. I posted on the mailing list and hopefully I will get some sort of response. The list admin says that get() shouldn't block, but I observe that it does block. And the listpost doesn't say anything about timeout. – Marc Aug 01 '13 at 03:24