0

I have a web application which pings REST service using javax.ws.rs.client when it is being deployed to tomcat. Rest service is not available on purpose.

However, if this pinging is not on a seperate thread, tomcat application will not deploy and will be stuck (I'm guessing some timeout for HTTP call?).

I can't seem to figure out why is that. Must application be deployed to tomcat to use javax.ws.rs.client since tomcat provides implementation?

These 2 methods are used to ping REST service:

private Object sendGetRequest(WebTarget webTarget, Class<?> returnClass) throws BatchConnectionException {
    if (returnClass != null) {
        return webTarget.request().get(returnClass);
    } else {
        Response response = webTarget.request().get();
        validateResponseStatus(response.getStatus());
        return response;
    }
}

public boolean isRestAccessible() {
    boolean accessible = true;
    try {
        batchTaskUtils.sendRequest(batchTaskUtils.getBatchWebTarget().path(Resources.ROOT), String.class,
                BatchTaskUtils.RequestType.GET);
    } catch (BatchConnectionException e) {
        accessible = false;
    }
    return accessible;
}

Does not work as exptected:

public class App extends GuiceServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        super.contextInitialized(servletContextEvent);
        // stuck here
        isRestAccessible();
    }
}

Works as expected:

public class App extends GuiceServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        super.contextInitialized(servletContextEvent);
        ExecutorService loggingExecutor = Executors.newFixedThreadPool(1);
        loggingExecutor.execute(() -> {
            isRestAccessible();
        });
    }
CrazySabbath
  • 1,274
  • 3
  • 11
  • 33
  • contextInitialized is called in deploy and when rest call has no response everything awaits, but when you run the call in seperate thread deploy happens instantly probably other thread waits forever, better to set time out https://stackoverflow.com/a/19541931/175554 and change the flow accordingly, like after 10 second if rest does not responds or throws exception just kill the app. just an ange :) – ozkanpakdil Jan 10 '21 at 19:45
  • Thanks, I tested with timeout, and you are correct, it works. But I was confused why was there no response? Because in a separate thread, I get REST response almost immediately. – CrazySabbath Jan 11 '21 at 10:15
  • probably something else is blocking the client, you can enable debug logs or trace logs and try to find out, my suggestion just move on :) no need to loose time on small things. – ozkanpakdil Jan 11 '21 at 15:08

0 Answers0