java - Spring RestTemplate - Log on request sometimes hangs awaiting response -
i use rest template in java client log onto server , receive required headers upgrade connection secure websocket.
here code:
private static void loginandsavejsessionidcookie(final string user, final string password, final httpheaders headerstoupdate) { string url = "http://localhost:" + port + "/websocket-services/login.html"; new resttemplate().execute(url, httpmethod.post, new requestcallback() { @override public void dowithrequest(clienthttprequest request) throws ioexception { system.out.println("start login attempt"); multivaluemap<string, string> map = new linkedmultivaluemap<>(); map.add("username", user); map.add("password", password); new formhttpmessageconverter().write(map, mediatype.application_form_urlencoded, request); } }, new responseextractor<object>() { @override public object extractdata(clienthttpresponse response) throws ioexception { system.out.println("got login repsonse"); headerstoupdate.add("cookie", response.getheaders().getfirst("set-cookie")); return null; } }); }
this works, (especially after websocket connection has timed out) there no response server , client stops responding while method hangs awaiting response.
could suggest fix or work around ? causes client freeze completely, requiring force close.
to async code threading best way , can use executorservice specify timeout wish have. following 2 options available per need (pls chk api know difference between them) :-
<t> list<future<t>> invokeall(collection<? extends callable<t>> tasks, long timeout, timeunit unit) throws interruptedexception;
or
<t> t invokeany(collection<? extends callable<t>> tasks, long timeout, timeunit unit) throws interruptedexception, executionexception, timeoutexception;
Comments
Post a Comment