I have a controller that calls an external webservice (WS). When it calls the WS but this doen´t process anything because it´s already updated, it returns OK(200) and my controller returns success (AJAX). However, if the WS has to process an update and it takes a few minutes (not too long), it still returns OK(200) but my controller returns ERROR 500.
I also try calling the controller with a submit button, instead of AJAX, with the same result.
Hope I have explained well. Can anyone help?
callWebService controller
@PostMapping(value = "/xyz.htm", params="callWebService")
@ResponseBody
public String callWebService(final HttpSession session, HttpServletRequest request) {
String resultado="{"messageType":"error","message":"Updated error"}";
try {
SendNotification sendNotification = new SendNotification();
if (SendNotification.send()) {
resultado="{"messageType":"info","message":"Updated"}";
}
}catch (Exception e) {
logger.error("xyz.callWebService -- Error calling WS", e);
}
return resultado;
}
SendNotification class
public boolean send() throws Exception {
String token = getToken();
if (token != null && !token.isEmpty()) {
Client client = null;
WebTarget myResource = null;
Response res = null;
try {
client = ClientBuilder.newBuilder().sslContext(Conexiones.configurarSSL()).build();
myResource = client.target(urlAvisos);
Invocation.Builder builder = myResource.request(MediaType.APPLICATION_JSON);
builder.header(HttpHeaders.AUTHORIZATION, "Bearer " + token);
res = builder.post(Entity.entity("{}", MediaType.APPLICATION_JSON));
if (res.getStatusInfo().getReasonPhrase().equalsIgnoreCase("ok")) {
logger.error("sent - OK");
return true;
} else {
logger.error("sent- not OK");
return false;
}
} catch (Exception ex) {
logger.error("SendNotification.send", ex);
} finally {
if (res != null) {
res.close();
}
if (client != null) {
client.close();
}
}
} else {
return false;
}
}
The HTTP client you are using (JAX-RS Client) may have a default timeout configuration that is too short. If the external service takes longer than this timeout to respond, it could result in an error.
So, try increasing the timeout settings for your HTTP client. For example:
Client client = ClientBuilder.newBuilder()
.sslContext(Conexiones.configurarSSL())
.connectTimeout(10, TimeUnit.SECONDS) // adjust as necessary
.readTimeout(30, TimeUnit.SECONDS) // adjust as necessary
.build();
2