You can access WildFly Management API through multiple channels, one of this is HTTP and JSON.In this article we will learn how to do it and which are the advantages of using this approach.
Use cases for WildFly HTTP Management API
Although the recommended approach for managing WildFly remains the Command Line Interface, in some cases it would be useful to manage the application server with just as little as basic tools such as curl. For example, if you haven’t got a local jboss-client.sh and you cannot access a browser, then WildFly HTTP Management API becomes a valid substitute.
A basic example of HTTP Management API
You can use WildFly Management interface with just an HTTP GET request. For example, let’s see how to check the server state:
http://localhost:9990/management?operation=attribute&name=server-state
The browser will prompt for authentication. After that, it will return the server status attribute in JSON format:
On the other hand, if you want to automate the HTTP request, you can use a command line tool such as curl. Here is how to check the server state with curl:
curl --digest http://localhost:9990/management --header "Content-Type: application/json" -u admin:admin -d '{"operation":"read-attribute","name":"server-state","json.pretty":1}'
On a Windows machine you might use other tools such as the Power Shell tool to perform an equivalent command:
$headers = @{ "Content-Type" = "application/json" } $credentials = Get-Credential -UserName "admin" -Message "Enter your credentials" $body = '{"operation":"read-attribute","name":"server-state","json.pretty":1}' Invoke-RestMethod -Uri "http://localhost:9990/management" -Headers $headers -Method POST -Credential $credentials -Body $body
Other examples
Now that we know the basic of JBoss / WildFly HTTP Management API, let’s see some other examples you can use.
How to check the server log:
curl --digest -L -D - http://127.0.0.1:9990/management?useStreamAsResponse --header "Content-Type: application/json" -u admin:admin -d '{"operation":"read-attribute","address":[{"subsystem":"logging"},{"log-file":"server.log"}],"name":"stream"}'
How to shutdown the application server
curl --digest http://localhost:9990/management --header "Content-Type: application/json" -u admin:admin -d '{"operation":"shutdown","blocking":true}'
How to display the JNDI tree:
curl --digest http://localhost:9990/management --header "Content-Type: application/json" -u admin:admin -d '{"operation":"jndi-view", "address":["subsystem","naming"], "json.pretty":1}'
How to test a Datasource’s connection pool:
curl --digest http://localhost:9990/management --header "Content-Type: application/json" -u admin:admin -d '{"operation":"test-connection-in-pool","address":[{"subsystem":"datasources"},{"data-source":"ExampleDS"}]}'
How to restart servers in Domain Mode for the main-server-group
curl --digest http://localhost:9990/management --header "Content-Type: application/json" -u admin:admin -d '{"operation":"restart-servers","address":[{"server-group":"main-server-group"}]}'
Application Management with HTTP / JSON API
Finally, some examples you can use to deploy or undeploy an application.
How to undeploy the application app.war
curl -S -H "content-Type: application/json" -d '{"operation":"undeploy", "address":[{"deployment":"app.war"}]}' --digest http://user:password@hostname:9990/management
How to deploy an application
bytes_value=`curl -F "file=@/path/to/app.war" --digest http://user:password@$hostname:9990/management/add-content | perl -pe 's/^.*"BYTES_VALUE"\s*:\s*"(.*)".*$/$1/'`
Conclusion
This article showed some examples of HTTP Management API to control the resources of the application server with basic tools such as curl