Get application status from Tomcat

The project in which I'm working on right now has a feature named "Check Component Status". In our case, we've a large project that consists many microservices. We've deployed these microservices in different servers. Now, we are interested to check which microservice is up and which is down. Also we're interested to know which service has not been deployed yet. So, we created a tiny application that would check the status of the microservices. Some of our microservices were deployed in tomcat and some were in Wildfly. In this post I'm going to explain how we checked the component/microservice status from tomcat.

Tomcat already has a management application which is installed by default in the 'management' context path. If we're running tomcat locally on port 8080, we can access the default tomcat application page here: http://localhost:8080/. Clicking on the "Manager App" link or going to the http://localhost:8080/manager/html will prompt for username and password. After entering the credentials it'll take us to the manager application UI.

So, in order to access the management application, we must have a user created who has access to the management application. Go to the conf directory of the tomcat root and open the tomcat-users.xml file. In this file, add an user with role manager-gui like the following:

<user username="admin" password="admin" roles="manager-gui" />

Now, you can access the gui of the management application by providing username=admin and password=admin.

Management application also exposes an API throuh which we can perform the management operations without using the gui application. In order to do this, we need an user with role=manager-script. You can add this role to an existing user like this

<user username="admin" password="admin" roles="admin,manager-gui, manager-script"/>

or add another user with the manager-script role. However, adding separate user is the suggested way to do this.

Now, we need to access the API from our code:

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
String url = "http://localhost:8080/manager/text/list";
HttpGet req = new HttpGet(url);
CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
InputStream responseStream = null;
String res = null;
HttpResponse response = client.execute(requestBase);
HttpEntity responseEntity = response.getEntity();
responseStream = responseEntity.getContent();

BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
StringBuilder sb = new StringBuilder();

String line;

while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
}

br.close();
res = sb.toString();

System.out.println(res);

The above code will print something like this:

OK - Listed applications for virtual host localhost
/:running:0:ROOT
/examples:running:0:examples
/host-manager:running:0:host-manager
/manager:running:1:manager
/docs:running:0:docs
/WebApplication1:running:0:WebApplication1

You can check the status of any deployed application by parsing this output. To know more about other management options, please look here https://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html

Comments

  1. Your style is very unique compared to other folks I have read stuff from. Many thanks for posting when you have the opportunity, Guess I'll just bookmark this blog.

    ReplyDelete
  2. We are a group of volunteers and starting a new scheme in our community. Your site provided us with useful information to work on. You have done a formidable job and our entire neighborhood shall be thankful to you.

    ReplyDelete

Post a Comment

Popular posts from this blog

Run tasks in background in Spring

Conditional field inclusion in Jackson and Spring Boot

How to configure Wildfly 10 to use MySQL