0

I am trying to test ActiveMQ connection and return a value. it crashes on line:

httpResponse  = client.execute(theHttpGet); 

It is not my code I am trying to debug it. Can anyone help me to understand why the code is using HttpGet?

  public ActivemqBrokerInfo(String serverAddress, int port, String apiUrl, int timeout) {

    // Default Activemq location
    this.serverAddress = String.format("http://%s:%s/%s", serverAddress, port, apiUrl);

    int timeoutInMs = timeout;

    HttpClientBuilder builder = HttpClientBuilder.create();
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeoutInMs).build();
    builder.setDefaultRequestConfig(requestConfig);

    client = builder.build();
  }

  public ActivemqBrokerInfo(String serverAddress) {

    this(serverAddress, DEFAULT_PORT, DEFAULT_API_URL, DEFAULT_TIMEOUT);
  }

  @Override
  public boolean testConnection() {

    HttpGet theHttpGet = new HttpGet(serverAddress);
    theHttpGet.addHeader("test-header-name", "test-header-value");
    HttpResponse httpResponse = null;

    try{
      httpResponse  = client.execute(theHttpGet);// Code is crashing on this line 
    } catch (IOException ex){
      LOGGER.error("Broker down: ", ex);
    }

    return httpResponse != null;
  }
Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
rszaman
  • 705
  • 1
  • 5
  • 12
  • Couple of question. What's the value of `DEFAULT_API_URL`? Are you using the default value or are you using your own? If you're using your own `apiUrl` value what is it? What do you mean it "crashes"? Does it throw an exception? If so, what's the exception? – Justin Bertram May 29 '19 at 19:11
  • private static final String DEFAULT_API_URL = "api/jolokia/"; url here is http://127.0.0.1:8161/api/jolokia/ So application completely stops there and the page goes blank. It is not returning anything for testConnection @JustinBertram – rszaman May 30 '19 at 12:39

1 Answers1

0

When ActiveMQ runs is normally starts an embedded web server. This web server is used to host the web admin console as well as the Jolokia endpoint which acts as an HTTP facade in front of the broker's MBeans. In other words, any client can send HTTP requests to specially formed URLs on the broker to get results from the underlying management beans. This is exactly what your bit of code appears to be doing. It appears to be sending an HTTP request to the Jolokia endpoint (i.e. api/jolokia) in order to determine if the broker is alive or not.

Based on the information provided it is impossible to determine why testConnection() is not returning successfully since you've included no information about the configuration or state of the broker.

I recommend you add additional logging to see what may be happening and also catch Exception rather than just IOException.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
  • It doesn't throw any error in the log. Is there any other was I can check the testConnection() – rszaman May 30 '19 at 16:50
  • After `httpResponse = client.execute(theHttpGet);` runs what is the value of `httpResponse`? – Justin Bertram May 30 '19 at 18:12
  • null. And if I step further it just stops responding. – rszaman May 30 '19 at 19:00
  • `null` seems like a valid return value for `client.execute(theHttpGet)` given that `testConnection()` actually checks for `null` when returning. Therefore `testConnection()` should return `false`. What do you mean "if I step further it just stops responding"? What stops responding? – Justin Bertram May 30 '19 at 19:33
  • testConnection() doesn't return anything. the debugger stops on that line. and the page(where is suppose to show value) goes blank – rszaman May 30 '19 at 19:53
  • The debugger stops on *what* line? And what do you mean it stops? Do you mean it's waiting for `execute` to return? Have you tried stepping into the `execute` method to see what's going on? – Justin Bertram May 30 '19 at 20:09
  • httpResponse = client.execute(theHttpGet);// Code is crashing on this line. it waits for execute to return which never return anything. If i try to step into it says "source not found" – rszaman May 30 '19 at 21:04
  • If the method never returns that doesn't mean it has "crashed". It could simply be waiting for a response. The timeout could be several minutes. You should find the source-code for your dependency, download it, and put it on your classpath so that you can step into it with your IDE. Most modern IDEs can do this automatically for Maven based projects. – Justin Bertram May 31 '19 at 01:08
  • Seems like it has some problem with dependency and my IDE is not able to update it. – rszaman Jun 03 '19 at 17:15